ssh-forwarding

ssh forwarding #

本地端口映射到远程服务器上 #

bash
ssh -fgNR 127.0.0.1:80:127.0.0.1:80 hostname
  • -f表示后台执行
  • -g表示远程主机访问转发的端口
  • -N表示不执行任何命令

服务端开启GatewayPorts yes之后,绑定远程IP地址才会生效,详见sshd_config(5)

服务端开启AllowTcpForwarding yes之后(默认yes),转发tcp端口才会生效。

远程端口映射到本地 #

bash
ssh -fgNL 127.0.0.1:80:127.0.0.1:80 hostname

用例:本地http代理映射到远程服务器上 #

转发端口:

bash
ssh -gNR 127.0.0.1:2080:127.0.0.1:7890 hostname

远程服务器设置代理:

text
~/.bashrc
enable_http_proxy() {
    export http_proxy=http://127.0.0.1:2080
    export https_proxy=http://127.0.0.1:2080
}

disable_http_proxy() {
    unset http_proxy
    unset https_proxy
}

#enable_http_proxy
2024年7月12日