Linux 日志审计与轻量级入侵检测实践

日志是安全排查的第一手资料 系统层面的关键日志位置: /var/log/auth.log(Debian/Ubuntu)或 journalctl -u sshd:登录与认证记录 /var/log/syslog / journalctl:系统级事件 /var/log/nginx/access.log:Web 访问记录 auditd 日志(/var/log/audit/audit.log):文件访问与系统调用审计 快速排查异常登录 # 查看最近登录成功记录 last -20 # 查看登录失败记录 sudo grep "Failed password" /var/log/auth.log | tail -50 # 统计失败登录来源 IP 排名 sudo grep "Failed password" /var/log/auth.log \ | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head 使用 auditd 监控关键文件 sudo apt install auditd sudo systemctl enable --now auditd # 监控 /etc/passwd 的写入操作 sudo auditctl -w /etc/passwd -p wa -k passwd_changes # 查询相关审计记录 sudo ausearch -k passwd_changes 规则可写入 /etc/audit/rules.d/audit.rules 实现持久化,重启后仍然生效。 ...

2026-07-27 · 1 min · 128 words · ITNote

Linux 防火墙配置指南:ufw 与 nftables

ufw:面向新手的简化管理 ufw(Uncomplicated Firewall)是对 iptables/nftables 的简化封装,适合快速配置基础规则。 sudo apt install ufw # 默认策略:拒绝入站,允许出站 sudo ufw default deny incoming sudo ufw default allow outgoing # 放行必要端口 sudo ufw allow 22022/tcp # SSH(对应自定义端口) sudo ufw allow 80/tcp sudo ufw allow 443/tcp # 限制某端口的来源 IP 段 sudo ufw allow from 192.168.1.0/24 to any port 8000 sudo ufw enable sudo ufw status verbose nftables:更细粒度的现代方案 nftables 是 iptables 的继任者,语法更统一,规则集也更易维护。 sudo apt install nftables # /etc/nftables.conf table inet filter { chain input { type filter hook input priority 0; policy drop; ct state established,related accept iif lo accept tcp dport 22022 accept tcp dport { 80, 443 } accept # 限制单 IP 新建连接速率,缓解简单的连接洪泛 tcp dport 22022 ct state new limit rate 10/minute accept } chain forward { type filter hook forward priority 0; policy drop; } chain output { type filter hook output priority 0; policy accept; } } sudo nft -f /etc/nftables.conf sudo systemctl enable --now nftables 场景示例:仅内网可访问的 AI 推理服务 将 vLLM 等服务监听在内网接口,或通过防火墙限制外部访问: ...

2026-07-27 · 1 min · 187 words · ITNote

SSH 安全配置最佳实践

使用密钥认证替代密码登录 # 本地生成密钥对(推荐 ed25519) ssh-keygen -t ed25519 -C "your_email@example.com" # 上传公钥到服务器 ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server /etc/ssh/sshd_config 关键配置 # 禁用密码登录,强制密钥认证 PasswordAuthentication no PermitEmptyPasswords no # 禁止 root 远程登录 PermitRootLogin no # 更改默认端口,降低自动化扫描命中率(非核心防护手段,只是降噪) Port 22022 # 限制协议版本与登录尝试 Protocol 2 MaxAuthTries 3 LoginGraceTime 30 # 仅允许特定用户/组登录 AllowUsers deploy admin 修改后重启服务: sudo systemctl restart sshd 注意:修改端口或禁用密码登录前,务必保留一个已登录的会话窗口,避免配置错误导致无法远程访问。 使用 fail2ban 防暴力破解 sudo apt install fail2ban # /etc/fail2ban/jail.local [sshd] enabled = true port = 22022 maxretry = 5 bantime = 3600 findtime = 600 sudo systemctl enable --now fail2ban sudo fail2ban-client status sshd 双因素认证(可选增强) 对高安全要求场景,可结合 libpam-google-authenticator 实现 SSH 二次验证,在密钥认证基础上再叠加一层动态口令。 ...

2026-07-27 · 1 min · 110 words · ITNote

Linux 系统安全加固基础清单

账户与权限 禁用 root 直接远程登录,改用普通账户 + sudo 为每个人员/服务分配独立账户,避免共享凭证 定期审查 sudo -l 权限范围,遵循最小权限原则 # 检查是否有异常的 sudo 权限 sudo -l -U username 更新与补丁 sudo apt update && sudo apt upgrade -y # 开启无人值守安全更新(Debian/Ubuntu) sudo apt install unattended-upgrades sudo dpkg-reconfigure --priority=low unattended-upgrades 文件与目录权限 # 查找全局可写的敏感文件 find / -xdev -type f -perm -0002 -not -path "/proc/*" 2>/dev/null # 查找 SUID/SGID 程序,核对是否为预期程序 find / -xdev \( -perm -4000 -o -perm -2000 \) -type f 2>/dev/null 关闭不必要的服务 systemctl list-unit-files --type=service --state=enabled sudo systemctl disable <不需要的服务> 内核参数加固(/etc/sysctl.conf) # 禁止 IP 转发(非路由器场景) net.ipv4.ip_forward = 0 # 防止 SYN Flood net.ipv4.tcp_syncookies = 1 # 忽略 ICMP 广播请求 net.ipv4.icmp_echo_ignore_broadcasts = 1 # 禁用来源路由 net.ipv4.conf.all.accept_source_route = 0 sudo sysctl -p 日志与审计 安装 auditd 记录关键系统调用与文件访问,配合 journalctl 定期审查异常登录尝试: ...

2026-07-27 · 1 min · 125 words · ITNote