2.1.配置密码策略
注意:所有密码策略配置完成之后,重启服务器。
2.1.1.缩短默认密码生存周期
vim /etc/login.defs 修改下面参数: PASS_MAX_DAYS 90 #新建用户的密码最长使用天数 PASS_MIN_DAYS 0 #新建用户的密码最短使用天数 PASS_MIN_LEN 8 #设定最小用户密码长度为8位 PASS_WARN_AGE 7 #新建用户的密码到期提前提醒天数 |
2.1.2.使用chage命令修改用户设置。(针对已创建用户)
例如,chage -m 0 -M 30 -I 5 -W 7 <用户名>
表示将此用户的密码最长使用天数设为30,最短使用天数设为0,密码过期后5天之后账户失效,过期前7天警告用户。
2.1.3.设置密码复杂度
1、备份原有配置文件 cp /etc/pam.d/system-auth /etc/pam.d/system-auth.bak 2、设置复杂度策略文件 vim /etc/pam.d/system-auth password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= #默认配置 找到包含pam_pwquality.so模块的行,将原有行注释并修改为如下的新配置: 密码长度最少10位,至少包含一个大写字母,一个小写字母,一个数字,一个特殊符号。 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个字符。 lcredit=-1 密码应包含的小写字母的至少一个 ucredit=-1 密码应包含的大写字母至少一个 dcredit=-1 将密码包含的数字至少为一个 ocredit=-1 设置其他符号的最小数量,例如@,#、! $%等,至少要有一个 enforce_for_root 确保即使是root用户设置密码,也应强制执行复杂性策略。 |
2.1.4.设置强制密码历史
# 修改pam认证文件 vim /etc/pam.d/system-auth # 添加、修改内容 password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok remember=5 |
2.1.5.设置账户锁定策略
vim /etc/pam.d/system-auth # 加在注释文档后一行就行,连续6次失败,就拉黑 auth required pam_tally2.so deny=6 unlock_time=300 even_deny_root root_unlock_time=60 # 查看登录失败次数 [root@bogon ~]# pam_tally2 --user hello Login Failures Latest failure From hello 6 05/31/23 10:36:23 pts/0 # 重置登录失败 pam_tally2 --user hello --reset |
2.2.限制超级管理员root远程登录
1.vim /etc/ssh/sshd_config # 进制root用户远程登录 PermitRootLogin no # 最大失败次数3 MaxAuthTries 3 2.重启sshd服务 systemctl restart sshd |
2.3.禁止任何人su至root账户
禁止任何人su至root账户,如果需要su切换,将用户加入wheel组。
1.修改su的配置文件,只有wheel组用户可以su至root vim /etc/pam.d/su auth sufficient /lib/security/pam_rootok.so auth required /lib/security/pam_wheel.so group=wheel 2.如果需要su切换,将用户加入wheel组 usermod -g wheel <用户名> |
2.4.赋予普通用户sudo权限
vim /etc/sudoers 添加以下内容: test ALL=(ALL) NOPASSWD:ALL #这样test用户就可以使用sudo,且无须输入密码。 |
2.5.修改ssh默认端口号
2.5.1.检查是否已安装ssh服务
[root@LF ~]# rpm -qa | grep ssh openssh-server-7.4p1-22.el7_9.x86_64 libssh2-1.8.0-4.el7.x86_64 openssh-clients-7.4p1-22.el7_9.x86_64 openssh-7.4p1-22.el7_9.x86_64 |
2.5.2.检查ssh服务是否已开启
# 显示running则表示ssh服务已开启并正在运行中 [root@LF ~]# systemctl status sshd.service ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 一 2024-09-30 10:19:29 CST; 17min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1215 (sshd) Tasks: 1 CGroup: /system.slice/sshd.service └─1215 /usr/sbin/sshd -D 9月 30 10:19:29 LF systemd[1]: Starting OpenSSH server daemon... 9月 30 10:19:29 LF sshd[1215]: Server listening on 0.0.0.0 port 22. 9月 30 10:19:29 LF sshd[1215]: Server listening on :: port 22. 9月 30 10:19:29 LF systemd[1]: Started OpenSSH server daemon. 9月 30 10:34:10 LF sshd[3122]: Did not receive identification string from 192.168.220.1 port 32313 9月 30 10:34:20 LF sshd[3126]: Did not receive identification string from 192.168.220.1 port 32335 9月 30 10:34:26 LF sshd[3128]: Accepted password for laofeng from 192.168.220.1 port 32336 ssh2 |
2.5.3.检查sshd进程运行状态
[root@LF ~]# ps -ef | grep sshd root 1215 1 0 10:19 ? 00:00:00 /usr/sbin/sshd -D root 3128 1215 0 10:34 ? 00:00:01 sshd: laofeng [priv] laofeng 3132 3128 0 10:34 ? 00:00:01 sshd: laofeng@pts/1 root 11633 6474 0 10:38 pts/1 00:00:00 grep --color=auto sshd |
2.5.4.检查sshd程序运行端口号
[root@LF ~]# netstat -anpl | grep sshd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1215/sshd tcp 0 0 192.168.220.128:22 192.168.220.1:32336 ESTABLISHED 3128/sshd: laofeng tcp6 0 0 :::22 :::* LISTEN 1215/sshd unix 3 [ ] STREAM CONNECTED 49220 3128/sshd: laofeng unix 2 [ ] DGRAM 49216 3128/sshd: laofeng unix 3 [ ] STREAM CONNECTED 22325 1215/sshd unix 3 [ ] STREAM CONNECTED 49219 3132/sshd: laofeng@ |
2.5.5.修改sshd默认端口
[root@LF ~]# vim /etc/ssh/sshd_config # $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $ # This is the sshd server system-wide configuration file. See # sshd_config(5) for more information. # This sshd was compiled with PATH=/usr/local/bin:/usr/bin # The strategy used for options in the default sshd_config shipped with # OpenSSH is to specify options with their default value where # possible, but leave them commented. Uncommented options override the # default value. # If you want to change the port on a SELinux system, you have to tell # SELinux about this change. # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER # #Port 22 Port 4422 #AddressFamily any #ListenAddress 0.0.0.0 #ListenAddress :: |
2.5.6.添加此端口号4422至防火墙,允许端口号通过防火墙
# 添加防火墙规则 [root@LF ~]# firewall-cmd --zone=public --add-port=4422/tcp --permanent Success # 重新加载防火墙规则 [root@LF ~]# firewall-cmd --reload Success # 重启防火墙 [root@LF ~]# systemctl restart firewalld.service # 查看防火墙状态 [root@LF ~]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: active (running) since 一 2024-09-30 10:59:50 CST; 3s ago Docs: man:firewalld(1) Main PID: 36568 (firewalld) Tasks: 2 CGroup: /system.slice/firewalld.service └─36568 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid 9月 30 10:59:49 LF systemd[1]: Starting firewalld - dynamic firewall daemon... 9月 30 10:59:50 LF systemd[1]: Started firewalld - dynamic firewall daemon. 9月 30 10:59:52 LF firewalld[36568]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration o...it now. Hint: Some lines were ellipsized, use -l to show in full. |
2.5.7.检查SELinux服务是否运行
# 由下面信息得知,SELinux是运行的 [root@LF ~]# sestatus -v | grep SELinux SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux |
2.5.8.使用semanage命令查看当前SELinux当中ssh运行端口号
[root@LF ~]# semanage port -l | grep ssh ssh_port_t tcp 22 |
2.5.9.使用semanage命令添加ssh运行端口号4422到SELinux中
[root@LF ~]# semanage port -a -t ssh_port_t -p tcp 4422 # 查看当前SELinux当中ssh运行端口号 # 再次查看ssh端口号已经多了4422号端口 [root@LF ~]# semanage port -l | grep ssh ssh_port_t tcp 4422, 22 |
2.5.10.重启sshd服务
[root@LF ~]# systemctl restart sshd.service [root@LF ~]# systemctl status sshd.service ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 一 2024-09-30 11:15:49 CST; 1s ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 45933 (sshd) Tasks: 1 CGroup: /system.slice/sshd.service └─45933 /usr/sbin/sshd -D 9月 30 11:15:49 LF systemd[1]: Starting OpenSSH server daemon... 9月 30 11:15:49 LF sshd[45933]: Server listening on 0.0.0.0 port 4422. 9月 30 11:15:49 LF sshd[45933]: Server listening on :: port 4422. 9月 30 11:15:49 LF systemd[1]: Started OpenSSH server daemon. |
2.5.11.登录测试
用XTerminal测试登陆,用新的ssh端口4422登陆服务器。
2.5.12.从防火墙规则中删除端口22
[root@LF ~]# firewall-cmd --zone=public --remove-port=22/tcp --permanent Warning: NOT_ENABLED: 22:tcp success [root@LF ~]# firewall-cmd --reload success [root@LF ~]# systemctl restart firewalld.service |
若想要删除自己添加的端口号4422可使用命令:
semanage port -d -t ssh_port_t -p tcp 4422