# 定期更新系统软件包
sudo apt update && sudo apt upgrade -y # Debian/Ubuntu
sudo yum update -y # CentOS/RHEL
sudo dnf upgrade -y # Fedora
# 配置自动安全更新
sudo apt install unattended-upgrades # Debian/Ubuntu
sudo dpkg-reconfigure unattended-upgrades
# 创建新用户并设置强密码
sudo adduser newuser
sudo passwd newuser
# 禁用root SSH登录
sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
# 设置sudo权限
sudo visudo
# 添加: newuser ALL=(ALL:ALL) ALL
# 修改SSH默认端口
sudo sed -i 's/#Port 22/Port 2222/' /etc/ssh/sshd_config
# 禁用密码认证,启用密钥认证
sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config
# 限制SSH访问IP
echo "sshd: 192.168.1.100" | sudo tee -a /etc/hosts.allow
echo "sshd: ALL" | sudo tee -a /etc/hosts.deny
sudo systemctl restart sshd
# 使用UFW (Ubuntu)
sudo ufw enable
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2222/tcp # SSH新端口
# 使用firewalld (CentOS/RHEL)
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload
# 检查敏感文件权限
sudo chmod 600 /etc/shadow
sudo chmod 644 /etc/passwd
sudo chmod 600 /home/*/.ssh/authorized_keys
# 禁用不必要的SUID/SGID程序
sudo find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -l {} \;
# 启用auditd服务
sudo systemctl start auditd
sudo systemctl enable auditd
# 监控重要文件更改
sudo auditctl -w /etc/passwd -p wa -k passwd_changes
sudo auditctl -w /etc/shadow -p wa -k shadow_changes
# 配置日志轮转
sudo nano /etc/logrotate.conf
# 编辑sysctl.conf
sudo nano /etc/sysctl.conf
# 添加以下内容:
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.default.rp_filter=1
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.conf.all.accept_redirects=0
net.ipv6.conf.all.accept_redirects=0
net.ipv4.tcp_syncookies=1
kernel.exec-shield=1
kernel.randomize_va_space=2
# 应用配置
sudo sysctl -p
# 安装fail2ban防止暴力破解
sudo apt install fail2ban -y # Debian/Ubuntu
sudo yum install fail2ban -y # CentOS/RHEL
# 配置fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
sudo systemctl restart fail2ban
# 安装rootkit检测工具
sudo apt install rkhunter chkrootkit -y
sudo rkhunter --update
sudo rkhunter --propupd
sudo rkhunter --check
# 查看所有运行的服务
sudo systemctl list-units --type=service --state=running
# 禁用不必要的服务
sudo systemctl stop servicename
sudo systemctl disable servicename
# 使用netstat查看开放端口
sudo netstat -tulnp
# 使用Lynis进行安全审计
sudo apt install lynis -y
sudo lynis audit system
# 使用OpenVAS或Nessus进行漏洞扫描
# 需要单独安装配置
通过实施这些措施,您可以显著提高Linux服务器的安全性,降低被攻击的风险。