作为IT工程师,通过命令行工具强化Linux服务器安全是日常运维的关键任务。以下是一套全面的安全增强实践方案:
# 定期更新系统
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 useradd -m -s /bin/bash username
sudo passwd username # 会提示输入密码
# 禁用root SSH登录
sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
# 使用sudo替代root
sudo visudo # 添加: username 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.0/24" | sudo tee -a /etc/hosts.allow
echo "sshd: ALL" | sudo tee -a /etc/hosts.deny
# 重启SSH服务
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新端口
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
# 使用firewalld (CentOS/RHEL)
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
# 安装配置fail2ban
sudo apt install fail2ban -y
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo systemctl enable fail2ban && sudo systemctl start fail2ban
# 配置日志监控
sudo apt install logwatch -y
sudo logwatch --detail High --mailto admin@example.com --range today
# 安装rkhunter进行rootkit检测
sudo apt install rkhunter -y
sudo rkhunter --update
sudo rkhunter --propupd
sudo rkhunter --check
# 检查文件权限
find / -type f -perm -o+w -exec ls -l {} \; # 查找全局可写文件
find / -type d -perm -o+w -exec ls -ld {} \; # 查找全局可写目录
# 设置敏感文件权限
sudo chmod 600 /etc/shadow
sudo chmod 644 /etc/passwd
sudo chmod 600 /home/*/.ssh/authorized_keys
# 安装aide进行文件完整性检查
sudo apt install aide -y
sudo aideinit
sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db
sudo aide --check
# 查看开放端口
sudo netstat -tulnp
sudo ss -tulnp
# 禁用不必要的服务
sudo systemctl list-unit-files --type=service | grep enabled
sudo systemctl disable <unneeded-service>
# 使用TCP Wrappers限制访问
echo "ALL: ALL" | sudo tee -a /etc/hosts.deny
echo "sshd: 192.168.1.0/24" | sudo tee -a /etc/hosts.allow
# 使用Lynis进行安全审计
sudo apt install lynis -y
sudo lynis audit system
# 使用ClamAV进行病毒扫描
sudo apt install clamav clamav-daemon -y
sudo freshclam # 更新病毒库
sudo clamscan -r /home # 扫描用户目录
# 检查SUID/SGID文件
find / -perm -4000 -type f -exec ls -l {} \;
find / -perm -2000 -type f -exec ls -l {} \;
# 编辑sysctl.conf加固内核
sudo nano /etc/sysctl.conf
# 添加以下内容:
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.default.rp_filter=1
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.icmp_ignore_bogus_error_responses=1
kernel.exec-shield=1
kernel.randomize_va_space=2
# 应用配置
sudo sysctl -p
# 创建重要文件备份
sudo tar -czvf /backup/etc_backup_$(date +%F).tar.gz /etc
# 设置定时备份 (添加到crontab)
0 2 * * * tar -czvf /backup/full_backup_$(date +\%F).tar.gz /etc /home /var/www
通过实施这些命令行安全措施,您可以显著提高Linux服务器的安全性。建议定期执行安全审计,并根据最新的安全威胁调整防护策略。