
11

通过代理连接 ssh 服务器
source link: https://blog.diqigan.cn/posts/ssh-proxy.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

通过代理连接 ssh 服务器
碍于各种复杂的网络环境,有些情况下我们并不能直接访问 SSH 服务器。
这时,网络代理和 SSH 隧道就成了救命稻草。
本文记录了通过网络代理和 SSH 隧道连接远程服务器的几种方法,希望对你有所帮助。
使用 SSH 隧道连接服务器
常用于跳板机场景。
ssh ${ssh-user}@${ssh-host} -o ProxyCommand="ssh ${jump-host-user}@${jump-host} -p ${jump-host-port} -W %h:%p"
SSH + ncat 使用 HTTP (s) 代理连接服务器
安装 ncat
sudo apt install ncat
通过代理连接远程服务器
ssh ${ssh-user}@${ssh-host} -o ProxyCommand="ncat --proxy ${proxy-ip}:${proxy-port} --proxy-type http --proxy-auth ${proxy-account}:${proxy-password} %h %p"
SSH + ncat 使用 Socks 代理连接服务器
安装 ncat
sudo apt install ncat
通过代理连接远程服务器
ssh ${ssh-user}@${ssh-host} -o ProxyCommand="ncat --proxy ${proxy-ip}:${proxy-port} --proxy-type socks5 --proxy-auth ${proxy-account}:${proxy-password} %h %p"
SSH + corkscrew 使用 HTTP (s) 代理连接服务器
安装 corkscrew
sudo apt install corkscrew
通过代理连接远程服务器
ssh ${ssh-user}@${ssh-host} -o "ProxyCommand corkscrew ${proxy-ip} ${proxy-port} %h %p [${auth-file-path}]"
上述命令中
[]
包裹的部分代表可选参数。如果 HTTP 代理设有权限校验,需要将 HTTP 代理的账号密码以
account:password
的格式写入到文件中,然后在上述指令中指定[${auth-file-path}]
即可。
SSH + nc 使用无身份校验 Socks 代理连接服务器
nc 工具的优点是多数服务器自带,无需额外安装;缺点是不支持有身份校验的 socks 代理。
ssh ${user}@${host} -o ProxyCommand="nc -X 5 -x ${proxy-ip}:${proxy-port} %h %p"
持久化配置
我们可以通过编辑 ~/.ssh/config
文件把 SSH 连接配置持久化,配置内容如下:
Host ${ssh-host-alias}
HostName ${ssh-host-ip}
Port ${ssh-port}
User ${ssh-user}
ProxyCommand ${proxy-command}
Host *
ServerAliveCountMax 3
ServerAliveInterval 30
ExitOnForwardFailure yes
Compression yes
GSSAPIAuthentication no
ControlMaster auto
ControlPersist 4h
配置完成之后,就可以通过 ssh ${ssh-host-alias}
命令连接服务器啦。
上述命令都可以通过灵活多变的指令来实现不同的效果,还请各位看官自行尝试,此处不再赘述。
SSH 功能非常强大,包括但不限于连接远程服务器、架设 socks 代理、文件传输、远程执行命令等等等等,建议深挖。
SSH config 也非常强大,适当的配置可以帮助我们很好得进行多服务器管理(甩 XShell 几条街)。
</div
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK