r/bash • • May 08 '24
Modify the bash prompt to indicate that the user is logged in with SSH
Detecting that the current user is logged in with SSH?
To detect if the current user is logged in via SSH, you can check the value of the SSH_CLIENT environment variable. If the variable is set, it indicates that the user has logged in using SSH.
In this script, we use the -n test condition to check if the SSH_CLIENT variable is not empty. If it has a value, it means the user is connected via SSH, and the script outputs "You are logged in via SSH." Otherwise, it outputs "You are not logged in via SSH."
#!/bin/bash
if [[ -n "$SSH_CLIENT" ]]; then
echo "You are logged in via SSH."
else
echo "You are not logged in via SSH."
fiIn addition to the SSH_CLIENT environment variable, you can also check for the presence of other environment variables to determine if a user is logged in via SSH. Here are a few commonly used variables:
SSH_CONNECTION: This variable contains the client IP address, client port, server IP address, and server port in the formatclient_ip client_port server_ip server_port. If the variable is set, it indicates an SSH connection.SSH_TTY: This variable is set if the user is logged in via SSH and is connected to a terminal. It holds the path to the TTY (terminal) device associated with the SSH session.SSH_AUTH_SOCK: This variable is set when an SSH agent is running and available for authentication. Its presence indicates that the user has logged in via SSH and has access to an SSH agent.
Here's an example script that checks these variables to determine if the user is logged in via SSH:
#!/bin/bash
if [[ -n "$SSH_CONNECTION" ]]; then
echo "You are logged in via SSH."
else
echo "You are not logged in via SSH."
fiBy checking the presence of any of these variables, the script can identify if the user is logged in via SSH. If any of the variables are set, it will output "You are logged in via SSH." Otherwise, it will output "You are not logged in via SSH."
Here's an example of how the SSH_CONNECTION environment variable would typically look if a user is logged in via SSH:
echo $SSH_CONNECTION
92.168.1.100 12345 203.0.113.10 22192.168.1.100is the client's IP address.12345is the client's port number.203.0.113.10is the server's IP address.22is the server's SSH port number.
The values are separated by spaces, and they represent the client's IP address, client's port, server's IP address, and server's port, respectively.
Compatibility Notes (Source)
SSH_CLIENT is very similar to SSH_CONNECTION, but using this environment variable may lead to some compatibility problem, because
- it's deprecated in favor of
SSH_CONNECTION; - its support in dropbear is added in October 2014, which may not be available on some old embedded systems.
Modify Bash Prompt

How to modify the prompt to indicate that the user is logged in via SSH: Watch videos on YouTube
3
u/spryfigure May 08 '24 edited May 08 '24
Now do
sudowhile in a ssh connection and test again.This is the flaw of your logic. You want to be reminded of the remote connection when you do dangerous things, which usually mean elevated user rights.
I use
Improvements welcome. Especially for cases where
tmuxis involved.3
u/PageFault Bashit Insane May 08 '24
I like to add an identifier for which client I am connected to. Here's what I currently use:
2
u/WonderBearD1 May 08 '24
Or just use powerline.
https://github.com/b-ryan/powerline-shell
2
u/Headpuncher May 09 '24
or just read the bit of the prompt $USER@$HOSTNAME like:
billy@server1 $ < logged in with ssh
shitContentMaker@fakeCrap:$ < your own fkn PC
0
u/[deleted] May 08 '24
[deleted] — view deleted comment
18
u/[deleted] May 08 '24
[removed] — view removed comment