How Do You Make the Bash Prompt Change Colors When Logged Into a Server?

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

If you are someone who access multiple servers throughout the work day, in that case being able to tell which one you are working currently with just at a glance based on a color scheme can be really useful.

The Remote Bash Prompts are set by remote ~/.bashrc, so you will still need to copy it to the remote servers. But, you can use a single ~/.bashrc for all hosts and set the Bash Prompt color depending on the host name:

changing-the-color-of-bash

Notes

  • Do not set the PS1 if it is not set already (which means that, if the shell is not interactive). Just testing to see if the PS1 is “non-empty” is a common way to find out if the shell is interactive or not and we don’t want to confuse programs that do that (that means a more accurate test is checking to see if $- contains i).
  • If you want this code to run when you are logging into a remote server, you should have one of these profile files always source ~/.bashrc (Here I am assuming you already know that).
  • In PS1, the escape codes must be enclosed with in \[…\].
  • \[033[m resets the foreground and background to their default values, so here:\w appears in the terminal background/foreground.
  • \[033[48;5;XXXm\033[38;5;YYYm sets background/foreground to XXX/YYY.
  • For a script that dumps the available colors, try colortest.
  • To check and see what the Bash Prompt would look like, use the: echo -e “<\033[48;5;16m\033[38;5;196mhost\033[m:dir>$ “
The following two tabs change content below.

William Fieldhouse

I currently work full time as a penetration tester and have been involved within the IT security industry for over a decade. I also love to pioneer any forms of new technology and ideologies for future advancements. Feel free to contact me at [email protected]

William Fieldhouse

I currently work full time as a penetration tester and have been involved within the IT security industry for over a decade. I also love to pioneer any forms of new technology and ideologies for future advancements. Feel free to contact me at [email protected]

Leave a Reply