系统更新与补丁管理
yum update
(RHEL/CentOS) 或 apt update && apt upgrade
(Debian/Ubuntu)配置自动安全更新:
# RHEL/CentOS 8+
dnf install dnf-automatic
systemctl enable --now dnf-automatic.timer
# Ubuntu/Debian
apt install unattended-upgrades
dpkg-reconfigure unattended-upgrades
最小化安装原则
apt autoremove
或 dnf autoremove
SSH安全加固
# 编辑 /etc/ssh/sshd_config
Port 2222 # 修改默认端口
PermitRootLogin no # 禁止root直接登录
PasswordAuthentication no # 禁用密码认证,仅使用密钥
MaxAuthTries 3 # 最大尝试次数
ClientAliveInterval 300 # 客户端活动检查间隔
ClientAliveCountMax 0 # 不活动时断开连接
AllowUsers your_username # 只允许特定用户登录
重启SSH服务:systemctl restart sshd
用户账户安全
bash
useradd -m -s /bin/bash adminuser
passwd adminuser
usermod -aG sudo adminuser # Debian/Ubuntu
usermod -aG wheel adminuser # RHEL/CentOS
设置密码策略:
# 编辑 /etc/login.defs
PASS_MAX_DAYS 90
PASS_MIN_DAYS 7
PASS_WARN_AGE 14
# 安装密码复杂度模块
apt install libpam-pwquality # Debian/Ubuntu
yum install pam_pwquality # RHEL/CentOS
# 配置 /etc/security/pwquality.conf
minlen = 12
minclass = 3
SUDO权限控制
/etc/sudoers
或 /etc/sudoers.d/
下的文件:
%admin ALL=(ALL) ALL
%wheel ALL=(ALL) ALL
Defaults logfile="/var/log/sudo.log"
Defaults log_input, log_output
防火墙配置
bash
systemctl enable --now firewalld
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
firewall-cmd --list-all
bash
ufw allow 2222/tcp
ufw allow http
ufw enable
ufw status verbose
网络服务限制
/etc/hosts.allow
和 /etc/hosts.deny
)bash
systemctl list-unit-files --type=service | grep enabled
systemctl disable <unnecessary_service>
文件权限管理
bash
chmod 750 /home/*
chmod 700 /etc/ssh/ssh_host*_key
chmod 644 /etc/ssh/ssh_host*_key.pub
chmod 600 /etc/ssh/sshd_config
bash
# 编辑 /etc/profile 或 /etc/bashrc
umask 027
文件完整性检查
安装AIDE (Advanced Intrusion Detection Environment):
apt install aide # Debian/Ubuntu
yum install aide # RHEL/CentOS
aideinit
mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db
bash
aide --check
日志配置
bash
# 编辑 /etc/rsyslog.conf
*.emerg :omusrmsg:*
auth.* /var/log/auth.log
authpriv.* /var/log/secure
/etc/logrotate.conf
和 /etc/logrotate.d/
入侵检测
安装fail2ban:
apt install fail2ban # Debian/Ubuntu
yum install fail2ban # RHEL/CentOS
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 86400
SELinux/AppArmor
bash
getenforce # 检查状态
setenforce 1 # 临时启用
bash
aa-status # 检查状态
systemctl start apparmor
内核参数加固
/etc/sysctl.conf
:
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.default.rp_filter=1
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.conf.all.accept_redirects=0
net.ipv6.conf.all.accept_redirects=0
kernel.exec-shield=1
kernel.randomize_va_space=2
应用设置:sysctl -p
安全审计工具
bash
apt install lynis # Debian/Ubuntu
yum install lynis # RHEL/CentOS
lynis audit system
bash
yum install openscap-scanner scap-security-guide
oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_stig \
--results scan-results.xml \
--report scan-report.html \
/usr/share/xml/scap/ssg/content/ssg-centos7-ds.xml
定期检查清单
cat /etc/passwd
find / -perm -4000 -type f -exec ls -la {} \; 2>/dev/null
ss -tulnp
或 netstat -tulnp
ls -la /etc/cron*
和 crontab -l
通过实施以上安全措施,可以显著提高Linux服务器的防御能力。但请记住,安全是一个持续的过程,需要定期审查和更新安全策略。