# 禁用root直接登录
sudo sed -i 's/^PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
# 限制协议版本为SSHv2
echo "Protocol 2" | sudo tee -a /etc/ssh/sshd_config
# 禁用密码认证,强制使用密钥
sudo sed -i 's/^#PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo sed -i 's/^PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
# 更改默认端口(可选)
sudo sed -i 's/^#Port 22/Port 2222/' /etc/ssh/sshd_config
# 重启SSH服务
sudo systemctl restart sshd
# 设置登录尝试限制
echo "MaxAuthTries 3" | sudo tee -a /etc/ssh/sshd_config
echo "LoginGraceTime 1m" | sudo tee -a /etc/ssh/sshd_config
# 配置空闲会话超时
echo "ClientAliveInterval 300" | sudo tee -a /etc/ssh/sshd_config
echo "ClientAliveCountMax 0" | sudo tee -a /etc/ssh/sshd_config
# 限制用户和组访问
echo "AllowUsers admin1 admin2" | sudo tee -a /etc/ssh/sshd_config
# 或
echo "AllowGroups ssh-users" | sudo tee -a /etc/ssh/sshd_config
# 启用两步验证(需要Google Authenticator)
sudo apt install libpam-google-authenticator
echo "auth required pam_google_authenticator.so" | sudo tee -a /etc/pam.d/sshd
echo "ChallengeResponseAuthentication yes" | sudo tee -a /etc/ssh/sshd_config
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2222/tcp # 替换为您设置的SSH端口
sudo ufw enable
# 清除现有规则
sudo iptables -F
# 默认策略
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
# 允许已建立的连接
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# 允许本地回环
sudo iptables -A INPUT -i lo -j ACCEPT
# 允许SSH(替换为您的端口)
sudo iptables -A INPUT -p tcp --dport 2222 -j ACCEPT
# 防止SSH暴力破解
sudo iptables -A INPUT -p tcp --dport 2222 -m recent --name ssh --update --seconds 60 --hitcount 3 -j DROP
sudo iptables -A INPUT -p tcp --dport 2222 -m recent --name ssh --set -j ACCEPT
# 保存规则(Ubuntu)
sudo apt install iptables-persistent
sudo netfilter-persistent save
# 安装
sudo apt install wireguard
# 生成密钥
wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
# 创建配置文件 /etc/wireguard/wg0.conf
[Interface]
PrivateKey = <服务器私钥>
Address = 10.0.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# 启动服务
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
sudo apt install fail2ban
# 创建本地配置文件 /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 86400
# 安装并配置logwatch
sudo apt install logwatch
sudo cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf/
# 编辑/etc/logwatch/conf/logwatch.conf
Output = mail
Format = html
MailTo = your@email.com
Range = yesterday
Detail = High
定期更新系统:
sudo apt update && sudo apt upgrade -y
禁用不必要的服务:
sudo systemctl list-unit-files --type=service | grep enabled
sudo systemctl disable <不需要的服务>
使用SSH证书而非密码:
# 客户端生成密钥
ssh-keygen -t ed25519
# 上传公钥到服务器
ssh-copy-id -p 2222 user@your-server
定期审计用户和权限:
sudo less /etc/passwd
sudo less /etc/shadow
sudo awk -F: '($3 == "0") {print}' /etc/passwd
实施文件完整性监控:
sudo apt install aide
sudo aideinit
sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db
# 定期检查
sudo aide --check
通过实施这些措施,您可以显著提高Linux服务器的远程访问安全性,降低被攻击的风险。