# 安装密码策略模块
sudo apt install libpam-pwquality # Debian/Ubuntu
sudo yum install pam_pwquality # CentOS/RHEL
# 配置密码复杂度要求
sudo nano /etc/security/pwquality.conf
配置示例:
minlen = 12
minclass = 4 # 要求包含4种字符类型(小写、大写、数字、特殊字符)
maxrepeat = 3
# 编辑SSH配置
sudo nano /etc/ssh/sshd_config
修改为:
PermitRootLogin no
# 安装Google Authenticator
sudo apt install libpam-google-authenticator # Debian/Ubuntu
sudo yum install google-authenticator # CentOS/RHEL
# 为用户配置
google-authenticator
sudo nano /etc/pam.d/sshd
添加:
auth required pam_google_authenticator.so
ssh-keygen -t ed25519 -a 100 # 推荐使用Ed25519算法
# 或
ssh-keygen -t rsa -b 4096 # 传统RSA算法
sudo nano /etc/ssh/sshd_config
确保以下设置:
PubkeyAuthentication yes
PasswordAuthentication no
ChallengeResponseAuthentication yes
# 安装Fail2Ban
sudo apt install fail2ban # Debian/Ubuntu
sudo yum install fail2ban # CentOS/RHEL
# 复制配置文件
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# 自定义配置
sudo nano /etc/fail2ban/jail.local
sudo nano /etc/hosts.allow
添加:
sshd: 192.168.1.*, 203.0.113.5
# 查看最近的SSH登录
sudo grep "sshd" /var/log/auth.log | grep "Accepted"
# 或CentOS/RHEL
sudo grep "sshd" /var/log/secure | grep "Accepted"
# 查看失败的登录尝试
sudo grep "Failed password" /var/log/auth.log
w
who
last
# Debian/Ubuntu
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
# CentOS/RHEL
sudo yum install yum-cron
sudo systemctl enable yum-cron
sudo systemctl start yum-cron
通过实施这些措施,您可以显著提高Linux服务器的安全性,减少未经授权访问的风险。定期审查和更新这些配置是保持系统安全的关键。