Alliot's blog

Using a proxy for ssh connections based on netcat

  Sometimes, we need to forward our ssh connection through a proxy, For example, you want to connect your cloud server through socks5 proxy. When you try to specify a proxy by executing export all_proxy=socks5://127.0.0.1:123 command through terminal, you will find that it does not take effect for ssh connection. Fortunately, netcat can easily achieve your purpose.

Install netcat

1
2
3
4
5
6
7
8
# homebrew 
brew install netcat

# apt
apt install netcat

# rhel/centos
yum install nmap

Configure ssh config

vim ~/.ssh/config (Create it if it doesn’t exist.)

1
2
3
4
5
6
Host alliot_server-01       # specify a alias for this host
Hostname 192.168.1.123 # the IP/Hostname of this ssh connection
User root # ssh username
ForwardAgent yes
Port 22 # port
ProxyCommand /usr/bin/nc -X 5 -x 127.0.0.1:7890 %h %p # This configuration represents a proxy using the SOCKS v.5 protocol, and the proxy address is 127.0.0.1:1234

After do this, You can establish a connection on the basis of this proxy directly through ssh alliot_server-01.

Dry-run

  If you’re only connecting temporarily and don’t need persistence, you can directly use the following command:

1
ssh -o ProxyCommand="/usr/bin/nc -X 5 -x 127.0.0.1:1234 %h %p" root@192.168.1.123 -p 22

------ 本文结束 ------

本文标题:Using a proxy for ssh connections based on netcat

文章作者:Alliot

发布时间:2022年07月21日 - 19:07

最后更新:2022年11月27日 - 00:11

原始链接:https://www.iots.vip/post/ssh-over-proxy-by-nc.html

许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 转载请保留原文链接及作者。

若文章为您解决燃眉之急或是带来些许明朗,不妨打赏 Alliot 一杯香茗或是一杯咖啡