Is there an easy way to tell if I am SSH'd in somewhere without typing 'exit'?

Like, if I am currently sitting at computer A and I SSH'd to computer B and then from B to computer C, is there a way I would be able to tell short of closing the session and using 'hostname' to discover where I am at?

(Presumably, this solution would also make it clear that I am not SSH'd anywhere, if I am just at localhost but if I am SSH'd elsewhere and from there SSH'd back to my local machine, I'd be able to tell.)


Check out $SSH_CONNECTION. It tells you which IP and port you are currently connecting from and to. At least, this is the case with OpenSSH. YMMV with other servers.


If you know you are 'ssh'ed, but don't know from where, try


at a command-line. The output includes a FROM field which may be useful.


Try setting your prompt to include the hostname; otherwise I like to use screen on every login with a status line containing the hostname (a bit complicated to setup, and it gets recursive with status lines within screens with status lines). You should be able to echo $HOSTNAME or just execute hostname.

  • Not as fun to set up but with the amount of machines I need to get to, I think it's time I just started using screen. :-). – briealeida Nov 15 '09 at 18:39

I like the "w" answer,

and another way is to check the hostname to find out what the computers name is:


In your scenario, connect directly from A to C, if this is possible. This will avoid the double encryption. And perhaps will increase latency.

Also modify the shell variable PS1 for every account to reflect the hostname, if it doesn't do it yet. This trick will also help you in situations when you use chroot.

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.