@@ -59,46 +59,50 @@ SSH 配置 TCP 端口转发的格式为 `[bind_address:]port:host:hostport`,SS
59
59
60
60
本地端口转发(** L** ocal port forwarding)
61
61
62
+ ![ ] ( E:\erchius\Git\storage\igem\Linux201-docs\docs\images\ssh\local-forward.png )
63
+
62
64
: 在本地上监听一个端口,将收到的数据转发到远程主机的指定端口。即** 将远程主机上某个服务的端口转发到本地** ,使本地的其他程序可以通过 SSH 访问到远程的服务。例如将远程主机的 80 端口转发到本地的 8080:
63
65
64
66
```shell
65
67
ssh -L 8080:localhost:80 example
66
68
```
67
-
69
+
68
70
也可以将远程主机所在网络的机器通过这种方法转发,假设需要访问的远程主机网络内部的机器名叫 `internalserver`:
69
-
71
+
70
72
```shell
71
73
ssh -L 8080:internalserver:80 example
72
74
```
73
-
75
+
74
76
本地端口转发默认监听在 localhost。如果要监听其他地址,可以指定需要监听的地址,例如:
75
-
77
+
76
78
```shell
77
79
ssh -L 0.0.0.0:8080:localhost:80 example
78
80
```
79
-
81
+
80
82
虽然 SSH 客户端也有一个 `GatewayPorts` 选项,但它只影响没有指定监听地址的语法模式(即三段式 `localport:remotehost:remoteport`)。指定四段式语法后,`GatewayPorts` 选项不再起作用。
81
83
82
84
远程端口转发(** R** emote port forwarding)
83
85
86
+ ![ ] ( E:\erchius\Git\storage\igem\Linux201-docs\docs\images\ssh\remote-forward.png )
87
+
84
88
: 在远程主机上监听一个端口,将收到的数据转发到本地的指定端口。即** 将本地某个服务的端口转发到远程主机上** ,使远程的其他程序可以通过 SSH 访问到本地的服务。例如将本地主机的 80 端口转发到远程主机的 8080 端口:
85
89
86
90
```shell
87
91
ssh -R 8080:localhost:80 example
88
92
```
89
-
93
+
90
94
上面命令表示在远程主机 example 上监听 8080 端口,将收到的数据转发到本地的 80 端口。
91
-
95
+
92
96
同样的,也可以将本地网络中的机器做转发,假设对应机器名为 `myinternalserver`:
93
-
97
+
94
98
```shell
95
99
ssh -R 8080:myinternalserver:80 example
96
100
```
97
-
101
+
98
102
注意远程端口转发默认只能监听 localhost。如果要监听其他地址,需要在远程主机的 `sshd_config` 中设置 `GatewayPorts yes`。与另外两种端口转发不同,客户端无法覆盖服务端的 `GatewayPorts` 设定。
99
-
103
+
100
104
在 OpenSSH 7.6 版本之后的客户端,`-R` 也可以用来让远程主机利用本地作为 SOCKS5 代理(相当于下面的 `-D` 参数反过来),对应手册中的 `-R [bind_address:]port` 部分:
101
-
105
+
102
106
```shell
103
107
ssh -R 1080 example
104
108
# 指定远程主机上的监听地址
@@ -107,20 +111,22 @@ SSH 配置 TCP 端口转发的格式为 `[bind_address:]port:host:hostport`,SS
107
111
108
112
动态端口转发(** D** ynamic port forwarding)
109
113
114
+ ![ ] ( E:\erchius\Git\storage\igem\Linux201-docs\docs\images\ssh\dynamic-forward.png )
115
+
110
116
: 在本地监听一个端口用作 SOCKS5 代理,将收到的数据转发到远程主机,相当于** 利用了远程主机作为代理** 。例如:
111
117
112
118
```shell
113
119
ssh -D 1080 example
114
120
```
115
-
121
+
116
122
由于 SOCKS 代理是一个通用的代理协议,因此可以用于任何 TCP 连接,不仅仅是 HTTP。
117
-
123
+
118
124
与 LocalForward 类似,DynamicForward 也可以指定监听地址:
119
-
125
+
120
126
```shell
121
127
ssh -D 0.0.0.0:1080 example
122
128
```
123
-
129
+
124
130
同样地,`GatewayPorts` 只影响没有指定监听地址的语法模式(即只给出了一个端口)。指定监听地址后,`GatewayPorts` 选项不再起作用。
125
131
126
132
以上三种端口转发都可以在配置文件中指定,例如:
@@ -185,7 +191,7 @@ SFTP(Secure File Transfer Protocol)和 SCP(Secure Copy Protocol)都是
185
191
!!! tip "Rsync"
186
192
187
193
SCP 和 SFTP 能够提供的文件传输功能较为基础。如果你需要更多的功能,例如增量传输、断点续传、文件校验等,可以考虑使用 Rsync。Rsync 可以使用 SSH 作为传输层,因此可以替代 `scp` 命令。
188
-
194
+
189
195
详情可以参考[本教程关于 Rsync 的章节](../ops/storage/backup.md#rsync)。
190
196
191
197
### SCP
@@ -241,9 +247,9 @@ scp username@remotehost:/path/to/remote/file /path/to/local/directory
241
247
```shell
242
248
scp -P 2222 /path/to/local/file username@remotehost:/path/to/remote/directory
243
249
```
244
-
250
+
245
251
!!! tip
246
-
252
+
247
253
你也可以在 SSH 客户端配置文件中为 `Host remotehost` 指定 `Port 2222`,这样就不需要每次在命令行中指定端口了。
248
254
249
255
限制带宽
@@ -269,9 +275,9 @@ scp username@remotehost:/path/to/remote/file /path/to/local/directory
269
275
```shell
270
276
scp -C /path/to/local/file username@remotehost:/path/to/remote/directory
271
277
```
272
-
278
+
273
279
!!! tip
274
-
280
+
275
281
你也可以在 SSH 客户端配置文件中为 `Host remotehost` 指定 `Compression yes`,这样就不需要每次在命令行中启用压缩了。
276
282
277
283
### SFTP
@@ -313,7 +319,7 @@ sftp -P 2233 username@remotehost
313
319
!!! 使用脚本进行自动化操作
314
320
315
321
通过创建一个包含 SFTP 命令的批处理文件,你可以 让SFTP 会话自动执行这些命令。例如,你可以创建一个文件 `upload.txt`,其中包含以下内容:
316
-
322
+
317
323
```shell
318
324
put file1.txt
319
325
put file2.jpg
@@ -350,7 +356,7 @@ sshd 接受 SIGHUP 信号作为重新载入配置文件的方式。`sshd -t` 命
350
356
: 限制此公钥只能用于执行指定的命令,且不能登录 shell。如果使用此公钥登录时提供了额外的命令(例如 ` ssh user@host some/other/command ` ),提供的命令将会在 ` SSH_ORIGINAL_COMMAND ` 环境变量中传递给指定的命令。
351
357
352
358
指定命令的一个常用场景是为备份服务提供有限的访问,例如 `command="/usr/bin/rrsync /path/to/backup"`,这样备份服务就只能使用 rsync 命令访问指定的目录。
353
-
359
+
354
360
如果你需要使用 `command=` 的话,你很可能也需要 `restrict`(见下)。
355
361
356
362
` no-port-forwarding ` , ` no-X11-forwarding ` , ` no-agent-forwarding ` , ` no-pty ` , ` no-user-rc `
@@ -362,7 +368,7 @@ sshd 接受 SIGHUP 信号作为重新载入配置文件的方式。`sshd -t` 命
362
368
: 禁止所有可选功能,相当于同时使用上一条列出的(和没列出的,详情见 man page)所有选项。
363
369
364
370
通常与 `command=` 搭配使用,确保指定公钥只能做指定的事情。
365
-
371
+
366
372
如果需要在 `restrict` 的基础上单独开放某些功能,可以使用 `port-forwarding` 等(也就是去掉前面的 `no-`)。
367
373
368
374
完整的选项列表可以在 [ sshd(8)] [ sshd.8 ] 的 ` AUTHORIZED_KEYS FILE FORMAT ` 部分找到。
@@ -383,7 +389,7 @@ sshd 接受 SIGHUP 信号作为重新载入配置文件的方式。`sshd -t` 命
383
389
Host example
384
390
HostName example.com
385
391
User user
386
-
392
+
387
393
Include ~/.ssh/global.conf
388
394
```
389
395
@@ -395,7 +401,7 @@ sshd 接受 SIGHUP 信号作为重新载入配置文件的方式。`sshd -t` 命
395
401
Host example
396
402
HostName example.com
397
403
User user
398
-
404
+
399
405
Match all
400
406
Include ~/.ssh/global.conf
401
407
```
@@ -406,7 +412,7 @@ sshd 接受 SIGHUP 信号作为重新载入配置文件的方式。`sshd -t` 命
406
412
407
413
```shell
408
414
Include ~/.ssh/global.conf
409
-
415
+
410
416
Host example
411
417
HostName example.com
412
418
User user
0 commit comments