< Day Day Up > |
Recipe 17.10. Customizing the Bash Prompt for ssh17.10.1 ProblemSure, 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. 17.10.2 SolutionCustomize 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. 17.10.3 DiscussionCustomizing 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 17-1).
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. 17.10.4 See Also
|
< Day Day Up > |