SSH 配置文件详解完全指南
SSH 的配置文件是实现安全访问和个性化设置的核心。本文详细解析 SSH 客户端和服务端的配置文件,帮助您全面掌握 SSH 的配置技巧。
SSH 配置文件概述
SSH 有两个主要的配置文件:
- 客户端配置:~/.ssh/config
- 服务端配置:/etc/ssh/sshd_config
SSH 服务端配置 (sshd_config)
1. 基本连接配置
# 端口设置(默认22,建议修改为高位端口)
Port 22
# 监听地址(0.0.0.0 监听所有网卡)
ListenAddress 0.0.0.0
# 协议版本(建议使用SSH2)
Protocol 2
2. 认证配置
# 启用公钥认证
PubkeyAuthentication yes
# 公钥文件位置
AuthorizedKeysFile .ssh/authorized_keys
# 禁用密码认证(推荐生产环境使用)
PasswordAuthentication no
# 禁用空密码登录
PermitEmptyPasswords no
# 禁止root用户通过SSH登录
PermitRootLogin no
3. 安全超时配置
# 空闲超时时间(秒)
ClientAliveInterval 300
ClientAliveCountMax 2
# 登录宽限期(秒)
LoginGraceTime 60
4. 日志配置
# 日志级别(VERBOSE级别记录更详细)
LogLevel VERBOSE
5. 限制用户/组登录
# 只允许特定用户
AllowUsers user1 user2
# 只允许特定组
AllowGroups sshusers
# 拒绝特定用户
DenyUsers baduser
# 拒绝特定组
DenyGroups badgroup
6. 高级安全配置
# 禁用基于主机的认证
HostbasedAuthentication no
# 启用严格检查(严格检查主机密钥)
StrictModes yes
# 禁用用户环境文件
PermitUserEnvironment no
# 禁用TCP转发
AllowTcpForwarding no
# 禁用X11转发
X11Forwarding no
# 禁用-agent转发
AllowAgentForwarding no
7.Banner和警告信息
# 登录前显示的警告Banner
Banner /etc/ssh/banner
SSH 客户端配置 (config)
1. 基本配置语法
Host alias_name
HostName 实际服务器地址
User 用户名
Port 端口
IdentityFile 私钥路径
2. 常用配置示例
# 默认配置
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
Compression yes
# 服务器1
Host server1
HostName 192.168.1.100
User ubuntu
Port 22
IdentityFile ~/.ssh/server1_key
# 服务器2(使用别名)
Host prod-server
HostName example.com
User deploy
Port 2222
IdentityFile ~/.ssh/prod_key
# GitHub 配置
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_key
3. 常用客户端选项
# 保持连接
ServerAliveInterval 60
ServerAliveCountMax 3
# 端口转发
LocalForward 8080 localhost:3000
RemoteForward 9000 localhost:8000
# 代理配置
ProxyCommand ssh -W %h:%p jump-server
ProxyJump jump-server
# 压缩
Compression yes
# 密钥文件
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
# 禁用主机密钥检查(测试环境使用)
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
SSH 密钥配置
1. 密钥类型选择
- Ed25519(推荐):现代算法,安全且快速
- RSA(4096位):兼容性最好
- ECDSA:中等兼容性
- DSA:已废弃,不推荐使用
2. 生成密钥
# Ed25519(推荐)
ssh-keygen -t ed25519 -C "your_email@example.com"
# RSA 4096位
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 指定文件名
ssh-keygen -t ed25519 -f ~/.ssh/my_key -C "comment"
3. 密钥权限设置
# SSH目录
chmod 700 ~/.ssh
# 私钥文件
chmod 600 ~/.ssh/id_rsa
# 公钥文件
chmod 644 ~/.ssh/id_rsa.pub
# authorized_keys文件
chmod 600 ~/.ssh/authorized_keys
# config文件
chmod 600 ~/.ssh/config
SSH 安全性最佳实践
1. 修改默认端口
Port 2222
2. 使用密钥认证
PubkeyAuthentication yes
PasswordAuthentication no
3. 限制登录用户
AllowUsers username1 username2
4. 启用防火墙规则
sudo ufw allow 22/tcp
# 或自定义端口
sudo ufw allow 2222/tcp
5. 使用fail2ban防止暴力破解
sudo apt install fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
6. 双因素认证(2FA)
安装Google Authenticator PAM模块实现双因素认证。
7. 查看SSH连接日志
# 查看认证日志
sudo tail -f /var/log/auth.log | grep SSH
# 查看失败登录尝试
grep "Failed password" /var/log/auth.log
常见问题排查
1. 配置文件修改后不生效
# 检查配置语法
sudo sshd -t
# 重启服务
sudo systemctl restart ssh
2. 密钥登录失败
# 检查权限
ls -la ~/.ssh/
# 检查SSH服务日志
sudo journalctl -u ssh -f
3. 连接被拒绝
# 检查服务状态
sudo systemctl status ssh
# 检查端口监听
sudo netstat -tlnp | grep ssh
总结
掌握 SSH 配置文件是 Linux 系统管理员的必备技能。通过合理配置,您可以:
- 提高SSH连接的安全性
- 简化日常连接操作
- 实现更细粒度的访问控制
- 防止未授权访问
建议生产环境遵循以下原则:
– 使用密钥认证代替密码
– 修改默认端口
– 限制登录用户
– 启用日志审计
– 定期更新SSH版本
祝您配置顺利!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END












暂无评论内容