January 27, 2011

Bash Customization

Almost everyone after a fresh install makes some customizations to make the work on the server easier... here are some I like (yes, bash configuration ahead).



First history search. I'm quite a lazy person and don't like to type the same commands again and again, especially the long ones :) so I usually add this two lines to the inputrc file, which enables quick and simple history searches - just use meta+up arrow and meta+down arrow, meta is usually Alt. Sure you have ctrl+r for incremental reverse history search, but this solution is IMO better.

[root@cent:~]# vim /etc/inputrc

# for simple history search
"e[1;3A": history-search-backward
"e[1;3B": history-search-forward


After added to inputrc, write a beginning of a command and press alt+up arrow to browse the history.


Keeping track of BASH history is also important so you can set a huge HISTSIZE value and HISTCONTROL to ignoreboth, which will make bash ignore duplicates and commands beginning with a space won't be shown in history.

[root@cent:~]# vim .bash_profile
export HISTSIZE=10000
export HISTCONTROL=ignoreboth

The duplicates won't be in history:

[root@cent:~]# ls /var/www/html/
[root@cent:~]# ls /var/www/html/
[root@cent:~]# ls /var/www/html/
[root@cent:~]# history | tail -n3
1809 xm list
1810 ls /var/www/html/
1811 history | tail -n3

And if it begins with a space it won't show up in bash history at all.

[root@cent:~]#  cat /etc/shadow | grep root

[root@cent:~]# history | tail -n3
1809 xm list
1810 ls /var/www/html/
1811 history | tail -n3


Prompt color

Now some prompt eye candy... a lot of old-school admins really hate color prompts. The idea is that you should concentrate on the output and not on the prompt itself. So it's up to you, I like this prompt configuration for root and user:

[root@cent:~]# vim .bash_profile

export PS1="[[$(tput bold)][$(tput setaf 1)]u@h[$(tput sgr0)]:[$(tput setaf 3)]w[$(tput sgr0)[[$ "
[podlevak@cent:~]$ vim .bash_profile

export PS1="[[$(tput bold)][$(tput setaf 2)]u@h[$(tput sgr0)]:[$(tput setaf 6)]w[$(tput sgr0)[[$ "

btw also the full path will be shown, but you will need tput for this


If you want all new users to take advantage of those changes put it in:

[root@cent:~]# vim /etc/skel/.bash_profile



Now a lot of users hate this messages, I think they are fun... On some serious servers I used to put some legal notice in the motd file, like in the example below:

[root@cent:~]# vim /etc/motd

^[[32mWelcome to cent server !^[[0m

^[[31m* The use of this system is restricted to authorized users.
* Unauthorized access is forbidden and will be prosecuted by law.
* All information and communications on this system are subject
to review, monitoring and recording at any time, without
notice or permission.^[[0m

I didn't come up with it, it's a slightly changed and colored version I found on the Internet... IMO doing this has similar meaning as writing at end of your email footer this serious legal notice that you are not allowed to read the email if your are not the intended recipient and you go to hell if you do :).

Note: The symbol ^[ to print it with color is done in Vim by pressing ctrl+v+esc .



To be a little wiser after each login you can use fortunes. The command will print a quote, joke or whatever depending on your configuration.

[root@cent:~]# yum install fortune-mod

After it's installed you can test it, to list available fortunes use the -f option.

[root@cent:~]# fortune
A hacker does for love what others would not do for money.

[root@cent:~]# fortune -f

And to display a message after login, add it to your .bash_profile file.

[root@cent:~]# vim .bash_profile

echo -e "
$( fortune -e linux people drugs wisdom )

On my Ubuntu box I've used fortune together with cowsay.

root@crack:~# aptitude install fortune fortunes cowsay

To list all your cow files and print one use:

palo@crack:~$ ls /usr/share/cowsay/cows
palo@crack:~$ cowsay -f tux.cow $(fortune)

/ Arnold's Laws of Documentation: (1) If | it should exist, it doesn't. (2) If it |
| does exist, it's out of date. (3) Only |
| documentation for useless programs |
transcends the first two laws. /
|o_o |
|:_/ |
// (| | )
/'_ _/`

Something like this should do the trick in the bashrc file:

echo "$(cowsay -f tux.cow $(fortune))"


What changes and customizations do you make to new installed Linux severs ?

Click Here!