Super User is a question and answer site for computer enthusiasts and power users. 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

If I'm in a terminal and I ssh into a new host, then ssh to another host, then another etc., is there a way to determine this chain of hosts other than recursively exiting and seeing where I end up?

share|improve this question

There are variables called SSH_CLIENT and SSH_CONNECTION that show in your current shell where you ssh'd from and which IP you came in on, but that only shows for the last connection. There are commands you can send to each ssh client connection (such as <RET><RET>~C to open the command line), but this is limited to just a few things. Maybe the most interested to you would be ~#, which shows connection info, but it doesn't show hosts.

If you can keep track of how many times you press ~ after <RET><RET>, you can background connection N in your chain starting from the end and in turn echo $SSH_CONNECTION each time to see where it goes. When you get back to your host, you'll need to foreground them all again with fg.

I guess my question for you would be, why do you need to know and do you need to know often?

share|improve this answer
I work in an environment with many hosts and I often ssh from machine to machine when I need to check something out on each host without bothering to exit. I tend to work out of the same few windows and after a while there are sometimes many jumps. This is fine except when it turns out one of the hosts is going down and I want to know which of my terminals is about to get killed. I won't be doing this often, so efficiency isn't too important. – pythonic metaphor Nov 29 '10 at 21:13

Run netstat -t -n|grep 22 (replace 22 with port number of your ssh-server if you changed it), at least you know the IP address of the host you are coming from. Or try who

share|improve this answer
who gets me part of the way. You get output that looks like "user pts\3 Nov 30 09:09 (hostname)". And you can see what your current tty is with, well, tty. This at least establishes the first link. – pythonic metaphor Nov 30 '10 at 14:22
Oh yeah who is the way in your case also because there could be several ssh connections at a time and thus difficult to tell from netstat output. – Ken Ratanachai S. Nov 30 '10 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .