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
| |
| |
| |
| |
| |
| |
| |
| |
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 22This only works on the remote prompt. Running it at the local prompt will return a blank line:
user@localhost$ echo $SSH_CLIENTbecause 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.
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.

Help

