> 文章列表 > CentOS系统常规初始化操作详解

CentOS系统常规初始化操作详解

CentOS系统常规初始化操作详解

环境准备:

1)设置本地国际化语言为en_US.UTF-8

[root@c58 ~]# sed -i \'s/^\\(LANG=\\).*$/\\1\"en_US.UTF-8\"/\' /etc/sysconfig/i18n

[root@c58 ~]# cat /etc/sysconfig/i18n

LANG=\"en_US.UTF-8\"

[root@c58 ~]# LANG=en_US.UTF-8

2)更新系统软件包

备份默认yum源:

find /etc/yum.repos.d -name \'*.repo\' -exec mv {} {}.bak \\;

添加163yum源:

redhat5或centos5:

wget http://mirrors.163.com/.help/CentOS5-Base-163.repo -P /etc/yum.repos.d

redhat6或centos6

wget http://mirrors.163.com/.help/CentOS6-Base-163.repo -P /etc/yum.repos.d

添加epel yum源:

redhat5.x 32bit:

rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm

redhat5.x 64bit:

rpm -ivh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm

redhat6.x 32bit:

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

redhat6.x 64bit:

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

>

更新证书:

yum -y upgrade ca-certificates --disablerepo=epel

更新系统所有软件包:

yum clean allyum makecacheyum -y upgrade

下文以redhat5/centos5为例

一、服务最小化原则

关闭所有开机自启动服务,仅开启sshd、crond、network、iptables、syslog(redhat5)、rsyslog(redhat6),然后在此基础上按需添加需要开机启动的服务。

1)关闭所有开机自启动服务

[root@c58 ~]# for i in `chkconfig --list | awk \'{if ($1~/^$/) {exit 0;} else {print $1}}\'`; do chkconfig $i off; done

2)开启基础服务

[root@c58 ~]# for i in sshd network syslog crond iptables; do chkconfig $i on; done

3)查看开启的服务

[root@c58 ~]# chkconfig --list | grep \'3:on\'

crond 0:off 1:off 2:on3:on4:on5:on6:off

iptables0:off 1:off 2:on3:on4:on5:on6:off

network 0:off 1:off 2:on3:on4:on5:on6:off

sshd0:off 1:off 2:on3:on4:on5:on6:off

syslog0:off 1:off 2:on3:on4:on5:on6:off

二、用户登录限制

1)禁止使用root用户使用远程ssh

[root@c58 ~]# cd /etc/ssh

[root@c58 ssh]# cp sshd_config sshd_config~

[root@c58 ssh]# sed -i \'s/#\\(PermitRootLogin \\)yes/\\1no/\' sshd_config

[root@c58 ssh]# grep \'PermitRoot\' /etc/ssh/sshd_config

PermitRootLogin no

2)禁用登录提示信息

[root@c58 ssh]# >/etc/motd

3)修改ssh的默认监听端口(tcp:22)

#这里修改为tcp的11983端口

[root@c58 ssh]# sed -i \'s/#\\(Port \\)22/\\11983/\' sshd_config

[root@c58 ssh]# grep \'Port \' sshd_config

Port 11983

4)只允许指定的ip可以ssh (可选)

方法1(使用tcpwrapper):

#只允许192.168.124.0网段的ip使用ssh

echo \"sshd:192.168.124.0/255.255.255.0\" >> /etc/hosts.allow

echo \"sshd:ALL\" >> /etc/hosts.deny

方法2(使用iptables):

#注意,远程操作时需留心,以免把自己也拒绝而导致无法远程连接。如只允许192.168.1.0网段的所有ip进行ssh,其他所有ip都拒绝#先允许自己的ip,以防被后面的操作误伤

iptables -I INPUT -s 10.0.0.1 -p tcp --dport 22 -j ACCEPT

#允许192.168.1.0网段

iptables -I 2 INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

#拒绝所有

iptables -I 3 INPUT -p tcp --dport 22 -j DROP

#保存iptables的设置:

cp /etc/sysconfig/iptables /etc/sysconfig/iptables~

iptables-save > /etc/sysconfig/iptables

最后,重启sshd服务使上面配置生效(不用担心重启时已打开的远程终端连接会断开,重启只会对新开的终端生效)

[root@c58 ssh]# /etc/init.d/sshd restart

Stopping sshd: [OK]

Starting sshd: [OK]

三、用户及命令权限最小化
Photoshop教程详解