Total Pageviews

Sunday, 6 April 2014

Configuring bash shell !!!

Let us play with bash shell, Usually admins like us prefer working with bash shell. It is more comfortable shell to make things simple.
Now let us configure bash shell in such a way that it provides much more flexibility.

Below are the main files for configuring bash shell...

[root@mysrv1 ~]# ls -lrth .bash*
-rw-r--r--. 1 root root  176 May 20  2009 .bash_profile
-rw-r--r--. 1 root root   17 Apr  6 04:30 .bash_logout
-rw-------. 1 root root 1.1K Apr  6 04:36 .bash_history
-rw-r--r--. 1 root root  211 Apr  6 04:43 .bashrc
[root@mysrv1 ~]#

.bash_profile    --- When we invoke the bash shell, it first attempts to read and execute commands                                 from this file.
.bash_logout     --- When we exits from bash shell, it read and execute commands from this file.
.bash_history    --- This file maintains the command history in bash shell.
.bashrc              --- In this file we can specify our own aliases and functions.

Usually for temporary login prompt we can give ,

[root@mysrv1 ~]# PS1='MY SERVER# '
MY SERVER#
MY SERVER#
MY SERVER# exit
logout
[root@mysrv1 ~]#
[root@mysrv1 ~]#              --- soon after exiting from the shell,changes in prompt will be reverted

But to maintain a particular prompt for a user we need to edit his .bashrc file so that whenever he invokes a shell mentioned prompt will be displayed.

[root@mysrv1 ~]# vi .bashrc
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
PS1='\h@root #'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
[root@mysrv1 ~]#

Now to observe the change take a new session,

login as: root
root@192.168.18.133's password:
Last login: Sun Apr  6 04:40:30 2014 from 192.168.18.1
mysrv1@root #
mysrv1@root #
mysrv1@root #

So from now onwards root user's login prompt will be the displayed output.

Now let us see the other use of .bashrc file.This time I will create new alias for different commands for ease purpose.

[root@mysrv1 ~]# alias                           --- cmd to see defined aliases
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@mysrv1 ~]#

[root@mysrv1 ~]#
[root@mysrv1 ~]# alias c=clear
[root@mysrv1 ~]#
[root@mysrv1 ~]# c

[root@mysrv1 ~]#
[root@mysrv1 ~]#                    ------ Now the screen is cleared
[root@mysrv1 ~]#

[root@mysrv1 ~]# exit            ------- exit from current session
exit
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]# c
-bash: c: command not found      ------- this time my alias won't work, since it is set temporary.
[root@mysrv1 ~]#

So now to set a alias permanently we need to give alias in .bashrc file.

[root@mysrv1 ~]# bash
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]# vi .bashrc
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias c='clear'                 --- c is for clear screen
alias i='ifconfig'              --- i is for ifconfig

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
[root@mysrv1 ~]#

Let us try checking it...

[root@mysrv1 ~]# su -
[root@mysrv1 ~]#
[root@mysrv1 ~]# c            --- for c screen is cleared...
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]#

[root@mysrv1 ~]# i            --- for i ifconfig is executed...
eth0      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:DD
          inet addr:192.168.18.133  Bcast:192.168.18.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4f:66dd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4624 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2877 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:436891 (426.6 KiB)  TX bytes:457876 (447.1 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:E7
          inet addr:192.168.18.134  Bcast:192.168.18.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4f:66e7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:514 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:49306 (48.1 KiB)  TX bytes:2518 (2.4 KiB)

eth2      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:F1
          inet addr:192.168.18.132  Bcast:192.168.18.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4f:66f1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:513 errors:0 dropped:0 overruns:0 frame:0
          TX packets:21 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:49246 (48.0 KiB)  TX bytes:2333 (2.2 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:14 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:822 (822.0 b)  TX bytes:822 (822.0 b)

[root@mysrv1 ~]#

I will exit from bash and again I will invoke bash shell to check....

[root@mysrv1 ~]# exit
logout
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]# i
bash: i: command not found
[root@mysrv1 ~]# bash
[root@mysrv1 ~]#
[root@mysrv1 ~]# i
eth0      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:DD
          inet addr:192.168.18.133  Bcast:192.168.18.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4f:66dd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4666 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2907 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:440409 (430.0 KiB)  TX bytes:463064 (452.2 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:E7
          inet addr:192.168.18.134  Bcast:192.168.18.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4f:66e7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:515 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:49366 (48.2 KiB)  TX bytes:2518 (2.4 KiB)

eth2      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:F1
          inet addr:192.168.18.132  Bcast:192.168.18.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4f:66f1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:514 errors:0 dropped:0 overruns:0 frame:0
          TX packets:21 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:49306 (48.1 KiB)  TX bytes:2333 (2.2 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:14 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:822 (822.0 b)  TX bytes:822 (822.0 b)

[root@mysrv1 ~]#
[root@mysrv1 ~]# c
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]#   --- So even after logging into a new session of bash we can able to use alias....
[root@mysrv1 ~]#

.bash_profile is starting file which it will read and execute when we invoke bash shell.
Observe the content of .bash_profile file , it internally executes .bashrc file and its 
commands like PATH...

[root@mysrv1 ~]# cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin                ---- Here we can export EDITOR by giving EDITOR=vi

export PATH
[root@mysrv1 ~]#

.bash_history maintains history of commands that are used in bash shell...

[root@mysrv1 ~]# tail .bash_history
bash
vi .bash_logout
bash
cd /etc/
ls -lrth isuue*
ls -lrth issue*
c
bash
[root@mysrv1 ~]#

.bash_logout file is useful utility of bash shell with the which help of which we can take 
backup of some files and output of some commands automatically before exiting from 
bash shell.This file gets executed when we exit from bash shell.

[root@mysrv1 ~]#
[root@mysrv1 ~]# cat .bash_logout
# ~/.bash_logout
[root@mysrv1 ~]#
[root@mysrv1 ~]# vi .bash_logout
[root@mysrv1 ~]# 
[root@mysrv1 ~]#
[root@mysrv1 ~]# cat .bash_logout
# ~/.bash_logout
cp /etc/passwd /etc/passwd_old                 --- file which I need backup.
ls -lrth >>/tmp/oo                                       --- command which I need output.
[root@mysrv1 ~]#
[root@mysrv1 ~]# ls -lrth
total 96K
-rw-r--r--. 1 root root 9.0K Mar 15 14:37 install.log.syslog
-rw-r--r--. 1 root root  39K Mar 15 14:41 install.log
-rw-------. 1 root root 2.3K Mar 15 14:41 anaconda-ks.cfg
drwxr-xr-x. 3 root root 4.0K Apr  4 03:34 test
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Templates
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Public
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Downloads
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Videos
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Pictures
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Music
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Documents
drwxr-xr-x. 2 root root 4.0K Apr  5 05:35 Desktop
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]# ls -lrth /etc/passwd*                      --- now there is no passwd_old file
-rw-r--r--. 1 root root 1.4K Mar 15 14:42 /etc/passwd-
-rw-r--r--. 1 root root 1.4K Mar 15 14:42 /etc/passwd.OLD
-rw-r--r--. 1 root root 1.4K Mar 15 14:42 /etc/passwd
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]# cat /tmp/oo                                     --- now there is no /tmp/oo file
cat: /tmp/oo: No such file or directory
[root@mysrv1 ~]#

Now I will exit from my bash shell to check my new .bash_logout file

[root@mysrv1 ~]# exit
[root@mysrv1 ~]#
[root@mysrv1 ~]# logout
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# su -
[root@mysrv1 ~]#
[root@mysrv1 ~]# cat /tmp/oo               ----- /tmp/oo is created and grabbed the output of ls -lrth
total 96K
-rw-r--r--. 1 root root 9.0K Mar 15 14:37 install.log.syslog
-rw-r--r--. 1 root root  39K Mar 15 14:41 install.log
-rw-------. 1 root root 2.3K Mar 15 14:41 anaconda-ks.cfg
drwxr-xr-x. 3 root root 4.0K Apr  4 03:34 test
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Templates
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Public
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Downloads
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Videos
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Pictures
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Music
drwxr-xr-x. 2 root root 4.0K Apr  4 03:57 Documents
drwxr-xr-x. 2 root root 4.0K Apr  5 05:35 Desktop
[root@mysrv1 ~]#
[root@mysrv1 ~]#
[root@mysrv1 ~]# ls -lrth /etc/passwd*
-rw-r--r--. 1 root root 1.4K Mar 15 14:42 /etc/passwd-
-rw-r--r--. 1 root root 1.4K Mar 15 14:42 /etc/passwd.OLD
-rw-r--r--. 1 root root 1.4K Mar 15 14:42 /etc/passwd
-rw-r--r--. 1 root root 1.4K Apr  6 05:42 /etc/passwd_old    ----- backup of passwd file is taken
[root@mysrv1 ~]#
[root@mysrv1 ~]#

Thus We can configure a bash shell as per our requirement to make our login shell much more convenient and tasks can be performed much more easier.

################################################################################

No comments:

Post a Comment