sudo apt update && sudo apt upgrade (Debian/Ubuntu) 或 sudo yum update (RHEL/CentOS)bash
sudo ufw enable
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp # 仅允许必要的端口PermitRootLogin no in /etc/ssh/sshd_configbash
ssh-keygen -t rsa -b 4096
ssh-copy-id user@serverPort 2222 in /etc/ssh/sshd_configAllowUsers user@192.168.1.* in /etc/ssh/sshd_configbash
sudo apt install fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2bansudo apt install libpam-pwquality 并配置/etc/security/pwquality.confsudo less /etc/passwd 检查异常账户sudo visudo 谨慎分配权限bash
find / -perm -4000 -type f -exec ls -la {} \; 2>/dev/null
find / -perm -2000 -type f -exec ls -la {} \; 2>/dev/nullbash
sudo chattr +i /etc/passwd /etc/shadow /etc/group /etc/sudoersbash
sudo apt install aide
sudo aideinit
sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db
sudo aide.wrapper --check/etc/rsyslog.conf并集中管理日志bash
sudo apt install ossec-hids-serverLimitExcept GET POST in Apache配置bash
chown -R www-data:www-data /var/www/html/
find /var/www/html/ -type d -exec chmod 750 {} \;
find /var/www/html/ -type f -exec chmod 640 {} \;/etc/hosts.allow和/etc/hosts.denyecho 1 > /proc/sys/net/ipv4/icmp_echo_ignore_allecho 1 > /proc/sys/net/ipv4/tcp_syncookiesps auxfnetstat -tulnp或ss -tulnpcrontab -l和ls /etc/cron.*bash
sudo apt install lynis
sudo lynis audit system/var/log/, last, history等)通过实施这些多层次的安全措施,可以显著提高Linux服务器的安全性,降低被攻击的风险。安全是一个持续的过程,需要定期审查和更新防护策略。