系统更新与补丁管理
yum update -y
yum install yum-cron -y
systemctl enable yum-cron
systemctl start yum-cron
禁用不必要的服务
systemctl stop postfix
systemctl disable postfix
# 根据实际需求禁用其他不需要的服务
配置防火墙 (firewalld)
systemctl enable firewalld
systemctl start firewalld
# 只开放必要端口
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --remove-service=dhcpv6-client
firewall-cmd --reload
修改SSH默认端口
sed -i 's/#Port 22/Port 2222/' /etc/ssh/sshd_config
禁用root直接登录
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
启用密钥认证,禁用密码认证
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
限制SSH访问IP
echo "AllowUsers youruser@your.ip.address" >> /etc/ssh/sshd_config
应用SSH更改
systemctl restart sshd
隐藏服务器信息
echo "ServerTokens Prod" >> /etc/httpd/conf/httpd.conf
echo "ServerSignature Off" >> /etc/httpd/conf/httpd.conf
禁用目录浏览
sed -i 's/Options Indexes FollowSymLinks/Options -Indexes +FollowSymLinks/' /etc/httpd/conf/httpd.conf
安装并配置ModSecurity
yum install mod_security -y
systemctl restart httpd
配置HTTP严格传输安全(HSTS)
echo "Header always set Strict-Transport-Security \"max-age=63072000; includeSubDomains; preload\"" >> /etc/httpd/conf.d/ssl.conf
运行安全安装脚本
mysql_secure_installation
禁用远程root访问
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
FLUSH PRIVILEGES;
删除测试数据库
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
FLUSH PRIVILEGES;
设置适当的文件权限
chown -R root:apache /var/www/html/
chmod -R 750 /var/www/html/
find /var/www/html/ -type f -exec chmod 640 {} \;
配置敏感文件的权限
chmod 600 /etc/passwd /etc/shadow /etc/group /etc/gshadow
chmod 700 /root
启用并配置auditd
yum install audit -y
systemctl enable auditd
systemctl start auditd
配置日志轮转
yum install logrotate -y
安装并配置Fail2Ban
yum install epel-release -y
yum install fail2ban -y
systemctl enable fail2ban
systemctl start fail2ban
设置自动安全更新
yum install yum-plugin-security -y
定期检查rootkit
yum install rkhunter -y
rkhunter --update
rkhunter --propupd
echo "0 0 * * * root /usr/bin/rkhunter --cronjob --update --quiet" >> /etc/crontab
设置AIDE进行文件完整性检查
yum install aide -y
aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
echo "0 5 * * * root /usr/sbin/aide --check" >> /etc/crontab
SELinux配置
setenforce 1
sed -i 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config
内核参数加固
echo "net.ipv4.conf.all.rp_filter=1" >> /etc/sysctl.conf
echo "net.ipv4.conf.default.rp_filter=1" >> /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.conf
sysctl -p
安装并配置ClamAV防病毒
yum install clamav clamd -y
freshclam
systemctl enable clamd@scan
systemctl start clamd@scan
完成以上配置后,建议重启服务器以确保所有更改生效,并进行全面的安全测试。