20

What are the SSH_TTY and SSH_CONNECTION variables for? On my Ubuntu system both of them are blank; what values should they have?

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233

4 Answers4

17

From ssh man page:

SSH_CONNECTION

Identifies the client and server ends of the connection.
The variable contains four space-separated values: client IP address,
client port number, server IP address, and server port number.

....

SSH_TTY

This is set to the name of the tty (path to the device) associated
with the current shell or command.  If the current session has no tty,
this variable is not set.

Because you are not in a ssh session, so these variables are not set.

cuonglm
  • 153,898
3

As mentioned by others, these variables are only set when a session is made. To see this for yourself, inspect the values under an active SSH session:

ssh user@host 'echo SSH_TTY: $SSH_TTY SSH_CONNECTION: $SSH_CONNECTION'
3

Upon a successful connection, OpenSSH sets several environment variables.

SSH_CONNECTION shows the address of the client, the outgoing port on the client, the address of the server and the incoming port on the server.

SSH_TTY names the pseudo-terminal device, abbreviated Ppty, on the server used by the connection.

For example:

SSH_CONNECTION='192.168.223.17 36673 192.168.223.229 22'
SSH_TTY=/dev/pts/6
Maythux
  • 1,888
2

Both variables are only defined when you are logged in through ssh. SSH_TTY specifies the device node of the current virtual terminal (e.g. /dev/pts/25). The SSH_CONNECTION is set to ip and port on client and on host machine (4 fields separated by whitespace).

orion
  • 12,502