ssh ProxyJump
通过跳板机(堡垒机)连接内网服务器,无需在跳板机上存储密钥
语法
ssh -J jump_host [user@]target_host
参数
| 参数 | 说明 | 示例 | 级别 |
|---|---|---|---|
-J ProxyJump |
指定跳板机 | ssh -J bastion user@internal |
常用 |
-W |
标准输入输出转发(ProxyCommand 用) | ssh -o ProxyCommand="ssh -W %h:%p bastion" user@internal |
进阶 |
ProxyJump |
配置文件中的跳板机设置 | ProxyJump user@bastion:22 |
常用 |
多级跳转 |
通过多个跳板机连接 | ssh -J bastion1,bastion2 user@target |
进阶 |
示例
通过跳板机连接内网
ssh -J user@bastion.example.com admin@10.0.0.50
通过公网跳板机访问内网服务器
多级跳转
ssh -J bastion1,bastion2 user@final-target
依次经过多个跳板机到达目标
配置文件中设置跳板机
Host internal-* ProxyJump bastion User admin Host bastion HostName bastion.example.com User jump-user
ssh internal-db 自动通过 bastion 跳转
通过跳板机传文件
scp -o ProxyJump=user@bastion file.txt admin@10.0.0.50:/tmp/
scp 也支持通过跳板机传输文件
常见错误
ssh: Could not resolve hostname bastion
跳板机主机名无法解析,使用 IP 或在 /etc/hosts 中添加解析
channel 0: open failed: administratively prohibited
跳板机不允许 TCP 转发,联系管理员开启 AllowTcpForwarding
技巧
- ProxyJump(-J)是 OpenSSH 7.3+ 的新特性,比旧的 ProxyCommand 更简洁
- 跳板机上不需要存储目标服务器的密钥,密钥验证在本地完成
- 在 config 中配置后,scp/sftp/rsync 等工具也能自动通过跳板机