作为IT工程师,确保Linux服务器安全是首要任务。以下是5个关键命令及其安全应用场景:
ss -tulnp | grep LISTEN
bash
ss -tulnp | grep -vE '(127.0.0.1|::1)' # 只显示外部可访问端口
last -aiF | head -20
bash
grep 'Accepted password' /var/log/auth.log # 查看SSH成功登录
grep 'Failed password' /var/log/auth.log # 查看暴力破解尝试
find / -perm -4000 -type f 2>/dev/null
bash
find / -perm -4000 -exec ls -la {} \; | grep -vE '/usr/bin|/bin'
# 检查非标准路径的SUID文件
lsof -i -P -n | grep ESTABLISHED
bash
lsof -iTCP -sTCP:ESTABLISHED -nP +c 10 | awk '{print $1,$3,$9}' | sort | uniq
# 显示进程名、用户和连接信息
journalctl -u sshd --since "1 hour ago" -p warning
bash
journalctl -u sshd -f | grep --line-buffered "Failed" | awk '{print $11}' | sort | uniq -c | sort -nr
# 实时统计攻击源IP
进阶安全实践: 1. 定期审计脚本:
#!/bin/bash
echo "[$(date)] Security Audit Report" > /var/log/security_audit.log
{
echo "### Open Ports ###"; ss -tulnp
echo "### SUID Files ###"; find / -perm -4000 2>/dev/null
echo "### Sudoers ###"; grep -v '^#' /etc/sudoers
} >> /var/log/security_audit.log
apt install inotify-tools
inotifywait -m -r /etc/ssh /etc/passwd /etc/shadow --format '%w%f %e' | while read file; do
echo "ALERT: $file modified at $(date)" | mail -s "File Change Alert" admin@example.com
done
提示:所有审计命令建议通过sudo或root执行,普通用户可能权限不足。对于生产环境,建议将这些检查集成到SIEM系统或定期cron任务中。