I'd like to know whether there is a way to assign a default ssh gateway to a hostname such that, whenever you attempt to ssh to the hostname it uses the gateway to reach the host. Ideally this should be done in the ssh_config

e.g.: client C attempts to reach target T by

ssh T

The only way this connection can be established is through gateway GW

ssh -tA GW ssh T

EDIT: I neglected to mention that one may have various gateways which will reach different hosts

EDIT: I tried using the ssh config:

Host my-target-host
ProxyCommand ssh -tA GW ssh %h

But then I get "Pseudo-terminal will not be allocated because stdin is not a terminal"

EDIT: Okay so I figured it out :-) The correct config looks like this:

Host my-target-host
ProxyCommand ssh my-gateway-host exec nc %h %p

Not entirely sure why this works though...

| improve this question | | | | |
  • If you found a solution to your question, please post it as an answer and accept it as the correct solution later. Thanks :) – Der Hochstapler May 26 '12 at 14:39
  • I would love to know why it works... How does netcat know to use my forwarded ssh-agent? What about all the particulars about the ssh protocol, how does netcat know this? – jabalsad May 26 '12 at 14:45
  • That might already be a new question. In that case, ask a new question and add a link to this one. – Der Hochstapler May 26 '12 at 14:56

After some tinkering and searching, I found that this works (for reasons that are beyond me):

In your ssh_config:

Host my-target-host
ProxyCommand ssh my-gateway-host exec nc %h %p
| improve this answer | | | | |
  • This command causes your local SSH client to establish two SSH sessions. The first is to the gateway host where it starts netcat to serve as a dumb proxy to the target host. The second is via the netcat proxy to the target host. Because it is a proxy, your local ssh-agent (not the gateway) serves the key. – Jeremy W May 26 '12 at 15:22

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.