服务器安全加固的底层逻辑:最小权限与纵深防御
Linux服务器安全加固不是装个防火墙、改个SSH端口就完事的清单任务,而是一套围绕”最小权限原则”和”纵深防御策略”的体系化工程。攻击面的每一层——网络层、系统层、应用层、数据层——都需要独立的防护措施,任何单点突破不应导致全局沦陷。本文从SSH防护、内核参数调优、文件系统权限、审计日志到入侵检测,给出一套可直接落地的服务器安全加固方案。
SSH防护:从端口修改到证书认证的完整配置
SSH是Linux服务器最暴露的攻击面。默认22端口每天会收到数百次扫描和暴力破解尝试,仅改端口治标不治本。完整加固分三步:禁用密码认证、启用证书认证、限制登录来源。
/etc/ssh/sshd_config安全配置项:
# 禁用密码认证
PasswordAuthentication no
ChallengeResponseAuthentication no
# 禁用root直接登录
PermitRootLogin no
# 仅允许证书认证
PubkeyAuthentication yes
# 限制可登录用户
AllowUsers deploy admin@10.0.1.0/24
# 空闲超时断开
ClientAliveInterval 300
ClientAliveCountMax 2
# 限制认证尝试次数
MaxAuthTries 3
# 禁用不需要的转发
X11Forwarding no
AllowTcpForwarding no
AllowAgentForwarding no
密钥生成建议使用Ed25519算法,比RSA更短更快更安全:
ssh-keygen -t ed25519 -a 100 -C "deploy@prod-server-01"
-a 100设置密钥派生函数的迭代次数为100轮,增加暴力破解难度。部署到服务器时用ssh-copy-id推送公钥,确认可以证书登录后再禁用密码认证——顺序搞反会把自己锁在门外。
内核安全参数调优:sysctl关键配置详解
Linux内核默认配置面向通用场景,安全方面偏宽松。以下sysctl参数针对生产服务器加固:
# /etc/sysctl.d/99-security.conf
# 禁用IP转发(非路由器场景)
net.ipv4.ip_forward = 0
# 禁用源路由
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
# 启用反向路径过滤(防IP欺骗)
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# 禁用ICMP重定向
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# SYN flood防护
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_synack_retries = 2
# 连接超时优化
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_keepalive_probes = 3
# 禁用核心转储(防止敏感信息泄露)
fs.suid_dumpable = 0
# 限制ptrace权限(防调试注入)
kernel.yama.ptrace_scope = 2
# 启用ASLR(地址空间随机化)
kernel.randomize_va_space = 2
kernel.yama.ptrace_scope = 2是常被忽略的关键配置。ptrace_scope=2表示只有父进程可以ptrace子进程,防止攻击者通过ptrace注入已有进程。Docker容器内默认ptrace_scope=1,生产服务器建议直接设为2。tcp_syncookies = 1在SYN flood攻击时仍能正常建立合法连接,不丢半连接队列。
文件系统权限与关键目录加固
权限管理的核心原则:默认拒绝,按需授予。Linux的文件权限位(rwx)和ACL搭配使用,比单纯chmod更精细。
关键目录权限设置:
# /etc/shadow 只允许root读取
chmod 600 /etc/shadow
# /tmp 设置sticky bit防止互删
chmod 1777 /tmp
# 禁止普通用户执行su
dpkg-statoverride --update root root 4750 /bin/su # Debian/Ubuntu
# 或直接:chmod 4750 /bin/su && chgrp wheel /bin/su # RHEL/CentOS
# 关键日志文件不可删除
chattr +a /var/log/secure
chattr +a /var/log/audit/audit.log
敏感配置文件设置不可变属性,防止被篡改:
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/ssh/sshd_config
注意:chattr +i会导致软件更新失败,需要先chattr -i解除再升级,升级完重新锁定。可以在ansible/saltstack中做循环检查,确保关键文件的immutable属性始终存在。
审计日志与入侵检测:从auditd到OSSEC
安全加固的最后一道防线是检测。auditd记录系统调用级别的事件,可以追踪到”谁在什么时间对什么文件做了什么操作”。
auditd关键规则配置:
# /etc/audit/rules.d/audit.rules
# 监控/etc/passwd修改
-w /etc/passwd -p wa -k identity_changes
# 监控sudoers文件
-w /etc/sudoers -p wa -k sudoers_changes
# 监控SSH配置
-w /etc/ssh/sshd_config -p wa -k sshd_config_changes
# 监控crontab变更
-w /etc/crontab -p wa -k cron_changes
-w /etc/cron.d/ -p wa -k cron_changes
# 监控系统调用:网络连接
-a always,exit -F arch=b64 -S connect -k network_connections
# 监控删除操作
-a always,exit -F arch=b64 -S unlink -S unlinkat -S rename -S renameat -k file_deletions
# 监控root权限变更
-a always,exit -F arch=b64 -S setuid -S setgid -k privilege_escalation
auditd日志查询示例:
# 查找所有修改passwd文件的事件
ausearch -k identity_changes
# 查找某个用户的所有特权操作
ausearch -ua deploy -k privilege_escalation
# 生成审计报告
aureport --auth
aureport --file
日志分析方面,OSSEC/HIDS可以实时监控文件完整性变化、rootkit检测和日志异常模式匹配。在容器化环境中Falco是更轻量的替代方案,默认规则已覆盖容器逃逸、特权挂载等云原生威胁场景。
防火墙策略:iptables到nftables的规则设计
防火墙规则遵循”默认拒绝”原则——最后一条规则是DROP ALL,前面的规则只放行已知必要流量。
# nftables配置示例 /etc/nftables.conf
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
# 允许已建立的连接
ct state established,related accept
# 允许loopback
iif lo accept
# ICMP限速允许
icmp type echo-request limit rate 5/second accept
# SSH仅允许跳板机
tcp dport 22 ip saddr 10.0.1.100 accept
# HTTP/HTTPS
tcp dport {80, 443} accept
# Prometheus监控
tcp dport 9100 ip saddr 10.0.1.0/24 accept
# 记录被拒绝的连接
log prefix "nftables_drop: " drop
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
日志记录被拒绝的连接很重要——攻击探测、端口扫描都会被记录,是入侵检测的信号源。limit rate 5/second防止ICMP flood耗尽带宽。
自动化加固:Ansible Playbook批量执行
手动加固不适合规模化场景。用Ansible Playbook将上述所有加固项编排成可审计、可重复执行的自动化流程:
- name: Linux Server Security Hardening
hosts: production
become: yes
tasks:
- name: Apply sysctl security parameters
sysctl:
name: "{{ item.key }}"
value: "{{ item.value }}"
state: present
reload: yes
loop: "{{ security_sysctl_params | dict2items }}"
- name: Deploy SSH hardening config
template:
src: sshd_config.j2
dest: /etc/ssh/sshd_config
owner: root
group: root
mode: '0600'
notify: restart sshd
- name: Deploy auditd rules
copy:
src: audit.rules
dest: /etc/audit/rules.d/audit.rules
notify: restart auditd
- name: Lock critical system files
command: "chattr +i {{ item }}"
loop:
- /etc/passwd
- /etc/shadow
- /etc/ssh/sshd_config
handlers:
- name: restart sshd
service: name=sshd state=restarted
- name: restart auditd
service: name=auditd state=restarted
加固不是一次性动作。定期用Lynis等自动化审计工具扫描系统安全评分,将新发现的问题加入Playbook持续修复,形成安全闭环。一条纪律:所有安全变更必须通过配置管理工具执行,禁止手工修改——手工修改不可审计、不可回滚,本身就是安全风险。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/linux-fu-wu-qi-an-quan-jia-gu-shi-zhan-ssh-zheng-shu-ren/