Use ProxyCommand with ssh to connect to host1 through host2

Sometime an host (e.g. host1) cannot be reached directly with ssh. However, host2 can be reached via ssh, and host2 can connect to host1 via ssh.

I define:

However, username1 and username2 can be identical.

You can transparently connect to host1 through host2 with ssh by setting up a ProxyCommand in .ssh/config file:

Host host1
    User username1
    ProxyCommand ssh username2@host2 exec nc host1 22

Now you can connect directly to host1 with the following command:

ssh host1

XKCD comics https://xkcd.com/530/

If you want to ask me a question or leave me a message add @bougui505 in your comment.