什么是 ProxyJump

ProxyJump(-J)是 OpenSSH 7.3+ 引入的跳板机功能,简化了通过中间主机连接目标主机的流程。

1
本地 → 跳板机 → 目标主机

命令行用法

基本语法

1
ssh -J jumpuser@jumphost targetuser@targethost

示例

1
2
# 通过 jump.example.com 连接 target.example.com
ssh -J root@jump.example.com root@target.example.com

多跳跳板机

1
2
# 本地 → jump1 → jump2 → target
ssh -J user1@jump1,user2@jump2 user3@target

SSH Config 配置

编辑 ~/.ssh/config

单跳配置

1
2
3
4
Host target
    HostName target.example.com
    User root
    ProxyJump jump.example.com

然后直接连接:

1
ssh target

带认证的跳板机

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Host jump
    HostName jump.example.com
    User jumpuser
    IdentityFile ~/.ssh/jump_key

Host target
    HostName target.example.com
    User targetuser
    ProxyJump jump
    IdentityFile ~/.ssh/target_key

多跳配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Host jump1
    HostName jump1.example.com
    User user1

Host jump2
    HostName jump2.example.com
    User user2
    ProxyJump jump1

Host target
    HostName target.example.com
    User user3
    ProxyJump jump2

ProxyJump vs ProxyCommand

ProxyJump(推荐)

1
ssh -J jump.example.com target.example.com

配置文件:

1
2
Host target
    ProxyJump jump

ProxyCommand(传统方式)

1
ssh -o ProxyCommand="ssh -W %h:%p jump.example.com" target.example.com

配置文件:

1
2
Host target
    ProxyCommand ssh -W %h:%p jump.example.com

对比

特性 ProxyJump ProxyCommand
引入版本 OpenSSH 7.3+ 早期版本
语法简洁度 简洁 复杂
多跳支持 原生支持 需嵌套
配置可读性 较低

推荐使用 ProxyJump,除非旧版 OpenSSH 不支持。

SCP 和 SFTP 通过跳板机

SCP

1
scp -J jump.example.com file.txt target.example.com:/path/

SFTP

1
sftp -J jump.example.com target.example.com

使用 Config 配置

配置好 ~/.ssh/config 后,直接使用别名:

1
2
scp file.txt target:/path/
sftp target

端口转发与跳板机

本地端口转发

1
ssh -J jump.example.com -L 3306:localhost:3306 target.example.com

动态端口转发(SOCKS 代理)

1
ssh -J jump.example.com -D 1080 target.example.com

安全建议

1. 使用专用跳板机密钥

1
2
3
Host jump
    IdentityFile ~/.ssh/jump_ed25519
    IdentitiesOnly yes

2. 限制跳板机权限

跳板机用户应只允许端口转发:

1
2
3
4
5
6
# /etc/ssh/sshd_config on jump host
Match User jumpuser
    AllowTcpForwarding yes
    X11Forwarding no
    PermitTTY no
    ForceCommand none

3. 使用证书认证

1
2
# 签发跳板机证书
ssh-keygen -s ca_key -I jump_user -V +52w jump_user_key.pub

故障排查

连接超时

1
2
3
4
5
6
# 检查跳板机可达性
ping jump.example.com
ssh jump.example.com "echo OK"

# 详细输出
ssh -vJ jump.example.com target.example.com

认证失败

1
2
3
4
5
# 指定密钥
ssh -i ~/.ssh/my_key -J jump.example.com target.example.com

# 检查 config
ssh -G target | grep -i proxy

版本检查

1
2
ssh -V
# 需要 OpenSSH 7.3+ 才支持 -J

参考来源