ssh via multiple hosts
For connecting to server B I have to first ssh to server A. What's the command line to access server B?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
If server B is reachable via
ssh and you only need
ssh (not direct
sftp), this also works very well:
ssh -t $SERVER_A ssh $SERVER_B
-t option forces allocation of a pseudo-tty even when running a single command at the other end. This is helpful, since
ssh needs a pseudo-tty.
Since you're using two nested instances of
ssh, the escape character in the inner session is Enter ~ ~ (two tildes). One tilde will send the escape to the first shell.
There isn't a built-in way in
ssh to do this, other than to use port forwarding.
However, there is a way that works reasonably well - the
ProxyCommand setting for ssh. You can specify that on a per-host basis in
~/.ssh/config and use it to specify the command to run to connect to the remote ssh port.
I use this on several hosts:
host serverB.example.com serverB ProxyCommand /usr/bin/ssh serverA.example.com /usr/bin/nc %h %p
ssh(1) manual page for the details, and
nc(1) from the
netcat package for the command I am using to forward on the connection. (You can use anything that makes a TCP connection and passes standard input and output through it, though.)