I am using ssh tunneling to login to system behind gateway. Problem is that I have ssh_alias defined in ~/.ssh/config on gateway server. Where I have key and ip setup. But those alias don't work with nc command. Is there a way to use alias with nc command and use keys setting done on gateway server? So I don't need to download key on local server. Example: GatewayServer

Host test
IdentityFile ~/keys/test
User test

LocalServer ~/.ssh/config

  Host test
  ProxyCommand ssh gateway nc %h %p

Following configuration fails as nc don't know about test.


What you asking is almost the same as this.

If you don't want to copy key from gateway to local box, the work around is use Method 1 (bash alias) in that post(and below). ssh_config cannot do what you want. I have detail explanation there about the difference between alias and tunnel method, which include ssh_config ProxyCommand.

Method 1

In ~/.bashrc, add following line

alias ssh-test='ssh -t gateway ssh test@test'

In command prompt, just type following


What ssh -t gateway ssh test@test do is almost the same as follow

local# ssh gateway
gateway# ssh test@test

Other than combining the two step in one, it skip opening a shell(csh, bash, etc) on gateway, instead starting a ssh session to test right after authentication.

You don't need ~/.ssh/config on local box BUT keep the one gateway for the test (for key usage).

Adding ForwardAgent yes to gatway .ssh/config

Host test
    ForwardAgent yes
    IdentityFile ~/keys/test
    User test

Method 2

If you don't want to copy gateway key to local box, but permitted to install your local ssh key into test@test. Then you can use following

local ~/.ssh/config

Host gateway
    User <gateway user>
    HostName <gateway/IP>

Host test
    User test
    Port 22
    ProxyCommand ssh gateway nc %h %p
| improve this answer | |
  • One more problem. With method one ssh-agent don't work. Every time I need to give password. Is there a solution for that – Vivek Goel Jan 6 '13 at 6:47
  • I am not 100%, but try this alias ssh-test='ssh -t -A gateway ssh -A test@test'. If it works I will update the answer too. – John Siu Jan 6 '13 at 6:50
  • No that does not work. I have already tried that solution. – Vivek Goel Jan 6 '13 at 6:51
  • See if adding ForwardAgent yes to gateway config works. I updated the answer too. – John Siu Jan 6 '13 at 6:58
  • that think -A option will do right? – Vivek Goel Jan 6 '13 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.