Wednesday, 16 May 2012

Cron in Linux

               Cron job are used to schedule commands to be executed periodically. You can setup commands or scripts, which will repeatedly run at a set time

#edit cron table
crontab -e


   *        *        *        *        *    command to be executed


* * * * *    USERNAME    command to be executed
|  |  | |  |
|  |  | |  |----- Day of week (0 - 6) (Sunday=0)
|  |  | |-------- Month (1 - 12)
|  |  |---------- Day of month (1 - 31)
|  |------------- Hour (0 - 23)
|---------------- Minute (0 - 59)

crontab -l
crontab -u username -l

crontab -r
crontab -r -u username

#run every 10 minutes
*/10 * * * * command

#stop receiving email output
0 3 * * * /root/ >/dev/null 2>&1

crontab -u root(username) -l > /home/cron.backup
0 12 * * * /usr/bin/top -n 1 -b -S >> /home/cron.backup

rpm -qa | grep cron
rpm -qil version

Keyword     Equivalent
@yearly      0 0 1 1 *
@daily        0 0 * * *
@hourly      0 * * * *
@reboot      Run at startup.

The asterisk (*) : specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month.

The comma (,) : specifies a list of values, for example: "1,5,10".

The dash (-) : specifies a range of values, for example: "5-10" days , which is equivalent to typing "5,6,7,8,9,10" using the comma operator.

NFS in Linux

                The Network File System (NFS) was developed to allow machines to mount a disk partition on a remote machine as if it were on a local hard drive. This allows for fast, seamless sharing of files across a network. It also gives the potential for unwanted people to access your hard drive over the network.

NFS Configration in RHEL 6/CentOS 6

yum install nfs*

rpm packeages

vim /etc/exports
/var/ftp/pub    *(ro,sync)    #for entire network
/data,sync)    #for particular machine
/jp,sync)    #for particular network

mkdir /data
mkdir /jp

service rpcbind status
rpcbind (pid 9905) is running...

service nfs restart
Shutting down NFS mountd:                                  [  OK  ]
Shutting down NFS daemon:                                 [  OK  ]
Shutting down NFS quotas:                                   [  OK  ]
Shutting down NFS services:                                 [  OK  ]
Starting NFS services:                                           [  OK  ]
Starting NFS quotas:                                             [  OK  ]
Starting NFS daemon:                                           [  OK  ]
Starting NFS mountd:                                            [  OK  ]

chkconfig rpcbind on
chkconfig nfs on

/var/ftp/pub    <world>

#see what the exported file system parameters look like
/usr/sbin/exportfs −v
exportfs -av

exporting *:/var/ftp/pub

showmount -e
/var/ftp/pub *

mount /mnt/

#verify nfs is running
rpcinfo −p localhost run on the server
rpcinfo −p servername run on the client

#to check allow and deny

exportfs −ra to force nfsd to re−read the /etc/exports file.

netstat −rn should show:
Destination    Gateway        Genmask        Flags    MSS Window   irtt Iface    U               0 0                0 eth0               

rpm -qa | grep -i nfs
rpm -qi nfs-utils
tracepath server(IP)
# strings /sbin/portmap | grep hosts

Monday, 14 May 2012

Linux Command for System Information

uname -r
To display the info more about kernel.

Memory info (in kilobytes).

df -h
(disk free) Print disk info about all the filesystems in human-readable form.

du / -bh | more
(=disk usage) Print detailed disk usage for each subdirectory starting at the "/" (root) directory (in human legible form).

cat /proc/cpuinfo
Cpu info--it show the content of the file cpuinfo.

cat /proc/interrupts
List the interrupts in use.

cat /proc/version
Linux version and other info

cat /proc/filesystems
Show the types of filesystems currently in use.

cat /etc/printcap
Show the setup of printers.

Show the kernel modules currently loaded.(As root. Use /sbin/lsmod to execute this command when you are a non-root user.)

Show the current user environment.

echo $PATH
Show the content of the environment variable "PATH". This command can be used to show other environment variables as well. Use "set" to see the full environment.

dmesg | less
Print kernel messages (the content of the so-called kernel ring buffer). Use less /var/log/dmesg  to see what "dmesg" dumped into this file right after the last system bootup.

Sunday, 13 May 2012

Kernel Version in Linux

uname --version

uname (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.

uname -r       

    2   : Kernel version
    6   : The major revision of the kernel
    32  : The minor revision of the kernel
    220 : Immediate fixing / bug fixing for critical error
    generic : Distribution specific sting. For example, Redhat appends string such as EL6 to indicate RHEL 6 kernel.

uname -a

Linux 2.6.32-220.13.1.el6.x86_64 #1 SMP Tue Apr 17 23:56:34 BST 2012 x86_64 x86_64 x86_64 GNU/Linux

uname -mrsn

Linux 2.6.32-220.13.1.el6.x86_64 x86_64

cat /proc/version

nux version 2.6.32-220.13.1.el6.x86_64( (gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) ) #1 SMP Tue Apr 17 23:56:34 BST 2012

rpm -q kernel


dpkg --list | grep linux-image    #in Ubuntu/Debain

ii  linux-image Generic Linux kernel image.
rc  linux-image-2.6.20-15-generic 2.6.20-15.27 Linux kernel image for version 2.6.20 on x86/
ii  linux-image-2.6.20-16-generic 2.6.20-16.32 Linux kernel image for version 2.6.20 on x86/
ii  linux-image-2.6.22-14-generic  2.6.22-14.47 Linux kernel image for version 2.6.22 on x86/

cat /etc/grub.conf
cat /proc/sys/kernel/osrelease
cat /etc/issue
lsb_release -a
cat /etc/lsb-release
also see uname man page

Friday, 11 May 2012

CentOS LAMP Install via YUM

CentOS Apache Install

yum install httpd

/etc/init.d/httpd start

chkconfig httpd on

CentOS MySQL Server Install

yum install mysql-server mysql

/etc/init.d/mysqld start

chkconfig mysqld on

CentOS PHP & php-mysql Install

To install PHP and php-mysql (php-mysql is required so that PHP can talk to MySQL)

yum install php php-mysql

FYI here is the full CentOS LAMP command (this will install Apache, MySQL & PHP)

yum install httpd php php-mysql mysql mysql-server

Useradd in Linux using script


# Script to add a user to Linux system

if [ $(id -u) -eq 0 ]; then

    read -p "Enter username : " username
    read -s -p "Enter password : " password

    egrep "^$username" /etc/passwd >/dev/null

    if [ $? -eq 0 ]; then

        echo "$username exists!"
        exit 1


        pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
        useradd -m -p $pass $username
        [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"

    echo "Only root may add a user to the system"
    exit 2

chmod 777

[root@snk Desktop]# ./
Enter username : testuser
Enter password : **********
User has been added to system!

Tuesday, 8 May 2012

File Permissions in Linux

              In LINUX Each file belongs to a user and to a user group, For restricting file access Linux defines three different types of rights,

Read(r)      - file can be read
Write(w)    - content of the file can be changed
Execute(x) - file can be executed

Each of these rights are defined for three sets of users,

user(u)     - the owner of the file
group(g)   - the users who are members of the group
others(o)  - neither members of the group nor the owner


ls -l testfile
-rw-r--r--. 1 annie team1 0 May  4 16:03 testfile

name           : testfile
permissions : -rw-r--r--
owner          : annie
group          : team1
other           : 0

The first character of permissions indicates,

Character   Type of file

   -           regular file
   d          directory
   l           symbolic link
   s          socket
   p          named pipe
   c          character device file (unbuffered)
   b          blocked device file (buffered)

Letter Permission

  r          Read
  w         Write
  x          Execute
  -          No permission

Letter Type of users

  u         User
  g         Group
  o         Other
  a         All (everybody)

Permission Value

  -                 0
  x                 1
  w                 2
  r                  4

Permission Value

    ---            0
    --x            1
    -w-            2
    -wx           3
    r--             4
    r-x            5
    rw-            6
    rwx           7

Changing the access mod of a file


chmod 752 testfile
chmod u=rwx,g=rx,o=w test

chmod u+x testfile   -user execute permission
chmod +x testfile    -everyone execute permission
chmod ugo+x testfile -everyone execute permission

Changing file owner or group


chown glen testfile
chgrp admin testfile
chown glen:admon testfile

Special permissions for executables

Setting the sticky bit on a directory

If you have a look at the /tmp permissions,

drwxrwxrwt   10 root root  4096 2006-03-10 12:40 tmp

t is called the sticky bit and indicates that in this directory, files can only be deleted by their owners, the owner of the directory or the root superuser. it is not enough for a user to have write permission on /tmp, he also needs to be the owner of the file to be able to delete it.

In order to set or to remove the sticky bit,

chmod +t tmp
chmod -t tmp

SGID attribute on a directory

chmod g+s directory
chmod g-s directory

Setting SUID and SGID attributes on executable files 

chmod g+s
chmod g-s

chmod u+s
chmod u-s

Setting the default file creation permissions

The default umask value is usually 022.

umask 022

By default umask is 000, files get mode 666 and directories get mode 777. As a result, with a default umask value of 022, newly created files get a default mode 644 (666 - 022 = 644) and directories get a default mode 755 (777 - 022 = 755).

Friday, 4 May 2012

Simple Backup & Compression in Linux

Compression reduces file to a fraction of its original size so the file can more efficiently store or transmit.

gzip -c test.txt > test.txt.gz #compress
gunzip test.txt.gz                #decompress
zcat test.txt.gz                    #view
less test.txt.gz                     #view pagewise

bzi2 -c test.txt > test.txt.bz2 #compress
bunzip2 test.txt.bz2              #decompress
bzcat test.txt.bz2                  #view
less test.txt.bz2                     #view pagewise

zip test.txt #compress
unzip test.txt             #decompress


tar -cvf filename.tar testfile

#backup with zip
tar -czvf filename.tar.gz testfile

#backup with gzip
tar -cjvf filename.tar.bz2 testfile

#uncompress tarfile
tar -xvf filename.tar

#uncompress .bz tarfile
tar -xjvf filename.tar.bz2

#uncompress .gz tarfile
tar -xzvf filename.tar.gz

c -create
v -verbose
x -extrat
f -filename
z -compress the tar file with gzip
j -compress the tar file with bzip2


Thursday, 3 May 2012


The Linux Logical Volume Manager (LVM) is a mechanism for virtualizing disks. It can create "virtual" disk partitions out of one or more physical hard drives, allowing you to grow, shrink, or move those partitions from drive to drive as your needs change. It also allows you to create larger partitions than you could achieve with a single drive.


#Create Partitions
fdisk /dev/sdc
make 3 partitions
change system ID as 8e for Linux LVM

#Create physical volumes
pvcreate /dev/sdc1 /dev/sdc2

Physical volume "/dev/sdc1" successfully created
Physical volume "/dev/sdc2" successfully created


#Create Virtual Group
vgcreate vg0 /dev/sdc1 /dev/sdc2

Volume group "vg0" successfully created


#Create Logical Volumes
lvcreate -L 2G Vg0 -n lv0

Logical volume "lv0" created

#Create File system on logical volumes
mkfs.ext3 /dev/vg0/lv0

#Edit /etc/fstab

/dev/vg0/lv0 /lvm ext3 defaults 0 0

#Mount logical volumes
mkdir /lvm
mount -a

lv0     vg0      -wi-ao  2.00g

#Extend logical volume
pvcreate /dev/sdc3
Physical volume "/dev/sdc3" successfully created

vgextend vg0 /dev/sdc3
Volume group "vg0" successfully extended

lvextend -L +1G /dev/vg0/lv0
Extending logical volume lv0 to 3.00 GiB
Logical volume lv0 successfully resized

resize2fs -p /dev/vg0/lv0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv0 is mounted on /lvm; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv0 to 786432 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 786432 blocks long.

lv0     vg0      -wi-ao  3.00g

#Reduce logical volume
lvreduce -L -500M /dev/vg0/lv0
WARNING: Reducing active and open logical volume to 2.51 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
Reducing logical volume lv0 to 2.51 GiB
Logical volume lv0 successfully resized

resize2fs -p /dev/vg0/lv0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv0 is mounted on /lvm; on-line resizing required
On-line shrinking from 786432 to 658432 not supported.

lv0     vg0      -wi-ao  2.51g

#Remove logical volume
vim /etc/fstab (edit fstab to dd lvm entry line)
mount -a
umount /lvm

lvremove /dev/Vg0/lv0
Do you really want to remove active logical volume lv0? [y/n]: y
Logical volume "lv0" successfully removed

Vgremove vg0
Volume group "vg0" successfully removed

Wednesday, 2 May 2012


                  Redundant Array of Independent Disks, originally Redundant Array of Inexpensive Disks is a storage technology that combines multiple disk drive components into a logical unit.

Disk arrays stripe data across multiple disks and access them in parallel to achieve:

Higher data transfer rates on large data accesses and
Higher I/O rates on small data accesses.

Level     Description

RAID 0 Block-level striping without parity or mirroring.

RAID 1 Mirroring without parity or striping.

RAID 2 Bit-level striping with dedicated Hamming-code parity.

RAID 3    Byte-level striping with dedicated parity.

RAID 4 Block-level striping with dedicated parity.

RAID 5 Block-level striping with distributed parity.

RAID 6 Block-level striping with double distributed parity.

RAID 10 Nested RAIDs or hybrid RAIDs are constructed by distributing data over mirrored sets of storage devices.

Mirroring plus striping (striped-mirror, RAID-1+0 or RAID-10)

            The combination of striping above mirroring is called a striped-mirror layout. Putting mirroring below striping mirrors each column of the stripe. If there are multiple sub disks per column, each subdisk can be mirrored individually instead of each column. A striped-mirror volume is an example of a layered volume. 


Level Description Minimum # of disks
RAID 0+1     Top Level RAID 1, Bottom Level RAID 0.    3
RAID 1+0     Top Level RAID 0, Bottom Level RAID 1. 4
RAID 5+0 Top Level RAID 0, Bottom Level RAID 5.    6
RAID 5+1 Top Level RAID 1, Bottom Level RAID 5. 6
RAID 6+0     Top Level RAID 0, Bottom Level RAID 6.    8
RAID 6+1     Top Level RAID 1, Bottom Level RAID 6.    8
RAID 1+0+0   RAID 0 RAID 0 RAID 1 8

Software RAID - the RAID task runs on the CPU of your computer system.

Hardware RAID - solution has its own processor and memory to run the RAID application.


The minimum number of software RAID partitions required for each RAID level:
        • RAID   0,1,10   – 2 partitions
        • RAID   4,5        – 3 partitions
        • RAID   6           – 4 partitions

#create partitions
fdisk /dev/sdb
Make 4 partitions of each 1GB
Change System ID as fd to Linux raid autodetect

#create raid
mdadm -C /dev/md0 -l 5 -n 3 /dev/sdb1 /dev/sdb2 /dev/sdb3
5-raid level
3-raid devices

mkfs.ext4 /dev/md0
mkdir /raid5

vim /etc/fstab
/dev/md0 /raid5 ext4 defaults 0 0

cd /raid5/

#raid status
mdadm --detail /dev/md0
cat /proc/mkstat

#set faulty partition
mdadm -f /dev/md0 /dev/sdb3
mdadm: set /dev/sdb3 faulty in /dev/md0

#add new raid partition
mdadm -a /dev/md0 /dev/sdb4
mdadm: added /dev/sdb4

#remove faulty raid partition
mdadm -r /dev/md0 /dev/sdb3
mdadm: hot removed /dev/sdb3 from /dev/md0