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

2020-01-30 16:32:41王冬梅

sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

syslog          0:off   1:off   2:on    3:on    4:on    5:on    6: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  ]