SSH Local and Remote Port Forwarding
Understanding of forwarding
- 转发的是什么?
答:port forwarding 其中的 port 就已经指明了这一点,传输层的 port 标定了具体的 application,所以转发的实际是对于这个 application 的请求。
- 转发的介质是什么?
答:ssh tunnel。
通过上面两个问题,不难看出 ssh port forwarding 中涉及的两个重要主体就是 application 和 ssh,为什么端口转发会分为 local 和 remote 两大类,就需要从这两个主体之间的关系来考虑。
对于 application 而言,转发一定是从 application client 转发到 application server,反方向的转发显然是不会得到正确结果的无意义行为。
对于 ssh 而言,因为实际的通信过程是借助于 ssh 完成的,所以 local 指的是 ssh client, remote 指的是 sshd server。
而 application 的 client 与 server 和 ssh 的 client 与 server 之间并没有严格的对应关系,因此根据不同的对应关系也就有了 local forwarding 和 remote forwarding 两种转发方式。
根据 application 和 ssh 的 client/server 是否位于同一台 host,又可以细分为是否存在 bastion host 的多种情况,具体的分类规则如下图所示。
Local Port Forwarding
The application client is located on the side of the ssh client (application server is located on the side of the sshd server), i.e. we want to access the remote resource by the local port, ssh client need to start listening on the specify port.
|
|
We can divide local port forwarding into the following two categories based on whether remote_addr
and sshd_addr
are the same.
Without Bastion host
With Bastion host
Remote Port Forwarding
The application client is located on the side of the sshd server (application server is located on the side of the ssh client), i.e. we want to expose the local resource to the remote host, sshd server need to start listening on the specify port.
|
|
相较于本地端口转发,远程端口转发默认只能在 remote host’s localback 访问到端口,通过 /etc/ssh/sshd_config
中的 GatewayPorts
项 can edit this configuration.
Without Bastion host
需要注意上图配置了 GatewayPorts yes
所以才能实现 remote_addr
为0.0.0.0
时转发有效。