最小化安装
系统更新
yum update -y
yum install yum-plugin-security -y
yum update --security -y
禁用不必要的服务
systemctl list-unit-files | grep enabled
systemctl disable <不必要的服务>
密码策略
# 编辑/etc/login.defs
PASS_MAX_DAYS 90
PASS_MIN_DAYS 7
PASS_MIN_LEN 8
PASS_WARN_AGE 14
# 安装cracklib加强密码复杂度
yum install cracklib -y
SSH安全配置
# 编辑/etc/ssh/sshd_config
Port 2222 # 修改默认端口
PermitRootLogin no
MaxAuthTries 3
LoginGraceTime 1m
AllowUsers your_username
Protocol 2
重启SSH服务:systemctl restart sshd
sudo权限控制
启用firewalld
systemctl enable firewalld
systemctl start firewalld
配置防火墙规则
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=2222/tcp # SSH自定义端口
firewall-cmd --reload
隐藏版本信息
# 在/etc/httpd/conf/httpd.conf中添加
ServerTokens Prod
ServerSignature Off
禁用目录浏览
Options -Indexes
限制HTTP方法
<Directory "/var/www/html">
<LimitExcept GET POST HEAD>
Deny from all
</LimitExcept>
</Directory>
隐藏版本信息
# 在/etc/nginx/nginx.conf的http块中添加
server_tokens off;
禁用不必要的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}
关键目录权限
chmod 750 /etc/httpd/conf/
chmod 640 /etc/httpd/conf/httpd.conf
chown root:root /etc/httpd/conf/httpd.conf
设置不可变属性
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/group
chattr +i /etc/gshadow
启用日志记录
# 配置rsyslog
yum install rsyslog
systemctl enable rsyslog
systemctl start rsyslog
安装fail2ban防暴力破解
yum install epel-release
yum install fail2ban
systemctl enable fail2ban
systemctl start fail2ban
自动安全更新
yum install yum-cron -y
systemctl enable yum-cron
systemctl start yum-cron
定期安全检查
yum install lynis
lynis audit system
备份策略
SELinux配置
# 查看状态
sestatus
# 设置为enforcing模式
setenforce 1
# 永久生效
sed -i 's/SELINUX=permissive/SELINUX=enforcing/g' /etc/selinux/config
安装ModSecurity WAF
# 对于Apache
yum install mod_security mod_security_crs
# 对于Nginx
# 需要从源码编译安装
配置HTTP安全头
# 在Web服务器配置中添加
Header set X-XSS-Protection "1; mode=block"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-Content-Type-Options "nosniff"
Header set Content-Security-Policy "default-src 'self'"
通过以上步骤,您的CentOS Web服务器将获得显著的安全提升。请根据实际业务需求调整配置,并定期复查安全设置。