Linux – ssh tunneling with hostname in /etc/hosts file

linuxmac-osxsshssh-tunnel

I am trying to ssh to destination host via a jump box. Below is my config file entry

Host 172.* 
   User surendra
   IdentityFile ~/.ssh/id_rsa
   ProxyCommand ssh -q -A surendra@jump nc -q0 %h %p

This works fine when I SSH to destination box with IP address. But when i am trying to SSH with hostname this will not work.

Note: I made an entry in my /etc/hosts file as well.

Can someone please help how to configure the .ssh/config file so that i can ssh to destination box using the hostname by looking the hostname entry in my /etc/hosts file.

Best Answer

Use the -W:

Match exec grep %h /etc/hosts
    [...]
    ProxyCommand ssh -W  %h:%p -q -A surendra@jump

It will resolve the hostnames from your local machine and not from the jumpbox.

It will also check if your host is mentioned in the /etc/hosts.