SSH(Secure Shell)是保护CentOS服务器远程访问的行业标准协议。以下是配置安全SSH访问的详细步骤:
sudo yum install openssh-server openssh-clients
sudo systemctl enable sshd
sudo systemctl start sshd
编辑/etc/ssh/sshd_config
文件:
sudo vi /etc/ssh/sshd_config
Port 2222 # 更改默认端口(22)
Protocol 2 # 仅使用SSH协议v2
PermitRootLogin no # 禁止root直接登录
MaxAuthTries 3 # 最大认证尝试次数
LoginGraceTime 1m # 登录宽限时间
AllowUsers user1 user2 # 只允许特定用户登录
AllowGroups sshusers # 只允许特定用户组登录
DenyUsers baduser # 明确拒绝某些用户
DenyGroups badgroup # 明确拒绝某些用户组
PasswordAuthentication no # 禁用密码认证,强制使用密钥
PubkeyAuthentication yes # 启用公钥认证
PermitEmptyPasswords no # 禁止空密码
X11Forwarding no # 禁用X11转发(除非需要)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 或使用RSA(兼容性更好)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 user@your_server_ip
sudo yum install google-authenticator
google-authenticator
然后在sshd_config
中添加:
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
sudo yum install epel-release
sudo yum install fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
配置Fail2Ban(创建/etc/fail2ban/jail.local
):
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/secure
maxretry = 3
bantime = 1h
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload
sudo systemctl restart sshd
测试连接(从另一终端):
ssh -p 2222 user@your_server_ip
sudo yum update openssh*
sudo tail -f /var/log/secure
通过以上配置,您的CentOS服务器SSH访问将具备企业级安全性。