Skip to content

Commit ea6a947

Browse files
committed
docs(port-forwarding): modify local forwarding
1 parent 9e8e09e commit ea6a947

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

docs/client.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ $ ssh -F /usr/local/ssh/other_config
281281
$ ssh -i my-key server.example.com
282282
```
283283
284+
**-J**
285+
286+
`-J`指定跳转服务器。假定本地无法直接与 SSH 服务器通信,就可以通过`—J`指定跳转服务器。
287+
288+
```bash
289+
$ ssh -J root@J1,root@J2 root@S1
290+
```
291+
292+
上面示例中,本机先通过 J1,再通过 J2,登陆到 S1 服务器。
293+
284294
**-l**
285295
286296
`-l`参数指定远程登录的账户名。

docs/port-forwarding.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,21 @@ DynamicForward tunnel-host:local-port
4848

4949
## 本地转发
5050

51-
本地转发(local forwarding)指的是,SSH 服务器作为中介的跳板机,建立本地计算机与特定目标网站之间的加密连接。本地转发是在本地计算机的 SSH 客户端建立的转发规则
51+
本地转发(local forwarding)指的是,创建一个本地端口,将发往该端口的所有通信都通过 SSH 服务器,转发到指定的远程服务器的端口。这种情况下,SSH 服务器只是一个作为跳板的中介,用于连接本地计算机无法直接连接的远程服务器。本地转发是在本地计算机建立的转发规则
5252

53-
它会指定一个本地端口(local-port),所有发向那个端口的请求,都会转发到 SSH 跳板机(tunnel-host),然后 SSH 跳板机作为中介,将收到的请求发到目标服务器(target-host)的目标端口(target-port)。
53+
它的语法如下,其中会指定本地端口(local-port)SSH 服务器(tunnel-host)、远程服务器(target-host)和远程端口(target-port)。
5454

5555
```html
56-
$ ssh -L local-port:target-host:target-port tunnel-host
56+
$ ssh -L -N -f local-port:target-host:target-port tunnel-host
5757
```
5858

59-
上面命令中,`-L`参数表示本地转发,`local-port`是本地端口,`target-host`是你想要访问的目标服务器,`target-port`是目标服务器的端口,`tunnel-host`是 SSH 跳板机
59+
上面命令中,有三个配置参数
6060

61-
举例来说,现在有一台 SSH 跳板机`tunnel-host`,我们想要通过这台机器,在本地`2121`端口与目标网站`www.example.com`的80端口之间建立 SSH 隧道,就可以写成下面这样。
61+
- `-L`:转发本地端口。
62+
- `-N`:不发送任何命令,只用来建立连接。没有这个参数,会在 SSH 服务器打开一个 Shell。
63+
- `-f`:将 SSH 连接放到后台。没有这个参数,暂时不用 SSH 连接时,终端会失去响应。
64+
65+
举例来说,现在有一台 SSH 服务器`tunnel-host`,我们想要通过这台机器,在本地`2121`端口与目标网站`www.example.com`的80端口之间建立 SSH 隧道,就可以写成下面这样。
6266

6367
```bash
6468
$ ssh -L 2121:www.example.com:80 tunnel-host -N
@@ -78,7 +82,7 @@ $ curl http://localhost:2121
7882
$ ssh -L 1100:mail.example.com:110 mail.example.com
7983
```
8084

81-
上面命令将本机的1100端口,绑定邮件服务器`mail.example.com`的110端口(POP3 协议的默认端口)。端口转发建立以后,POP3 邮件客户端只需要访问本机的1100端口,请求就会通过 SSH 跳板机(这里是`mail.example.com`),自动转发到`mail.example.com`的110端口。
85+
上面命令将本机的1100端口,绑定邮件服务器`mail.example.com`的110端口(POP3 协议的默认端口)。端口转发建立以后,POP3 邮件客户端只需要访问本机的1100端口,请求就会通过 SSH 服务器(这里是`mail.example.com`),自动转发到`mail.example.com`的110端口。
8286

8387
上面这种情况有一个前提条件,就是`mail.example.com`必须运行 SSH 服务器。否则,就必须通过另一台 SSH 服务器中介,执行的命令要改成下面这样。
8488

@@ -103,13 +107,13 @@ LocalForward client-IP:client-port server-IP:server-port
103107

104108
远程转发指的是在远程 SSH 服务器建立的转发规则。
105109

106-
它跟本地转发正好反过来。建立本地计算机到远程计算机的 SSH 隧道以后,本地转发是通过本地计算机访问远程计算机,而远程转发则是通过远程计算机访问本地计算机。它的命令格式如下。
110+
它跟本地转发正好反过来。建立本地计算机到远程 SSH 服务器的隧道以后,本地转发是通过本地计算机访问远程 SSH 服务器,而远程转发则是通过远程 SSH 服务器访问本地计算机。它的命令格式如下。
107111

108112
```bash
109113
$ ssh -R remote-port:target-host:target-port -N remotehost
110114
```
111115

112-
上面命令中,`-R`参数表示远程端口转发,`remote-port`是远程计算机的端口`target-host``target-port`是目标服务器及其端口,`remotehost`是远程计算机
116+
上面命令中,`-R`参数表示远程端口转发,`remote-port`是远程 SSH 服务器的端口`target-host``target-port`是目标服务器及其端口,`remotehost`是远程 SSH 服务器
113117

114118
远程转发主要针对内网的情况。下面举两个例子。
115119

@@ -197,4 +201,5 @@ $ ssh -L 2999:target-host:7999 tunnel2-host -N
197201
## 参考链接
198202

199203
- [An Illustrated Guide to SSH Tunnels](https://solitum.net/posts/an-illustrated-guide-to-ssh-tunnels/), Scott Wiersdorf
204+
- [An Excruciatingly Detailed Guide To SSH](https://grahamhelton.com/blog/ssh-cheatsheet/), Graham Helton
200205

0 commit comments

Comments
 (0)