Linux服务器加固

1、密码有效期

方法一:

chage -l username #查看用户的过期时间

chage -M 99999 username #用命令修改过期时间为永久
chage -M 90 username #设置密码有效期为90天
chage -d 0 username #强制用户登陆时修改口令
chage -d 0 -m 0 -M 90 -W 15 username #强制用户下次登陆时修改密码,并且设置密码最低有效期0和最高有限期90,提前15天发警报提示
chage -E '2020-09-30' username #这个账号的有效期是2020-09-30

方法二:

vim /etc/login.defs

PASS_MAX_DAYS 90 #密码最长过期天数 99999为永不过期
PASS_MIN_DAYS 0 #密码最小过期天数
PASS_MIN_LEN 8 #密码最小长度
PASS_WARN_AGE 15 #密码过期警告天数

 

2、密码复杂度

  • 重复密码限制使用

[root@linuxprobe~]# vi /etc/pam.d/system-auth
# near line 15: prohibit to use the same password for 5 generation in past

password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok remember=5
  • 设置最小密码长度。用户不能设置小于此参数的密码长度。

复制代码
# set 8 for minimum password length
[root@linuxprobe~]# authconfig --passminlen=8 --update
# the parameter is set in a config below
[root@linuxprobe~]# grep "^minlen" /etc/security/pwquality.conf
minlen = 8 

# 在新密码中设置同一类的允许连续字符的最大数目
# set 4 for maximum number of allowed consecutive characters of the same class
[root@linuxprobe~]# authconfig --passmaxclassrepeat=4 --update
# the parameter is set in a config below
[root@linuxprobe~]# grep "^maxclassrepeat" /etc/security/pwquality.conf
maxclassrepeat = 4 

# 在新密码中至少需要一个小写字符。
 [root@linuxprobe~]# authconfig --enablereqlower --update
# the parameter is set in a config below
# (if you'd like to edit the value, edit it with vi and others)
[root@linuxprobe~]# grep "^lcredit" /etc/security/pwquality.conf
lcredit = -1 

# 在新密码中至少需要一个大写字符
 [root@linuxprobe~]# authconfig --enablerequpper --update
# the parameter is set in a config below
# (if you'd like to edit the value, edit it with vi and others)
[root@linuxprobe~]# grep "^ucredit" /etc/security/pwquality.conf
ucredit = -1 

# 在新密码中至少需要一个数字
 [root@linuxprobe~]# authconfig --enablereqdigit --update
# the parameter is set in a config below
# (if you'd like to edit the value, edit it with vi and others)
[root@linuxprobe~]# grep "^dcredit" /etc/security/pwquality.conf
dcredit = -1 

# 密码包括至少一个特殊字符
 [root@linuxprobe~]# authconfig --enablereqother --update
# the parameter is set in a config below
# (if you'd like to edit the value, edit it with vi and others)
[root@linuxprobe~]# grep "^ocredit" /etc/security/pwquality.conf
ocredit = -1 

# 在新密码中设置单调字符序列的最大长度。 (ex⇒'12345''fedcb')
 [root@linuxprobe~]# vi /etc/security/pwquality.conf
# add to the end
maxsequence = 3 

# 设置新密码中不能出现在旧密码中的字符数
 [root@linuxprobe~]# vi /etc/security/pwquality.conf
# add to the end
difok = 5 

# 检查来自用户passwd条目的GECOS字段的长度超过3个字符的字是否包含在新密码中。
 [root@linuxprobe~]# vi /etc/security/pwquality.conf
# add to the end
gecoscheck = 1 

# 设置不能包含在密码中的Ssace分隔的单词列表
 [root@linuxprobe~]# vi /etc/security/pwquality.conf
# add to the end
badwords = denywords1 denywords2 denywords3 

# 为新密码设置hash / crypt算法。 (默认为sha512)
# show current algorithm

[root@linuxprobe~]# authconfig --test | grep hashing

password hashing algorithm is md5
# chnage algorithm to sha512

[root@linuxprobe~]# authconfig --passalgo=sha512 --update
[root@linuxprobe~]# authconfig --test | grep hashing
password hashing algorithm is sha512 
复制代码

 

3、用户连续N次输入错误密码进行登陆时,自动锁定X分钟

方法一、在字符终端下,实现某一用户连续错误登陆N次后,就锁定该用户X分钟(pam_tally2)

复制代码
执行 vim /etc/pam.d/login
在#%PAM-1.0 下新起一行,加入
auth required pam_tally2.so deny=3 unlock_time=5 even_deny_root root_unlock_time=10
如果不限制root用户,则可以写成
auth required pam_tally2.so deny=3 unlock_time=5
even_deny_root 也限制root用户;
deny 设置普通用户和root用户连续错误登陆的最大次数,超过最大次数,则锁定该用户;
unlock_time 设定普通用户锁定后,多少时间后解锁,单位是秒;
root_unlock_time 设定root用户锁定后,多少时间后解锁,单位是秒;
复制代码

备注: 

  • 此处使用的是 pam_tally2 模块,如果不支持 pam_tally2 模块可以使用 pam_tally 模块。另外,不同的pam版本,设置可能有所不同,具体使用方法,可以参照相关模块的使用规则。 
  • 也可以直接在 system-auth 文件中直接添加这些命令,修改完成后,凡是调用 system-auth 文件的服务,都会生效。因为有自动解锁时间,所以,不用担心全部限制后,会出现永远无法登陆的”尴尬”情况。 
  • 可以使用 pam_tally2 -r -u username 命令,手动清除某用户记录次数。

  

方法二、设置Linux用户连续N次登陆失败时,自动锁定X分钟(pam_tally)

  如果想在所有登陆方式上,限制所有用户,可以在 /etc/pam.d/system-auth 中增加2行

auth required pam_tally.so onerr=fail no_magic_root
account required pam_tally.so deny=3 no_magic_root even_deny_root_account per_user reset
deny 设置普通用户和root用户连续错误登陆的最大次数,超过最大次数,则锁定该用户;
no_magic_root 连root用户也在限制范围,不给root特殊权限。
详细参数的含义,参见 /usr/share/doc/pam-xxxx/txts/README.pam_tally
如果不想限制root用户,可以将 even_deny_root_account 取消掉。

  针对不同服务来限制不同登陆方式:

#只在本地文本终端上做限制,可以编辑如下文件,添加的内容和上方一样。
vim /etc/pam.d/login
#只在远程telnet、ssh登陆上做限制,可以编辑如下文件,添加的内容和上方也一样。
vim /etc/pam.d/remote
vim /etc/pam.d/sshd

  手动解除锁定:

复制代码
#查看某一用户错误登陆次数:
pam_tally –user username
#例如,查看work用户的错误登陆次数:
pam_tally –user work
#清空某一用户错误登陆次数:
pam_tally –user username –reset
#例如,清空 work 用户的错误登陆次数:
pam_tally –user work –reset
faillog -r 命令亦可。
复制代码

  pam_tally没有自动解锁功能
  因为pam_tally没有自动解锁的功能,所以,在设置限制时,要多加注意,万一全做了限制,而 root用户又被锁定了,就只能够进单用户模式解锁了。当然,也可以添加crontab任务,达到定时自动解锁的功能,但需要注意的是,如果在/etc /pam.d/system-auth 文件中添加了pam_tally的话,当root被锁定后,crontab任务会失效,所以,最好不要在system-auth 文件中添加pam_tally。

 

4、配置审计账号

新增审计账号

useradd  cmaudit
passwd  cmaudit

修改审计账号权限使其只具有查看功能

使用visudo命令修改 /etc/sudoers 文件,增加配置

cmaudit ALL=(root) NOPASSWD: /usr/bin/cat,/usr/bin/less,/usr/bin/more,/usr/bin/tail,/usr/bin/head

 

5、设置登录超时

方法一:通过修改.bashrc或.bash_profile文件来实现

通过修改home目录下的.bashrc或.bash_profile文件来实现。这两个文件选择其中一个在末尾加入如下一行,具体操作如下

cd ~
echo "TMOUT=90">>.bashrc 
source .bashrc

以上代码中我们选择的是.bashrc文件,如果您选择的是.bash_profile,那么也是一样的操作流程,以上我们设置用户自动登出时间为90秒。

 

方法二:通过修改ssh的配置文件来实现

通过修改ssh的配置文件 /etc/ssh/sshd_config 实现超时自动登出功能

vim /etc/ssh/sshd_config

修改下面两行的数字

ClientAliveInterval 60
ClientAliveCountMax 5

保存后重启ssh服务

service sshd restart

这种方法对除了root之外的所有用户都是60秒登录超时自动登出。

  • ClientAliveInterval选项定义了每隔多少秒给SSH客户端发送一次信号;
  • ClientAliveCountMax选项定义了检测到多少次不活动后断开与ssh客户端连接;

 

方法三:通过修改ssh的配置文件来实现

在 /etc/profile 文件中53行附近添加TMOUT字段(单位为秒),然后 source /etc/profile 即可。

TMOUT=1200

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL TMOUT

设置完成后可以通过 set 命令来查询配置值

set | grep TMOUT

 

方法四:创建脚本

以 root 用户登录,创建文件 /etc/profile.d/autologout.sh ,并编辑内容加入

TMOUT=100
readonly TMOUT
export TMOUT

然后为它添加可执行权限

chmod +x /etc/profile.d/autologout.sh

现在,登出或者重启系统。非活动用户就会在 100 秒后自动登出了。普通用户即使想保留会话连接但也无法修改该配置了,他们会在 100 秒后强制退出。

 

6、自动备份审计日志

使用 crontab 来对最近6个月的审计日志进行备份

1. 打开终端并使用root用户登录。

2. 创建一个新的备份目录,用于存储审计日志的备份文件。例如,可以使用以下命令创建一个名为"audit_logs_backup"的目录:

mkdir /mnt/audit_logs_backup

3. 编写备份用的脚本文件

复制代码
 #!/bin/bash
 # 定义日志文件路径
LOG_FILE="/mnt/logfile.log"
 # 备份命令和逻辑
echo "$(date): Starting backup..." >> $LOG_FILE
tar -czPf /mnt/audit_logs_backup/audit_logs_$(date +\%Y\%m\%d).tar.gz /var/log/audit/
echo "$(date): Backup completed." >> $LOG_FILE
 # 清理命令和逻辑
echo "$(date): Starting cleanup..." >> $LOG_FILE
find /mnt/audit_logs_backup/ -type f -name 'audit_logs_*.tar.gz' -mtime +180 -exec rm {} \;
echo "$(date): Cleanup completed." >> $LOG_FILE
复制代码

4. 编辑crontab文件以设置定期备份任务。运行以下命令来编辑crontab文件:

crontab -e

5. 在打开的crontab文件中,添加以下行来设置备份任务:

0 0 * * 0 /mnt/audit_log_backup.sh

保存后就会在每周日0点在备份目录中创建一个新的压缩文件,并且删除超过6个月(180天)的旧文件了。

如果需要检查策略语法配置可以使用下面命令

crontab -l | grep -v '^#' | crontab -

 如果需要查看执行日志可以查看 /var/log/cron.log 文件

cron工具: crontab执行时间计算 - 在线工具 (tool.lu)

posted @   安培昌浩  阅读(184)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示