Coloring terminals in Debian

"Please leave a message at the beep, we will get back to you when your support contract expires."

Moderators: phlip, Moderators General, Prelates

Kasperl
Posts: 135
Joined: Mon Jan 14, 2008 5:06 pm UTC

Coloring terminals in Debian

Postby Kasperl » Wed Apr 09, 2008 5:14 pm UTC

I've been using Debian happily on multiple systems lately, and soon I'll add even more systems to that list. I also use SSH a lot, and am stupid enough to leave root terminals open sometimes. This leaves the option open for 'shutdown -t now' not to cause a sudden lack of noise from the local server I thought I was rebooting, but simply 'connection lost', and a 10km bike ride to reboot the remote host I just powered off. To prevent this kind of thing, and stuff like accidentally checking out an SVN repo as root, I'm looking for a way to color-code my terminals, based on the host computer and being root or not. Either a different background color, different text, a bouncing animation or a boxing glove coming out of the monitor to hit me in the face before I accidentally delete stuff on the wrong PC.

Any ideas to make this work consistently on a server, desktop, laptop (EeePC), and remote hosts as well?
Just another forum newbie, asking all the stupid questions again.

coppro
Posts: 117
Joined: Mon Feb 04, 2008 6:04 am UTC

Re: Coloring terminals in Debian

Postby coppro » Wed Apr 09, 2008 6:11 pm UTC

You can modify your PS1 prompt value (in your ~/.bashrc, or perhaps your /etc/bash.bashrc) to make sure that it always contains the host name and the user name. Something like ">\u@\h:\w\$" (that's what I use) should do the trick.

zenten
Posts: 3799
Joined: Fri Jun 22, 2007 7:42 am UTC
Location: Ottawa, Canada

Re: Coloring terminals in Debian

Postby zenten » Wed Apr 09, 2008 6:54 pm UTC

Not that this is exactly fitting what you're asking for, but this is an excellent reason for using sudo instead of switching to root.

I'd probably just use a different terminal program for root stuff if I wasn't using sudo though.

User avatar
enk
Posts: 754
Joined: Mon Sep 10, 2007 12:20 am UTC
Location: Aalborg, Denmark
Contact:

Re: Coloring terminals in Debian

Postby enk » Wed Apr 09, 2008 6:56 pm UTC

Custom prompt strings rule.

To get a simple white-on-red terminal, stick this in your .bashrc

Code: Select all

if [ "$PS1" ]; then
export PS1="\[\e[37;41m\]\u@\h:\w\$ "
fi


More color stuff here: http://www.ibm.com/developerworks/linux/library/l-tip-prompt/
phlip wrote:Ha HA! Recycled emacs jokes.

Kasperl
Posts: 135
Joined: Mon Jan 14, 2008 5:06 pm UTC

Re: Coloring terminals in Debian

Postby Kasperl » Wed Apr 09, 2008 8:28 pm UTC

coppro wrote:You can modify your PS1 prompt value (in your ~/.bashrc, or perhaps your /etc/bash.bashrc) to make sure that it always contains the host name and the user name. Something like ">\u@\h:\w\$" (that's what I use) should do the trick.


The text is already there, I'm just enough of an idiot not to look at it. I was looking for the singing and dancing, punch-me-in-the-face kind of warning.

Enk: That looks great, thanks. Should I just edit the .bashrc file in the home dire for each user on each host?
Just another forum newbie, asking all the stupid questions again.

User avatar
enk
Posts: 754
Joined: Mon Sep 10, 2007 12:20 am UTC
Location: Aalborg, Denmark
Contact:

Re: Coloring terminals in Debian

Postby enk » Wed Apr 09, 2008 8:58 pm UTC

Kasperl wrote:Should I just edit the .bashrc file in the home dire for each user on each host?


You will have to do it on every server. If it should apply only to root shells, put it in /root/.bashrc. If it should apply to every shell on that server, use the global /etc/bashrc.
phlip wrote:Ha HA! Recycled emacs jokes.

User avatar
bridge
Posts: 195
Joined: Sun Feb 03, 2008 2:24 pm UTC
Location: Zurich < x < Rome

Re: Coloring terminals in Debian

Postby bridge » Wed Apr 09, 2008 10:40 pm UTC

Ansi color list may be helpful
Excuse my Super Mario accent

Posi
Posts: 111
Joined: Mon Jul 16, 2007 6:08 am UTC

Re: Coloring terminals in Debian

Postby Posi » Mon Apr 14, 2008 2:30 am UTC

Kasperl wrote:I've been using Debian happily on multiple systems lately, and soon I'll add even more systems to that list. I also use SSH a lot, and am stupid enough to leave root terminals open sometimes. This leaves the option open for 'shutdown -t now' not to cause a sudden lack of noise from the local server I thought I was rebooting, but simply 'connection lost', and a 10km bike ride to reboot the remote host I just powered off. To prevent this kind of thing, and stuff like accidentally checking out an SVN repo as root, I'm looking for a way to color-code my terminals, based on the host computer and being root or not. Either a different background color, different text, a bouncing animation or a boxing glove coming out of the monitor to hit me in the face before I accidentally delete stuff on the wrong PC.

Any ideas to make this work consistently on a server, desktop, laptop (EeePC), and remote hosts as well?

In the default Debian .bash_rc file has a colored prompt string that just isn't enabled by default. Open it up with a text editor, and search for PS1= . Under that it should there should be a line a few lines below that that is commented out and has a colored version of the string. Above it should be a comment that says something like "Uncomment the following line and comment the preceding for a colorized prompt". You will have to do this for each user. Then use the link bridge posted to set root to a different color (like red or something).

I'm not sure how to do it for different hosts.


Return to “The Help Desk”

Who is online

Users browsing this forum: No registered users and 15 guests