默认配置/etc/ssh/sshd_config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| Include /etc/ssh/sshd_config.d/*.conf
AddressFamily any
PermitRootLogin prohibit-password
KbdInteractiveAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
UseDNS no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
PasswordAuthentication yes
PermitEmptyPasswords no
|
安全升级
修改端口号
禁用密码登录,启用密钥认证
1 2 3 4
| PasswordAuthentication no
PubkeyAuthentication yes
|
生成SSH密钥对
1 2 3 4
| ssh-keygen -t rsa -b 4096 -C "zhiqiang2033@gmail.com"
ssh-copy-id -p 2022 user@server_ip
|
配置Fail2ban动态防护
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| sudo apt update && sudo apt install fail2ban -y
cat > /etc/fail2ban/jail.local << EOF [sshd] enabled = true # 启用 SSH 防护规则 port = ssh # 监听的端口(默认为 22) filter = sshd # 使用默认的 sshd 过滤器 logpath = /var/log/auth.log # 登录失败日志路径(Ubuntu 系统) maxretry = 5 # 允许的最大失败次数 findtime = 600 # 时间窗口:10 分钟内超过 maxretry 次则封禁 bantime = 3600 # 封禁时间:单位为秒,这里是 1 小时 # ignoreip = 127.0.0.1/8 # 忽略本地回环 IP,不对其封禁(白名单) EOF
|
查看封禁状态
1
| sudo fail2ban-client status sshd
|
手动解封IP
1
| sudo fail2ban-client set sshd unbanip 192.168.0.100
|
手动封禁IP
1
| sudo fail2ban-client set sshd banip 192.168.0.100
|
查看封禁日志
1
| sudo fail2ban-client get sshd banip
|
重启 Fail2ban 服务
1
| sudo systemctl restart fail2ban
|
启用双因子认证(2FA)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| sudo apt update && sudo apt install libpam-google-authenticator -y
google-authenticator 1.按提示回答问题(建议全部选择“是”以增强安全性) 2.会生成一个二维码和一串密钥,扫码到手机 Google Authenticator 或其他兼容 App 3.记录好备用密钥,用于恢复
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
auth required pam_google_authenticator.so nullok
|
最佳的配置/etc/ssh/sshd_config
这份 SSH 配置禁用密码和 root 登录,只允许公钥认证,改用非默认端口,限制登录尝试次数和会话数,关闭了 X11 和 TCP 转发,防止暴力破解和未授权访问,整体安全且实用,适合严格控制服务器访问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| Include /etc/ssh/sshd_config.d/*.conf
AddressFamily any
KbdInteractiveAuthentication no
UsePAM yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
Port 2022
HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key
PermitRootLogin no
PasswordAuthentication no
PermitEmptyPasswords no
PubkeyAuthentication yes
AuthenticationMethods publickey
MaxAuthTries 3
MaxSessions 10
MaxStartups 3:30:10
LoginGraceTime 1m
ClientAliveInterval 300
ClientAliveCountMax 2
UseDNS no
AllowUsers ubuntu
DenyUsers root guest nobody
X11Forwarding no
AllowTcpForwarding no
GatewayPorts no
PermitTunnel no
|
创建ubuntu账号
1 2
| useradd -m -d /home/ubuntu -s /bin/bash ubuntu echo "ubuntu:123456" | chpasswd
|