SSH 配置文件配置指南¶
本指南说明如何配置 SSH 配置文件(~/.ssh/config)以便与 SSH 隧道管理器配合使用。SSH 配置文件对于定义连接参数和确保隧道顺利建立至关重要。
概述¶
SSH配置文件(~/.ssh/config)允许您为SSH主机定义连接参数,包括:
- 主机别名和真实主机名
- 每个主机的用户名
- SSH端口号
- 私钥文件
- 连接选项和超时设置
- 代理配置
本项目严重依赖SSH配置文件,因为:
- 主机识别:
config.yaml中的remote_host参数引用您SSH配置中的条目 - 身份验证:SSH配置指定每个主机使用哪些私钥
- 连接参数:超时、端口和其他连接设置在此定义
- 简化配置:无需在每个隧道中指定完整的连接详细信息,您可以使用简单的主机别名
SSH配置文件位置¶
SSH配置文件应位于:
如果此文件不存在,请创建它:
基础配置¶
简单主机配置¶
多个主机¶
Host server1
HostName 192.168.1.100
User admin
Port 22
IdentityFile ~/.ssh/id_rsa
Host server2
HostName server2.example.com
User root
Port 2222
IdentityFile ~/.ssh/id_ed25519
Host jumphost
HostName jump.example.com
User jumpuser
Port 22
IdentityFile ~/.ssh/jump_key
高级配置¶
连接优化¶
Host *
# 启用连接复用
ControlMaster auto
ControlPath ~/.ssh/sockets/ssh_mux_%h_%p_%r
ControlPersist 600
# 连接超时设置
ServerAliveInterval 60
ServerAliveCountMax 3
ConnectTimeout 10
# 安全设置
StrictHostKeyChecking yes
UserKnownHostsFile ~/.ssh/known_hosts
Host production-server
HostName prod.example.com
User deploy
Port 22
IdentityFile ~/.ssh/production_key
# 此主机的特定设置
ServerAliveInterval 30
TCPKeepAlive yes
Compression yes
跳板机配置¶
Host jumphost
HostName jump.example.com
User jumpuser
Port 22
IdentityFile ~/.ssh/jump_key
Host internal-server
HostName 10.0.1.100
User admin
Port 22
IdentityFile ~/.ssh/internal_key
ProxyJump jumphost
# 旧版SSH的替代语法
# ProxyCommand ssh -W %h:%p jumphost
通配符模式¶
Host *.internal
User admin
Port 22
IdentityFile ~/.ssh/internal_key
ProxyJump jumphost
Host dev-*
User developer
Port 2222
IdentityFile ~/.ssh/dev_key
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
常见配置示例¶
示例1:简单VPS配置¶
Host vps1
HostName 203.0.113.10
User root
Port 22
IdentityFile ~/.ssh/vps1_key
ServerAliveInterval 60
ServerAliveCountMax 3
对应的config.yaml条目:
示例2:带跳板机的企业环境¶
Host corporate-jump
HostName jump.company.com
User myusername
Port 22
IdentityFile ~/.ssh/company_key
Host internal-db
HostName db.internal.company.com
User dbuser
Port 22
IdentityFile ~/.ssh/db_key
ProxyJump corporate-jump
对应的config.yaml条目:
示例3:多环境配置¶
Host dev-server
HostName dev.example.com
User developer
Port 2222
IdentityFile ~/.ssh/dev_key
Host staging-server
HostName staging.example.com
User deploy
Port 22
IdentityFile ~/.ssh/staging_key
Host prod-server
HostName prod.example.com
User deploy
Port 22
IdentityFile ~/.ssh/prod_key
StrictHostKeyChecking yes
安全最佳实践¶
1. 文件权限¶
确保SSH文件具有正确的权限:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_*
chmod 644 ~/.ssh/id_*.pub
chmod 600 ~/.ssh/known_hosts
2. 密钥管理¶
Host *
# 仅使用配置中指定的密钥
IdentitiesOnly yes
# 禁用密码认证
PasswordAuthentication no
PubkeyAuthentication yes
# 使用强加密算法
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
3. 主机验证¶
Host trusted-servers
HostName *.trusted.com
StrictHostKeyChecking yes
UserKnownHostsFile ~/.ssh/known_hosts
Host dev-*
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
LogLevel QUIET
故障排除¶
常见问题¶
- 权限被拒绝
- 主机密钥验证失败
- 连接超时
测试SSH配置¶
在使用autossh之前测试您的SSH配置:
调试模式¶
在SSH配置中启用调试模式:
与Autossh隧道集成¶
在autossh-tunnel项目中使用此SSH配置时:
- 主机引用:使用SSH配置中的
Host名称作为config.yaml中的remote_host值 - 身份验证:确保
IdentityFile路径正确且可从Docker容器内访问 - 权限:
~/.ssh目录在容器中以只读方式挂载 - 测试:在配置隧道之前始终手动测试SSH连接
集成示例¶
SSH配置(~/.ssh/config):
Host tunnel-server
HostName tunnel.example.com
User tunneluser
Port 22
IdentityFile ~/.ssh/tunnel_key
ServerAliveInterval 60
ServerAliveCountMax 3
隧道配置(config/config.yaml):