Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
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?

share|improve this question

marked as duplicate by xenoterracide Jun 24 '12 at 14:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

The second answer is the much better one. Please change the mark! – erikb85 Aug 19 '15 at 11:50
up vote 24 down vote accepted

If server B is reachable via ssh and you only need ssh (not direct scp or sftp), this also works very well:

ssh -t $SERVER_A ssh $SERVER_B

The -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.

share|improve this answer
Extra tip: you can add the -A argument to passthrough your identity keys. For example: ssh -A -t $SERVER_A ssh -A $SERVER_B, great solution for one of those oh-I-can't-access-this-server-directly-but-hey-I-can-reach-it-via-that-server-wi‌​th-key-auth – Zyphrax Oct 19 '15 at 17:02

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

See the 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.)

share|improve this answer
I replaced /usr/bin/nc with ssh. then after it asks for password of serverA I get this error(s): Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password). ssh_exchange_identification: Connection closed by remote host – Omid Jun 24 '12 at 7:20
There's no need to replace nc there. See, it's used for creating a TCP connection from serverA to serverB, which the ssh serverB command then uses to talk SSH. – sr_ Jun 24 '12 at 9:22
Just for the record newer versions of ssh support the -W option, you can do something like ProxyCommand ssh -W %h:%p gateway instead of depending on nc – Ulrich Dangel Jun 24 '12 at 9:48
@sr_: You are right! I thought it was mc(midnight commander) – Omid Jun 24 '12 at 19:39
@UlrichDangel you should add that as an answer to the other question: it is not included there. – jasonwryan Jun 24 '12 at 20:05

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