Strovent SystemsSign in
Reference

Check if a shell is running over SSH, inside tmux, or inside screen

Test SSH_CONNECTION or SSH_TTY for an SSH login, TMUX for tmux and STY for screen, and know when these variables are stale or missing after sudo or su.

Use environment variables. They are set by the program that started the shell, so a startup file or script can test them with no extra tools.

if [ -n "$SSH_CONNECTION" ]; then echo "over SSH"; fi
if [ -n "$SSH_TTY" ];        then echo "interactive SSH login with a terminal"; fi
if [ -n "$TMUX" ];           then echo "inside tmux"; fi
if [ -n "$STY" ];            then echo "inside GNU screen"; fi

SSH: SSH_CONNECTION and SSH_TTY

The OpenSSH server sets these for every session:

  • SSH_CONNECTION holds four values separated by spaces: client IP address, client port, server IP address and server port, for example 192.0.2.10 51514 198.51.100.5 22. It is set for any SSH session, including a command run with ssh host.example.com uptime.
  • SSH_TTY holds the terminal device, for example /dev/pts/3. It is set only when the session has a terminal, so it is empty for ssh -T, for a command run without -t, and for scp and sftp.

To run something only for interactive SSH logins, test SSH_TTY. To skip a step for every SSH session, including file transfers, test SSH_CONNECTION. Text printed by a startup file in a session without a terminal can break SFTP, so put echo lines behind an SSH_TTY test. See SFTP "Received message too long".

tmux: TMUX

tmux sets TMUX in every pane it starts, and TMUX_PANE to the pane's ID, such as %3. The usual guard against starting tmux inside tmux is [ -z "$TMUX" ]. See Keep your work after a disconnect with tmux, and attach automatically.

GNU screen: STY

screen sets STY to the session name inside its windows. When screen runs with STY set, it opens a new window in that session instead of starting a new session.

When the variables are wrong

  • After sudo. sudo's default env_reset starts the command with a small set of variables, so SSH_CONNECTION and SSH_TTY are normally gone unless the sudoers env_keep list keeps them. su - also starts a fresh login environment. See SSH agent not available after sudo or su for the same effect on SSH_AUTH_SOCK.
  • Inside a reattached tmux session. A pane keeps the environment it started with. If you started tmux over SSH and later attach from the host's own console, old panes still show the old SSH_CONNECTION. tmux copies the variables in its update-environment option (in tmux 3.5 it includes SSH_CONNECTION and SSH_AUTH_SOCK by default, but not SSH_TTY) into the session when you attach, and that value reaches only windows created after the attach. Run tmux show-environment SSH_CONNECTION to see the session's current value. The same applies to the agent socket; see SSH agent lost after reattaching tmux or screen: fix SSH_AUTH_SOCK.
  • Programs that clear the environment. cron jobs, systemd services and env -i start without these variables, whatever the login was.

Without the environment: ask systemd-logind

On Linux hosts with systemd, the login session records whether it is remote, and the PAM service that created it. XDG_SESSION_ID is set at login by pam_systemd:

loginctl show-session "$XDG_SESSION_ID" -p Remote -p Service

For an SSH login this prints Remote=yes and Service=sshd. If XDG_SESSION_ID is gone, for example after sudo, use the session ID that loginctl list-sessions shows for your user.

Sources

Spotted a mistake? Email [email protected].