Blog
Blog copied to clipboard
ProxyCommand
ProxyCommand是 OpenSSH 的特性。
使用 man ssh_config
来查看文档说明。实际上,完整版的命令应该是 man 5 ssh_config
。为啥有那个 5,请使用 man man
来查看。
打开文档之后可以发现 ssh_config 的描述是 OpenSSH SSH client 的配置文件描述文档。
ProxyCommand 是其中的一个配置项。作用是指定连接到 Server 的时候要执行的命令。这个命令会接收 TOKENS 章节中描述的参数,并将其展开替换为对应的值。
ProxyCommand 后面可以执行任何命令。这个命令需要满足以下要求:
从标准输入流中读取数据,写入到标准输出流中。
当在 .ssh/config 文件中对 Github.com 设置了 ProxyCommand 为 nc -X 5 -x 127.0.0.1:1086 %h %p
后,执行 ssh -T github.com -v
可以在输出的 Log 中看到这样一行:
debug1: Executing proxy command: exec nc -X 5 -x 127.0.0.1:1086 github.com 22
上面的命令表示 nc 使用 socks5 协议作为代理协议。
socks5 协议的大致工作流程如下:
| Client | Proxy | Server |
Client → Proxy 请求建立连接(可能包含认证)
Proxy → Client 应答 Client 的请求 (返回服务端选择的认证方法)
Client ←→ Proxy 执行认证
Client → Proxy 通知 Proxy,Client 想要连接哪个 Server 及端口
Proxy → Server 建立对应的连接(TCP)
Proxy → Client 通知 Client,Proxy 与 Server 的连接状态,比如连接成功
Client → Proxy → Server 发送正常请求,Proxy 此时只做转发功能
所以上面在执行 nc 命令的时候需要给出要连接 github.com:22
所以在配置了 ProxyCommand 之后数据是这么走的
SSH → nc → 127.0.0.1:1086 → RemoteProxyServer → github.com:22
给自己的总结
要学会使用 man
命令查看对应文档。
man
命令不止可以看 command 怎么使用,还可以看到某个配置文件的内容分别代表什么意思。
man man
可以看到 manual sections。可以看到 Section 5 包含了 File Formats and Conventions.