linux系统在安装完毕我们需要进行简单安全加固具体涉及到以下方面:
1. 密码长度修改:
[root@localhost ~]# vi /etc/login.defs
PASS_MIN_LEN 5 #这是系统默认密码最短为5位,需要设置最短密码长度为12,把5改为12即可。
2. 密码复杂度修改:
[root@localhost ~]# cp /etc/pam.d/system-auth /etc/pam.d/system-auth.bak #修改配置文件先备份旧配置文件
[root@localhost ~]# vi /etc/pam.d/system-auth
password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= minlen=10 lcredit=-1
ucredit=-1 dcredit=-1 ocredit=-1 enforce_for_root
参数解释:
minlen=10 密码最小长度为10个字符。
lcedit=-1 密码应包含的小写字母的至少一个
ucredit=-1 密码应包含的大写字母至少一个
dcredit=-1 将密码包含的数字至少为一个
ocredit=-1 设置其他符号的最小数量,例如@,#、! $%等,至少要有一个
enforce_for_root 确保即使是root用户设置密码,也应强制执行复杂性策略。
3. SSH服务默认端口修改:
[root@localhost ~]# vi /etc/ssh/sshd_config
Port 2200 #ssh服务端口修改为2200
[root@localhost ~]# systemctl restart sshd #重启ssh服务使其修改配置生效
4. 禁止root用户远程ssh登入:
修改完成以后如果想要使用root账户,用普通账户远程登入再用su - 命令切换到root账户
[root@localhost ~]# vi /etc/ssh/sshd_config
PermitRootLogin no #去掉原先注释,yes修改为no
[root@localhost ~]# systemctl restart sshd
5. SSH限制特定的IP远程登入:
方法一:
linux 服务器通过设置/etc/hosts.allow和/etc/hosts.deny这个两个文件,hosts.allow许可大于hosts.deny可以限制或者允许某个或者某段IP地址远程 SSH 登录服务器,方法比较简单,且设置后立即生效,不需要重启SSHD服务,具体如下:
[root@localhost ~]# vi /etc/hosts.allow
sshd:192.168.2.20:allow #允许192.168.2.20这个主机IP访问,其他ip访问都拒绝
sshd:192.168.2.*:allow #允许192.168.2.这个网段主机访问其他网段都拒绝
hosts.allow和hosts.deny两个文件同时设置规则的时候,hosts.allow文件中的规则优先级高,按照此方法设置后服务器只允许/etc/host
s.allow 这个配置文件IP地址的SSH登录,其它的IP都会拒绝。
[root@localhost ~]# vim /etc/hosts.deny
sshd:192.168.2.20:deny #拒绝192.168.2.20这个ip登入其他ip都允许
sshd:192.168.2.*:deny #拒绝192.168.2.这个网段ip访问其他网段都允许
方法二:
我们可以编辑/etc/ssh/sshd_config配置文件进行限时某个IP和组远程通过ssh服务连接主机,具体配置如下
AllowUsers:允许某个用户登入,其它都不能登录
AllowGroups:允许某个用户组登入,其它都不能登录
DenyUsers:拒绝某个用户登入,其它用户都能登录
DenyGroups:拒绝某个组登入,其它都能登录
[root@localhost ~]# vi /etc/ssh/sshd_config #打开配置文件添加以下配置
AllowUsers root@192.168.2.20 #允许192.168.2.20这台机器已root用户登入其他IP都不允许登入
[root@localhost ~]# vi /etc/ssh/sshd_config
DenyUsers root@192.168.2.20 #拒绝192.168.2.20 这个IP允许其他IP访问
[root@localhost ~]# vi /etc/ssh/sshd_config
AllowGroups root #允许root用户组访问,其他用户组拒绝
[root@localhost ~]# vi /etc/ssh/sshd_config
DenyGroups root #禁止root用户组访问,其他用户组允许