性能优化
生产环境 Linux 服务器安全加固:按 CIS 基线配置…
在需要加固生产 Linux 服务器或满足安全合规要求时,按 CIS 基线系统性地收紧 SSH、用户、防火墙、内核、文件权限与审计配置。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 49/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 85/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
生产环境 Linux 服务器安全加固:按通用
解决思路
- SSH 加固:编辑 /etc/ssh/sshd_config,设置 PermitRootLogin no、PasswordAuthentication no、PubkeyAuthentication yes、MaxAuthTries 3、ClientAliveInterval 300、ClientAliveCountMax 2、AllowUsers deploy admin、Protocol 2。
- 用户密码策略:安装 libpam-pwquality(sudo apt install libpam-pwquality),在 /etc/security/pwquality.conf 中设置 minlen = 14、dcredit = -1、ucredit = -1、ocredit = -1、lcredit = -1。
- 锁定不活跃账户:执行 useradd -D -f 30 设置默认 30 天未登录即锁定。
- 审计 sudo 使用:将 Defaults logfile=/var/log/sudo.log 追加到 /etc/sudoers。
- 防火墙(UFW):执行 ufw default deny incoming、ufw default allow outgoing、ufw allow ssh、ufw allow 443/tcp、ufw enable。
- 防火墙(iptables 替代方案):设置 iptables -P INPUT DROP、iptables -P FORWARD DROP,放行本地回环 iptables -A INPUT -i lo -j ACCEPT,放行已建立连接 iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT,放行 SSH iptables -A INPUT -p tcp --dport 22 -j ACCEPT。
- 内核加固:在 /etc/sysctl.d/99-security.conf 中设置 net.ipv4.conf.all.send_redirects = 0、net.ipv4.conf.all.accept_redirects = 0、net.ipv4.conf.all.accept_source_route = 0、net.ipv4.icmp_echo_ignore_broadcasts = 1、kernel.randomize_va_space = 2、fs.suid_dumpable = 0,然后执行 sysctl -p 应用。
- 关键文件权限:chmod 600 /etc/shadow、chmod 644 /etc/passwd、chmod 700 /root、chmod 600 /etc/ssh/sshd_config。
- 排查风险文件:用 find / -type f -perm -0002 -ls 查找全局可写文件,用 find / -perm -4000 -type f -ls 查找 SUID 文件。
- 审计配置:安装 auditd(apt install auditd),在 /etc/audit/rules.d/audit.rules 中添加 -w /etc/passwd -p wa -k identity、-w /etc/shadow -p wa -k identity、-w /etc/sudoers -p wa -k actions、-a always,exit -F arch=b64 -S execve -k exec。
- 最佳实践:禁用未使用的服务、保持系统更新、使用 fail2ban 做入侵防御、启用 SELinux/AppArmor、定期做安全审计、监控日志文件、实施最小权限原则。
适用边界 · 注意事项
- 在尚未确认 SSH 公钥登录可用前就禁用密码认证,可能导致自己被锁在服务器外
- 对非生产或临时调试环境过度加固,可能影响开发调试效率