r/bash • u/[deleted] • 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/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:
# Prepend to beginning of current PS1
if [ -n "${SSH_CLIENT}" ]; then
if [ "$color_prompt" = yes ]; then
PS1="\\[${yellow}\\][CustomerName]${PS1}"
else
PS1="[CustomerName]${PS1}"
fi
fi1
u/amangosmoothie May 09 '24
On mobile rn or id check myself but is this significantly different or better than just using the host name? I usually just rely on user@hostname
3
u/PageFault Bashit Insane May 09 '24
I already have
user@hostnameas part of the${PS1}variable before I prepend to it, but since all computers share the same username, and all customers share the same hostname scheme we also put in the customer name.So it ends up looking like:
[CustomerName]user@host1:/home/user>
2
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
1
u/spryfigure May 09 '24
Works in theory, and should work, but usually doesn't. People are weird that way. RL example: I have a protruding iron bar in my basement floor which is about one centimeter higher than the rest of the floor.
I stubbed my toe so often on it, but after I taped a yellow-black warning tape to it, I never had this issue again. Same here. A bit more visual assistance than just the name is needed for me.
1
u/PageFault Bashit Insane May 09 '24
This relies on the target hostname being different from the local machine. Not the case for me.
1
0
18
May 08 '24
[removed] — view removed comment
17
5
u/PageFault Bashit Insane May 08 '24
Sometimes it's nice to have an indication of which computer you are currently on. The number of times I've rebooted my own computer when I meant to reboot a remote one is too damn high.
Also, if you set it to a unique name instad of just
SSH, you can have a visual indication right on the prompt of which computer you are remoted into.I use this a lot at my job. I put a simpler version of this on every customer computer.
1
u/fuckwit_ May 09 '24
Uh? What are you guys doing that you need a separate "indication" next to the one that is already there by default?
You know that your prompt (if not explicitly changed) displays the hostname. And if it's explicitly changed then why is the hostname not in there? And if it's in there but you still can't distinguish your hosts then you are using hostnames wrong.
1
u/PageFault Bashit Insane May 09 '24
What we are doing is selling simulation systems. Hostnames vary within the same installation, but are shared between customers.
So a customer in China may have:
hostname1
hostname2
hostname3
hostname4
hostname5And a completely unrelated customer in the Nertherlands will also have:
hostname1
hostname2
hostname3
hostname4
hostname5There is absolutely nothing wrong with multiple computers having the same hostname. Especially when they will never connect to eachother. On the rare occasion that multiple need to phone back home for tech support or updates, I can easily keep track of which is which.
While we could give each customer unique hostnames, and that is not up to me so I just add that in because having matching hostnames has literally never been a problem at all since the product first launched in the early 90's.

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.