SSH to a host through multiple jump hosts. [Linux]
It is really frustrating to type credentials when connecting to a particular host through multiple jump hosts.
Is there a way to make things easier? Luckily YES! Follow these steps to make you life easier.
For explanation purpose I will use the following example. Here, we are trying to SSH to Host-C via Host-A and Host-B. You can extend this method to any number of Hosts.
[Your Computer]<—>[Host-A] <—> [Host-B] <—> [Host-C]
[1] First of all, create this file if not exists ~/.ssh/config
Then open that file using your favorite text editor. And type following entries and save it.
Host host-a
User username1
Hostname hosta.domain
Host host-b
User username2
Hostname hostb.domain
Port 22
ProxyCommand ssh -q -W %h:$p host-a
Host host-c
User username3
Hostname hostc.domain
Port 22
ProxyCommand ssh -q -W %h:$p host-b
[2] Saving credentials.
Open the terminal in ~/.ssh/ directory. Here we have 3 hosts; therefore, first we will create 3 rsa keys using this command.
ssh-keygen -t rsa -N "" -f hosta.key
ssh-keygen -t rsa -N "" -f hostb.key
ssh-keygen -t rsa -N "" -f hostc.key
Now we will save the credentials for each host. Execute the following command with argument as the host name and enter the password. Repeat for all hosts.
ssh-copy-id host-a
ssh-copy-id host-b
ssh-copy-id host-c
[3] Test the SSH. Now you can ssh to any host quickly using a single ssh command.
ssh host-c