File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed
Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -246,6 +246,72 @@ $ ssh-keygen -R <ip/hostname>
246246- PEM
247247- PKCS8
248248
249+ 端口转发
250+ ---------------
251+ <!-- rehype:body-class=cols-6-->
252+
253+ ### 动态端口转发语法
254+ <!-- rehype:wrap-class=col-span-3-->
255+
256+ 将本地端口变成一个 SOCKS 代理,自动转发任意目标地址的请求(适用于代理上网)。
257+
258+ ``` shell
259+ $ ssh -D 本地SOCKS端口 用户名@SSH服务器 -N
260+ ```
261+
262+ 示例:开启本地 1080 端口作为 SOCKS5 代理:
263+
264+ ``` shell
265+ $ ssh -D 1080 -N
[email protected] 266+ ```
267+
268+ ### 验证 SOCKS5 代理
269+ <!-- rehype:wrap-class=col-span-3-->
270+
271+ 使用 curl 命令验证 SOCKS5 代理是否工作正常:
272+
273+ ``` shell
274+ # 设置
275+ $ ssh -D 1080 -N
[email protected] 276+ # 验证
277+ $ curl --socks5 127.0.0.1:1080 https://ifconfig.me
278+ ```
279+
280+ 如果返回的是 ` example.com ` 服务器的公网 IP,说明 SOCKS5 代理正常工作。
281+
282+ ### 本地端口转发
283+ <!-- rehype:wrap-class=col-span-3-->
284+
285+ 将本地端口的流量通过 SSH 隧道转发到目标服务器(适用于访问远程/内网服务)。
286+
287+ ``` shell
288+ $ ssh -L [本地IP:]本地端口:目标IP:目标端口 用户名@SSH服务器 -N
289+ ```
290+
291+ 示例:将本地的 ` 4000 ` 端口转发到远程内网服务器 192.168.1.10:80:
292+
293+ ``` shell
294+ $ ssh -L 4000:192.168.1.10:80
[email protected] -N
295+ ```
296+
297+ 其中 ` -N ` 表示不执行远程命令,只是建立隧道。
298+
299+
300+ ### 远程端口转发
301+ <!-- rehype:wrap-class=col-span-3-->
302+
303+ 让远程 SSH 服务器上的端口转发到本地的某个服务(适用于让外部访问你本地服务)。
304+
305+ ``` shell
306+ $ ssh -R [SSH服务器IP:]远程端口:本地IP:本地端口 用户名@SSH服务器 -N
307+ ```
308+
309+ 示例:将远程服务器的 5000 端口映射到你本地的 localhost:3306(MySQL):
310+
311+ ``` shell
312+ $ ssh -R 5000:localhost:3306
[email protected] -N
313+ ```
314+
249315另见
250316--------
251317
You can’t perform that action at this time.
0 commit comments