Jump to content

How to Customize the Bash Prompt for ssh

+ 1
  carlaschroder's Photo
Posted Oct 13 2009 12:47 PM

Sure, you know that the prompt changes to display the remote hostname when you're logged in via SSH. But it's just a dumb little black-and-white prompt, and it's easy to make mistakes, so you want a customized, colorful prompt to indicate when you have an active SSH login.

To accomplish this, customize the Bash prompt on the remote PCs. This example turns the prompt red and adds "ssh" to it.

Add these lines to the ~./bashrc for the remote account you want to log into:

if [ -n "$SSH_CLIENT" ]; then text=" ssh"

fi

export PS1='\[\e[0;31m\]\u@\h:\w${text}$\[\e[m\] '

When you log into this machine, the prompt will look like this, in red:

               carla@server06:~ssh $

Only the prompt is red; all the other text will be your normal shell colors.


Customizing the Bash prompt is practically a book topic in itself. The example in this recipe can easily be edited to suit your preferences. You don't have to use "ssh" or name the variable "text;" these can be anything you like. [\e[0;31m\] determines the text color—just change the numbers.

[\e[m\] turns off the colors, so that your commands and command output will return to the normal shell colors (Table A).

Table A. Bash prompt text colors

Black 0;30

Dark Gray 1;30

Blue 0;34

Light Blue 1;34

Green 0;32

Light Green 1;32

Cyan 0;36

Light Cyan 1;36

Red 0;31

Light Red 1;31

Purple 0;35

Light Purple 1;35

Brown 0;33

Yellow 1;33

Light Gray 0;37

White 1;37


This customization works by checking for the presence of the SSH_CLIENT environment variable. Then Bash knows to use the custom SSH prompt instead of the default prompt. To see this for yourself, log into a remote host via SSH, then run:

               user@remotehost$ echo $SSH_CLIENT

192.168.1.5 33232 22

This only works on the remote prompt. Running it at the local prompt will return a blank line:

               user@localhost$ echo $SSH_CLIENT

because once you complete an SSH login, the remote machine controls your local terminal, and the SSH client is running on the remote PC. That is why you have to customize ~/.bachrc on the remote hosts.

Linux Cookbook

Learn more about this topic from Linux Cookbook.

Linux information can be found scattered in man pages, texinfo files, and source code comments, but the best source is the experts who have built up a working knowledge of managing Linux systems. The Linux Cookbook's tested techniques distill years of hard-won experience into practical cut-and-paste solutions to everyday Linux dilemmas. Use just one recipe from this collection of real-world solutions, and the hours of tedious trial-and-error saved will more than pay for the cost of the book. It's more than a time-saver; it's a sanity saver.

See what you'll learn


Tags:
0 Subscribe


1 Reply

0
  hagen-cool's Photo
Posted Feb 13 2013 11:21 AM

Yes !
That it is, for what i was looking.
Thanks ! :)