6

I want to see if I'm connected via ssh or running a local terminal.

If I just ssh into a server without changing to root via sudo, it's easy. Any of the variables $SSH_CLIENT, $SSH_CONNECTION or $SSH_TTY can be used to check if one is connected via SSH or local.

Problem: When I elevate to the root account with sudo -i to do administrative stuff, neither of these variables are helpful - they are all empty.

What is the best way to find out if the connection is local or via SSH then?

EDIT: With the accepted answer, it is easy to have an unobtrusive bash prompt which reflects ssh status and privileges:

if [ "$color_prompt" = yes ]; then
    # when system is accessed via SSH, hostname with light grey background
    if [[ $(pstree -s $$) = *sshd* ]]; then sshbg="\[\033[48;5;7m\]"; fi
    # when used as root, change username to orange and '#' to red for prompt
    if [ $(id -u) -eq 0 ]; then usercol="\[\033[38;5;3m\]"; hashcol="\[\033[38;5;1m\]"; else usercol="\[\033[38;5;2m\]"; fi
    # bash PS1 prompt
    PS1="${usercol}\u\[$(tput sgr0)\]@\[$(tput sgr0)\]\[\033[38;5;4m\]${sshbg}\h\[$(tput sgr0)\]:\[$(tput sgr0)\]\[\033[38;5;6m\]\w\[$(tput sgr0)\]${hashcol}\\$ \[$(tput sgr0)\]"
    unset sshbg rootcol hashcol
fi

The timed version of the pstree part runs in less than 20ms, so it can be used without introducing noticeable delays.

emk2203
  • 4,166
  • 1
  • 21
  • 47

2 Answers2

5

If you're in a bash shell, you can do pstree -s $$ ($$ is the current shell's PID) and look for "sshd" in the output.

Brian Turek
  • 1,826
  • Added another idea as an edit, try pstree -s $$ – Brian Turek Aug 28 '20 at 10:15
  • This is actually the best suggestion. Looking for "sshd" in the string – emk2203 Aug 30 '20 at 08:47
  • I researched the implications of your suggestion for a while, and it seems that the pstree trick is indeed the best variant. I would like to award you best answer, but at the moment, there's still the cat suggestion on top which is misleading and doesn't help much. Could you rewrite your answer a little? – emk2203 Sep 16 '20 at 08:07
  • Nuked the cat option :) – Brian Turek Sep 16 '20 at 08:11
2

You can also use:

who am i | awk -F' ' '{print $2}'

If it says pts/0, pts/1 etc. you're on SSH, if it says tty1, tty2 etc. you're on a local connection.

When I connect using SSH I get the following from who am i:

am       pts/0        2020-08-28 12:33 (172.17.0.5)

Maybe the IP address can also be used as an indicator to differentiate an SSH connection from a local connection on the desktop.

Also explained here: How to check which tty am I currently using?.

Artur Meinild
  • 26,018
  • This appears to not work if you're in a Desktop environment :( who am i literally returns nothing. – Brian Turek Aug 28 '20 at 10:00
  • For me, who am i (or who -m really) returns the same value for Desktop environment and SSH session - pts/0. Works for a real tty, though, but only to use from tty is too strong a restriction. – emk2203 Aug 28 '20 at 10:04
  • What about the IP address who am i returns as 5th column? What does the IP say when you're in a desktop environment - just the IP of localhost? Maybe you can use this as an indicator. – Artur Meinild Aug 28 '20 at 10:34
  • It appears to be some weird byproduct of certain versions of gnome-terminal. I don't know if it's a bug or something reliable. I found bug ticket for RHEL: https://bugzilla.redhat.com/show_bug.cgi?id=91442 – Brian Turek Aug 28 '20 at 11:51
  • Ah ok - yeah it sounds like a bug if it's not consistent at least - because if it were consistent then it would also be a usable criteria to determine the answer to the OP. – Artur Meinild Aug 28 '20 at 11:52