SSH配置文件概述
SSH配置文件允许用户自定义SSH客户端的行为,简化连接过程,提高工作效率。主要的配置文件包括:
- 用户配置:
~/.ssh/config
(用户级别) - 系统配置:
/etc/ssh/ssh_config
(系统级别) - 服务器配置:
/etc/ssh/sshd_config
(服务器端)
客户端配置文件结构
基本语法
# ~/.ssh/config
# 全局设置
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
# 特定主机配置
Host myserver
HostName 192.168.1.100
User admin
Port 2222
IdentityFile ~/.ssh/work_key
# 使用通配符
Host *.example.com
User developer
IdentityFile ~/.ssh/company_key
配置文件优先级
- 命令行参数(最高优先级)
- 用户配置文件
~/.ssh/config
- 系统配置文件
/etc/ssh/ssh_config
- 默认值(最低优先级)
常用配置选项
连接相关
Host production-server
HostName prod.example.com
User root
Port 22
# 连接超时设置
ConnectTimeout 10
# 保持连接活跃
ServerAliveInterval 60
ServerAliveCountMax 3
# 连接复用
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 600
认证相关
Host secure-server
HostName secure.example.com
User admin
# 指定私钥
IdentityFile ~/.ssh/secure_key
IdentitiesOnly yes
# 禁用密码认证
PasswordAuthentication no
# 启用公钥认证
PubkeyAuthentication yes
# 证书认证
CertificateFile ~/.ssh/user-cert.pub
安全相关
Host paranoid-server
HostName paranoid.example.com
# 严格主机密钥检查
StrictHostKeyChecking yes
# 指定已知主机文件
UserKnownHostsFile ~/.ssh/known_hosts
# 禁用X11转发
ForwardX11 no
# 禁用Agent转发
ForwardAgent no
# 指定加密算法
Ciphers aes256-gcm@openssh.com,aes128-gcm@openssh.com
# 指定MAC算法
MACs hmac-sha2-256,hmac-sha2-512
高级配置技巧
连接复用
# 全局启用连接复用
Host *
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 600
# 创建socket目录
mkdir -p ~/.ssh/sockets
跳板机配置
# 通过跳板机连接
Host target-server
HostName 10.0.1.100
User app
ProxyJump jumphost
Host jumphost
HostName jump.example.com
User admin
Port 2222
# 多级跳板
Host final-target
HostName 172.16.1.50
ProxyJump jumphost1,jumphost2
动态端口转发(SOCKS代理)
Host socks-proxy
HostName proxy.example.com
User proxy_user
DynamicForward 1080
# 使用方法
# ssh socks-proxy
# 然后配置浏览器使用 localhost:1080 作为SOCKS代理
本地端口转发
Host database-tunnel
HostName db-server.example.com
User dbadmin
LocalForward 3306 localhost:3306
# 使用方法
# ssh database-tunnel
# 然后连接 localhost:3306 即可访问远程数据库
远程端口转发
Host reverse-tunnel
HostName remote.example.com
User tunnel_user
RemoteForward 8080 localhost:80
# 远程服务器的8080端口会转发到本地的80端口
环境特定配置
开发环境
# 开发服务器配置
Host dev-*
User developer
IdentityFile ~/.ssh/dev_key
ForwardAgent yes
ForwardX11 yes
# 开发环境可以放宽安全设置
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
LogLevel QUIET
Host dev-web
HostName dev-web.company.com
LocalForward 3000 localhost:3000
LocalForward 8080 localhost:8080
Host dev-db
HostName dev-db.company.com
LocalForward 5432 localhost:5432
生产环境
# 生产服务器配置
Host prod-*
User admin
IdentityFile ~/.ssh/prod_key
# 生产环境严格安全设置
StrictHostKeyChecking yes
PasswordAuthentication no
ForwardAgent no
ForwardX11 no
# 连接保持
ServerAliveInterval 30
ServerAliveCountMax 5
Host prod-web
HostName prod-web.company.com
Port 2222
Host prod-db
HostName prod-db.company.com
Port 2222
# 生产数据库只允许特定操作
RequestTTY no
条件配置
基于匹配条件的配置
# 基于主机名匹配
Match host="*.internal.company.com"
User internal_user
IdentityFile ~/.ssh/internal_key
# 基于用户匹配
Match user="admin"
ForwardAgent yes
# 基于地址匹配
Match host="192.168.*"
StrictHostKeyChecking no
# 组合条件
Match host="*.prod.company.com" user="admin"
CertificateFile ~/.ssh/admin-cert.pub
配置文件管理
模块化配置
# ~/.ssh/config
# 包含其他配置文件
Include ~/.ssh/config.d/*
# ~/.ssh/config.d/work.conf
Host work-*
User employee
IdentityFile ~/.ssh/work_key
# ~/.ssh/config.d/personal.conf
Host personal-*
User personal
IdentityFile ~/.ssh/personal_key
配置验证
# 检查配置文件语法
ssh -F ~/.ssh/config -T test-host
# 显示最终配置
ssh -F ~/.ssh/config -G hostname
# 测试连接(不执行命令)
ssh -T git@github.com
服务器端配置要点
基本安全配置
# /etc/ssh/sshd_config
# 禁用root直接登录
PermitRootLogin no
# 禁用密码认证(仅使用密钥)
PasswordAuthentication no
ChallengeResponseAuthentication no
# 限制用户和组
AllowUsers admin developer
AllowGroups ssh-users
# 修改默认端口
Port 2222
# 限制协议版本
Protocol 2
# 配置空闲超时
ClientAliveInterval 300
ClientAliveCountMax 2
重启SSH服务
# 检查配置文件语法
sudo sshd -t
# 重启SSH服务
sudo systemctl restart sshd
# 或者
sudo service ssh restart
故障排除
配置调试
# 详细输出配置解析过程
ssh -vvv -F ~/.ssh/config hostname
# 显示最终生效的配置
ssh -G hostname
# 测试特定配置文件
ssh -F /path/to/config hostname
常见问题
- 配置不生效:检查语法和优先级
- 权限问题:确保配置文件权限正确(600)
- 路径问题:使用绝对路径或正确的相对路径
- 通配符匹配:注意匹配顺序和规则
小结
合理配置SSH客户端可以大大提高工作效率和安全性。通过使用配置文件,可以: - 简化复杂的连接命令 - 标准化不同环境的连接方式 - 提高连接的安全性和稳定性 - 实现高级功能如端口转发和连接复用
建议根据实际需求,分环境、分用途地组织配置文件,并定期审查和更新配置。