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_CONNECTIONholds four values separated by spaces: client IP address, client port, server IP address and server port, for example192.0.2.10 51514 198.51.100.5 22. It is set for any SSH session, including a command run withssh host.example.com uptime.SSH_TTYholds the terminal device, for example/dev/pts/3. It is set only when the session has a terminal, so it is empty forssh -T, for a command run without-t, and forscpandsftp.
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_resetstarts the command with a small set of variables, soSSH_CONNECTIONandSSH_TTYare normally gone unless the sudoersenv_keeplist keeps them.su -also starts a fresh login environment. See SSH agent not available after sudo or su for the same effect onSSH_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 itsupdate-environmentoption (in tmux 3.5 it includesSSH_CONNECTIONandSSH_AUTH_SOCKby default, but notSSH_TTY) into the session when you attach, and that value reaches only windows created after the attach. Runtmux show-environment SSH_CONNECTIONto 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 -istart 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.
Related
- Keep your work after a disconnect with tmux, and attach automatically
- Shell and tmux basics
- SSH agent not available after sudo or su
- SFTP "Received message too long"
- SSH: how it works
- tmux "sessions should be nested with care, unset $TMUX to force"
- Command not found with "ssh host command" but it works after logging in: PATH and .bashrc
- Fix "mesg: ttyname failed: Inappropriate ioctl for device" over SSH, sudo -i or su -