# Using a proxy server for ssh connection

When using ssh, you often have to use proxy server. For example, when you are at work you can directly connect to every sever (ex: server-i with i from 1 to n) on the local-network, but from home only few of those are connected to the Internet (ex: only server-1). Thus you need to first connect to one server accessible from the Internet (server-1) in order to secondly connect to another server of your work local-network which is not (server-i with i from 2 to n). In a typical ssh configuration file you will have two way to connect to server-2:

Host server-2
Hostname            server-2.com
Port                22
ServerAliveInterval 3600
ControlMaster       auto
ControlPersist      yes
ControlPath         ~/.ssh/socket-%r@%h:%p
• from elsewhere:
Host server-2.proxy
ProxyCommand        ssh -e none server-1 exec nc %h %p 2>/dev/null
Hostname            server-2.com
Port                22
ServerAliveInterval 3600
ControlMaster       auto
ControlPersist      yes
ControlPath         ~/.ssh/socket-%r@%h:%p

Using the sed command and you ssh configuration file you can easily configure your server such that happening for example .proxy to a server name will pipe the connection through the server proxy. To do just that edit out ~/.ssh/config file to add the following sections:

Host proxy
Hostname            server-1.com
ProxyCommand        none
ProxyCommand        ssh -aY proxy 'nc -w 900 echo %h | sed s/\\.proxy\$// %p'
ServerAliveInterval 3600