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_config
bash
ssh-keygen -t rsa -b 4096
ssh-copy-id user@server
Port 2222
in /etc/ssh/sshd_config
AllowUsers user@192.168.1.*
in /etc/ssh/sshd_config
bash
sudo apt install fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
sudo apt install libpam-pwquality
并配置/etc/security/pwquality.conf
sudo 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/null
bash
sudo chattr +i /etc/passwd /etc/shadow /etc/group /etc/sudoers
bash
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-server
LimitExcept 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.deny
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
ps auxf
netstat -tulnp
或ss -tulnp
crontab -l
和ls /etc/cron.*
bash
sudo apt install lynis
sudo lynis audit system
/var/log/
, last
, history
等)通过实施这些多层次的安全措施,可以显著提高Linux服务器的安全性,降低被攻击的风险。安全是一个持续的过程,需要定期审查和更新防护策略。