# 查看当前登录用户
who
# 查看用户登录历史
last
# 添加用户并设置不可登录shell
sudo useradd -s /sbin/nologin username
# 修改用户密码策略
sudo chage -M 90 -m 7 -W 14 username
# 查看sudo权限用户
sudo grep -Po '^sudo.+:\K.*$' /etc/group
# 查找SUID/SGID文件
find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -l {} \;
# 查找世界可写文件
find / -type f -perm -o=w ! -type l -exec ls -l {} \;
# 检查文件完整性
sha256sum /path/to/file
# 设置文件不可修改属性
sudo chattr +i /path/to/important_file
# 查看开放端口
sudo netstat -tulnp
# 或
sudo ss -tulnp
# 检查异常连接
sudo lsof -i
# UFW防火墙管理
sudo ufw enable
sudo ufw allow 22/tcp
sudo ufw deny 3306/tcp
# 使用auditd进行系统审计
sudo auditctl -l # 查看当前审计规则
sudo auditctl -w /etc/passwd -p wa -k passwd_changes
# 使用lynis进行系统扫描
sudo lynis audit system
# 检查已知漏洞
sudo apt-get install debsecan # Debian/Ubuntu
debsecan
# 监控登录尝试
sudo tail -f /var/log/auth.log
# 监控系统调用
sudo strace -p <pid>
# 监控文件变化
sudo inotifywait -m -r /etc
# 分析失败登录
sudo grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr
# 检查可疑命令历史
sudo cat /home/*/.bash_history | grep -E "(wget|curl|chmod|chown|ssh|scp)"
定期更新:
sudo apt update && sudo apt upgrade -y # Debian/Ubuntu
sudo yum update -y # CentOS/RHEL
禁用root SSH登录:
sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
配置自动安全更新:
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
设置强密码策略:
sudo apt install libpam-pwquality
sudo nano /etc/security/pwquality.conf
# 设置 minlen=12, dcredit=-1, ucredit=-1, ocredit=-1, lcredit=-1
通过熟练掌握这些命令和工具,您可以显著提高Linux服务器的安全性,及时发现潜在威胁,并采取适当的防护措施。