SSH ProxyCommand example: Going through one host to reach another server

www.laut‮t‬uri.com
SSH ProxyCommand example: Going through one host to reach another server

The ProxyCommand option in OpenSSH allows you to specify a command that will be used to establish a connection to a remote host. This can be useful in situations where you need to go through one host in order to reach another host.

For example, to connect to host b through host a, you can use the following ProxyCommand option:

ProxyCommand ssh -q -W %h:%p a

This will connect to host a and then establish a connection to host b. The %h and %p variables in the ProxyCommand option are replaced with the target host and port, respectively.

To use this ProxyCommand option, you can specify it on the command line when connecting to host b:

ssh -o ProxyCommand='ssh -q -W %h:%p a' b

You can also include the ProxyCommand option in the ~/.ssh/config file to use it every time you connect to host b. For example:

Host b
    HostName b
    User username
    ProxyCommand ssh -q -W %h:%p a

Keep in mind that you will need to have an account on host a and have passwordless SSH access to it in order to use the ProxyCommand option.

You can also specify additional options, such as port forwarding or agent forwarding, in the ProxyCommand option. For example:

ProxyCommand ssh -A -L 8080:localhost:8080 a nc %h %p

This will connect to host a, forward the local port 8080 to port 8080 on host a, and then establish a connection to host b.

Created Time:2017-10-30 14:27:17  Author:lautturi