目标
链式 SSH 命令会诱发自动化捷径,而最糟的捷径是把共享跳板私钥拷进笔记本。ProxyJump 把跳板写进配置、每主机 IdentityFile 隔离凭证、ControlMaster 把重复连接变成“十分钟认证一次”的套接字复用。结果比手动两跳更快,也比它取代的野路子更安全。
步骤要点
- 在 ~/.ssh/config 里为 bastion 与 int-* 分别声明主机、用户与独立密钥,int-* 挂 ProxyJump bastion。
- 加 ControlMaster auto + ControlPath + ControlPersist 10m,首连认证后十分钟内 ssh/scp/rsync 全走复用套接字。
- time 两次 ssh int-db01 true 对比:第二次应在几十毫秒完成而非完整握手。
- 在跳板 authorized_keys 里给公钥加 restrict 前缀:只允许转发到白名单目标,不给 shell。
- ssh -G 解析生效配置并核对 identityfile,确认没有任何共享私钥散落笔记本。
排错提示
网络切换后复用会话卡死是旧 control socket 残留,用 ssh -O exit 清理或调低 ControlPersist;交互可用而脚本失败是批处理模式在等密码输入,加载 agent 密钥并加 BatchMode yes 快速失败;跳板拒绝端口转发说明 PermitOpen 受限,应精确追加目标端口而不是全开。
适用边界
复用共享一条 TCP 连接,网络路径重置会同时断掉该套接字上的所有会话;跳板可用性从此等于全部内部访问的可用性,要按生产标准监控。
代码示例
Host bastion
HostName bastion.example.com
IdentityFile ~/.ssh/id_ed25519
Host int-*
ProxyJump bastion
IdentityFile ~/.ssh/id_ed25519_internal
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 10m