Tuesday, January 8, 2013

/etc/passwd and /etc/shadow


Whenever a new user account is created in Linux system, all information pertaining to that user is stored in /etc/passwd file, except for the encrypted password of the user. In spite of no password being stored in /etc/passwd file, it is called as "passwd" file because in earlier days password of the user used to be stored in this file.

So where is the user password stored? The encrypted user password is stored in /etc/shadow file. This is the control file used by /usr/bin/passwd command to check the legitimacy of user password.

Let us demonstrate how the entry looks for user "foo" in /etc/passwd and /etc/shadow


/etc/passwd file has seven fields as follows

Username:Password:UID:GID:Comment:Home Directory:Login Shell

foo:x:500:500::/home/foo:/bin/bash

For user foo, we observe that in the second field - password field, no encrypted password is stored and just contains an x.

For every line in /etc/passwd file, there exists a corresponding entry in /etc/shadow file. So for user "foo", the encrypted password of the user is stored in /etc/shadow file as follows

foo:$6$NVajcTOy$UT0RSwmUzXcr7ulUbyWKbjSmY/w2WbIe1N0leYq50iDlRYPcu.ozDsynkJZGNWn/rGrK5u5tgyZW4FHHG/mf0.:15454:0:99999:7:::

$6$NVajcTOy$UT0RSwmUzXcr7ulUbyWKbjSmY/w2WbIe1N0leYq50iDlRYPcu.ozDsynkJZGNWn/rGrK5u5tgyZW4FHHG/mf0. is the encrypted password of the user foo

How to determine the runlevel in Linux

To determine the linux runlevel in which we are working, we can use the commands who -r or runlevel


$ runlevel
N 5

$ who -r
run-level 5  Jan  8 18:25

fsck : file system consistency check


fsck is used to fix damaged file systems or inconsistent file systems. It is generally run when a filesystem fails to mount. In Linux, filesystems are usually marked "dirty" or "clean" and fsck checks only the dirty filesystems during the next startup.

Before running fsck, the file system should be unmounted. file system cannot be repaired while running. To repair root partition, the system should be booted into single  user mode.

fsck format is - fsck -t <File System Type>  <File System Mount/Device>

The <File System Mount/Device> can be specified by any of the following

  1. A device name (e.g /dev/sda1, /dev/mapper/VolGroup-logical_volume_vz)
  2. A mount point (e.g. /var, /opt, /home)
  3. A label (e.g. LABEL=home)
  4. UUID specifier (e.g. UUID=8868abf6-88c5-4a83-98b8-bfc24057f7bd )
If no filesystems are specified on the command line, and the -A  option is not  specified,  fsck  will  default  to  checking  filesystems  in /etc/fstab serially.  This is equivalent to the -As options.


The exit code returned by fsck is the sum of the following conditions:

  • 0     -  No errors
  • 1     -  File system errors corrected
  • 2     -  System should be rebooted
  • 4     -  File system errors left uncorrected
  • 8     -  Operational error
  • 16   - Usage or syntax error
  • 32   - Fsck canceled by user request
  • 128 - Shared library error
Generally fsck is run automatically at boot time if the file system becomes inconsistent due to non-graceful shutdown such as crash or power loss.

In /etc/fstab we can specify the order in which fsck should be run on partitions.

UUID=fffff7aa-57b8-40aa-baa4-588c4eff7651         /          ext4      defaults        1  1
UUID=8b5a0a93-1dd3-4394-bb3e-0032a77201fa   /boot    ext4      defaults        1  2
/dev/VolGroup/logical_volume_vz                           /vz        ext4      defaults        0  2
/dev/VolGroup/logical_volume_music                     /music   ext4      defaults        0  2
/dev/VolGroup/logical_volume_opt                         /opt        ext4      defaults        0  2 

The last column (6th column marked in Green) is the fsck option.
  • 0 = Do not check.
  • 1 = First file system (partition) to check; / (root partition) should be set to 1.
  • 2 = All other filesystems to be checked.

Let us see how to run fsck on an ext4 filesystem


1) First determine the filesystem type from /etc/fstab or mount command, like one as follows

           /dev/mapper/VolGroup-logical_volume_opt    on  /opt   type ext4 (rw)

2) This filesystem need to be unmounted. Before unmounting check if the filesystem in being used by any other process. This can be done using either fuser or lsof command

           [root@dhcppc5 ~]# fuser -m /opt
           /opt:                 1474m  7785c

    -m     name specifies a file on a mounted file system or a block device that is mounted.

So there are two processes - 1474 and 7785, using the /opt  mount point.

lsof /opt too shall help in identifying the processes using /opt partition.

The identified processes - 1474 and 7785 can be stopped gracefully.
Else the identified processes can killed forcefully as follows

fuser -km /opt kills all processes accessing the file system /opt in any way.

[root@dhcppc5 ~]# fuser -km /opt
/opt:                 1474m  7785c  8062c  8065c

[root@dhcppc5 ~]# fuser -m /opt
[root@dhcppc5 ~]# 

After the processes using the mount point are stopped, unmount the filesystem

[root@dhcppc5 ~]# umount  /opt

3) Now run fsck on  /opt

[root@dhcppc5 ~]# fsck -t ext4 /dev/mapper/VolGroup-logical_volume_opt
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/mapper/VolGroup-logical_volume_opt: clean, 803/327680 files, 61242/1310720 blocks
You have new mail in /var/spool/mail/root

By default fsck tries to skip the clean file system to do a quicker job.
That's what happened in the above case. However, we can force fsck to check the filesystem using -f option

[root@dhcppc5 ~]# fsck -t ext4 /dev/mapper/VolGroup-logical_volume_opt -f
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/VolGroup-logical_volume_opt: 803/327680 files (0.9% non-contiguous), 61242/1310720 blocks

Thus we observe that fsck is a five stage process.


How to force fsck on the next reboot


Method 1:

Just touch forcefsck file on the partition on which fsck need to be run on next reboot.

Say, I want to run fsck on root(/) partition on next reboot. Let us demonstrate as follows.

As superuser(root), change to / directory
# cd /

Create a file called forcefsck
# touch /forcefsck

Reboot the system
# reboot

The file forcefsck will be removed automatically when the process is finished. 

Method 2:

Using shutdown command with -F option will force fsck on reboot

# shutdown -rF now

Checking fsck Boot frequency


fsck is set to run after every "n" boots. To know how many boots have passed since last fsck check, run the following

# dumpe2fs -h /dev/sda2 | grep -i 'mount count'
dumpe2fs 1.41.12 (17-May-2010)
Mount count:              2
Maximum mount count:      -1 

# tune2fs -l /dev/sda2 | grep -i count | grep -i mount
Mount count:              2
Maximum mount count:      -1

To change the frequency of fsck checks - say, run fsck every 20 boots

# tune2fs -c 20 /dev/sda2

Howto bypass/skip fsck?


Using shutdown command as follows

# shutdown -rf now

Set boot without fsck using Linux kernel option - /boot/grub/grub.conf

Put "fastboot" at the end of kernel line, like follows

kernel /vmlinuz-2.6.32-042stab068.8 ro root=UUID=fffff7aa-57b8-40aa-baa4-588c4eff7651 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM fastboot

Skip fsck from /etc/fstab file

Set the last column(6th column) to 0 in /etc/fstab, so that the file system shall not be checked

/dev/VolGroup/logical_volume_opt    /opt       ext4      defaults      0 0

Using tune2fs

# tune2fs -i 0 /dev/sda2

Sunday, January 6, 2013

Difference between {} and () in shell scripting


Whenever shell executes a command or shell script, a sub-shell is spawned, which in turn executes the commands.

To group multiple commands, shell uses two types of operators : () and {}

The commands enclosed within () parentheses are executed in a sub-shell.
The commands enclosed within {} curly braces are executed in the current shell only.

Let us demonstrate () with following example

$ pwd
/Users/foo

$ ( cd python ; pwd )
/Users/foo/python

$ pwd
/Users/foo

Working from sub-shell - (), cd changed the working directory to /Users/foo/python.
The parent shell however remained at /Users/foo

Now let us demonstrate {}

$ pwd
/Users/foo

$ { cd python ; pwd ; }
/Users/foo/python

$ pwd
/Users/foo/python

In this case no new shell was spawned.

Note : While using curly braces {}, precede the closing brace with ;. Else you shall face problem like this

${ cd python ; pwd }
> c
> quit
> ;
-bash: syntax error near unexpected token `;'

Saturday, January 5, 2013

VIM : splitting the screen into multiple windows


While using vim, a screen can be split into multiple windows.
The window can be empty or a copy of the same file.

To view the same file in two separate windows, use the following in ex mode

:sp     Splits existing window in two

In this case changes made in one window shall be also seen in the other window.

To create a new window which is not associated with any file

:new       New blank window

To move between windows

[Ctrl-w][Ctrl-w]

To make the current window the only window on the screen and close the other window, use

:on     Removes the other window

Friday, January 4, 2013

for loop in shell scripting


Let us see how to use for loop in shell scripting

Say we want to print "n" numbers.

for i in 1 2 3 4 5 6 7 8 9 10
do
   echo $i
done

But, if we need to print 50 numbers, then it becomes a pain if we use the above method. So a simpler method is

for i in $(seq 50)
do
    echo $i
done

'seq' is part of the GNU "coreutils" package

$ which seq

/usr/bin/seq


$ rpm -qf /usr/bin/seq
coreutils-5.97-23.el5



Cricket score from command line using curl and wget

rediff.com provides the latest cricket score in the url

http://livechat.rediff.com/sports/score/score.txt

Just access the above url from command line as follows, using either curl or wget

  • curl -s http://livechat.rediff.com/sports/score/score.txt


  •  wget --quiet -O - http://livechat.rediff.com/sports/score/score.txt