Tuesday, July 12, 2011

Files and Directories 

Linux files are organised in logical fashion for ease of administration. File system acts as a large container contianing hundreds of directories. Linux follows FHS standard ( Filesystem Hierarchy Standard ). 

There are 2 types of file systems -- Disk based and memory based. 
Disk based filesystem are usually created on HDD. 
Memory based filesystem are usually virtual and gets destroys once the system reboots. 
There are 2 types of data : Static and Dynamic 
Static : Data which doesnot change on system like configuration files, binaries, device Files, kernel files, 
Dynamic : Data which keeps on changing log files, status files, temp files etc. 

Filesystem Hierarchy :  

                     /
                     |
 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+---
 |     |     |     |     |     |      |       |        |     |     |     |      |     |
bin sbin  usr lib  root boot home  tmp   var  opt  dev  mnt  etc  proc
             |                                                     |
             |                                                  +-------+
  +------+----+-----+-----+-----+-----+             |         |
  |        |    |     |           |     |     |               cdrom   floppy
include src  lib  share  bin   sbin  local
                    |                   |
               +----+----+         +-----+
               |    |       |           |     |
             info  man  doc       bin   sbin
                    |
                 +-----+
                 |     |
               man1...man9
 

A brief description of FHS ( Linux ):

/            -> "/" or "slash" contains all the other dirs in linux.
 |                          "Father of all directories".
 |
 |-- bin/        -> "/bin" contains compulsory binaries which all users
 |                          can use and without which linux will not work well.
 |
 |-- sbin/        -> "/sbin" contains compulsory binaries which only super
 |                          user can use and without which linux will not work.
 |
 |-- etc/        -> "/etc" contains all config files [ASCII/flat files]
 |
 |-- dev/        -> "/dev" contains all block and character special
 |                          device drivers.
 |
 |-- lib/        -> "/lib" contains all shared objects (.so) files.
 |                          Similar to (.dll) files in Windows.
 |
 |-- mnt/        -> "/mnt" contains dummpy dirs for mounting removable
 |   |                      devices.
 |   |
 |   |-- cdrom/        -> "/mnt/cdrom" is the mount point for CD-ROM drive.
 |   |
 |   `-- floppy/    -> "/mnt/floppy" is the mount point for floppy drive.
 |
 |-- opt/        -> "/opt" contains optional third party softwares.
 |
 |-- root/        -> "/root" is the home directory of super user.
 |
 |-- boot/        -> "/boot" contains all bootable files and the linux
 |                          kernel.
 |
 |-- home/        -> "/home" contains home directories of all users.
 |
 |-- proc/        -> "/proc" contains virtual file system i.e the linux
 |                          kernel in RAM
 |
 |-- tmp/        -> "/tmp" contains temporary files and directories.
 |                          It is like the scratch pad of the system.
 |                          Running apps use this for their temp files
 |
 |-- var/        -> "/var" contains variable files. eg. mail boxes of
 |                          users, log files.
 |
 |--lost+found/  --> This directory is used to hold up files that become orphan
 |                           after system crash. Orphan files are those who have lost 
 |                           there name. When filesystem is created its automatically 
 |                           created.If deleted can be recreated through mklost+found
 |
 |
 `-- usr/        -> "/usr" contains all installation files of linux.
     |                     
     |
     |-- bin/        -> "/usr/bin" contains optional binaries which all users
     |                      can use [3rd party binaries]
     |
     |-- sbin/        -> "/usr/sbin" contains optional binaries which only su-
     |                      per user can use  [3rd party binaries]
     |
     |-- local/                Program Files folder in Windows.
     |   |
     |   |-- bin/    -> "/usr/local/bin" contains third party binaries inst-
     |   |                  alled after installation, which all users can use.
     |   |
     |   `-- sbin/    -> "/usr/local/sbin" contains third party binaries inst-
     |                      alled after installation, which only super user can
     |                      use.
     |
     |-- include/    -> "/usr/include" - Mostly C header files, Assembler
     |                     
     |
     |-- src/        -> "/usr/src" contains the source code of the linux
     |                      kernel in a sub directory (linux-2.4.20-8) under it.
     |
     |-- lib/        -> "/usr/lib" contains all optional shared objects (.so)
     |                     
     `-- share/
         |
         |-- info/    -> "/usr/share/info" contains the Linux manual in HTML
         |
         |-- doc/    -> "/usr/share/doc/" contains the Linux Documentation
         |
         `-- man/    -> "/usr/share/man" contains the Linux Manual
         |
             `-- man1/
             `-- man2/
             `-- man3/
             `-- man4/
             `-- man5/
             `-- man6/
             `-- man7/
             `-- man8/
             `-- man9/

 
 


Overview of Linux :

Here we will try to cover topics like :

1. System Structure .
2. Logging in and Out.
3. Common Linux commands.

System Structures : 

It's basically divided into 3 parts : Kernel , Shell and The Directory Structure :

The Kernel : The kernel controls everything inside - out on a system that runs the linux OS.

The Shell : The shell is the interface between the user and the Kernel. User provides instructions to the shell, which are interperted and passed to the Kernel for processing.

Logging in and out :

Try logging using telnet and ssh to the server .

[root@instructor ~]# ssh instructor
root@instructor's password:
Last login: Tue Jul 12 15:09:34 2011 from 192.168.138.1

Common Linux Commands : 

Linux has many commands, we will try to cover as many as we can so that our daily administration task becomes very easy. 

Command Line ?? 
The command line refers to a linux command prompt where you enter commands for execution. 
Commands May or may not have arguments: 

Arguments ??
Arguments are used with commands for better, restricted or Enhanced output or for combination.

The basic syntax of command is : 

$ command argument1 argument 2 argument 3. 

===================================================
Command 01: ls
The ls commands displays list of files and directories and has many options attached to it which are mentioned below : 

ls -a            List hidden Files. 
ls -F            Displays file types. ( Shows ( / ) directories, ( * ) for executable,
                  (@ ) for symlink and nothing for text files.
ls -lh           Long listing with Filesize in Human Readable Format.
ls -l             used for checking permissions, link counts, size, date, time, etc.
ls -ld           To see permissions of a Directory. 
ls -R            To List the contents of of specified Directory and all its sub-dir.
ls -t             List all files sorted by Time and Date with newest file first.
ls -ltr           List all files sorted by Time and Date with Oldest file first.

----------------------------------------------------------------------------------------
[root@instructor ~]# ls -lt
total 56
drwxr-xr-x 2 root root  4096 Jul 12 15:08 Desktop
-rw------- 1 root root  1016 Jul 12 14:55 anaconda-ks.cfg
-rw-r--r-- 1 root root 29875 Jul 12 14:54 install.log
-rw-r--r-- 1 root root  3671 Jul 12 14:53 install.log.syslog

[root@instructor ~]# ls -ltr
total 56
-rw-r--r-- 1 root root  3671 Jul 12 14:53 install.log.syslog
-rw-r--r-- 1 root root 29875 Jul 12 14:54 install.log
-rw------- 1 root root  1016 Jul 12 14:55 anaconda-ks.cfg
drwxr-xr-x 2 root root  4096 Jul 12 15:08 Desktop

--------------------------------------------------------------------------------------
Command 02: pwd 
The pwd commands shows user's current working Directory. 

--------------------------------------------------------------------------------------
[root@instructor ~]# pwd
/root
-------------------------------------------------------------------------------------
Command 03: cd
The cd command is used to navigate the directory tree. 

cd /usr/bin        to change directory to /usr/bin.
cd                    to go back to home directory. 
cd ~                 to go back to home directory from anywhere. 
cd ..                 to go one directory up. 
cd -                  to switch between current and previous directory.

Command 04: tty
It displays the pseudo Terminal where you are logged in. 
-------------------------------------------------------------------------------------
[root@instructor ~]# tty
/dev/pts/1
-------------------------------------------------------------------------------------

Command 05: who
The who command displays information about all currently Logged users. 

Command 06:
The w command provide more details as compared to who cmd. 

Command 07:  whoami
Displays username of the user who executes this command. 

Command 08: logname 
The logname command show real user who logged in despite he has sudo's to switch his identity.

Command 09: id 
The id command displays a user's UID, username, GID, groupname and all secondary groups that a user is member. 
--------------------------------------------------------------------------------------
[root@instructor ~]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
-----------------------------------------------------------------------------------------

Command 10: groups 
The groups command displays all groups that a user is member of. 

Command 11: uname 
The uname command provides basic information about the system. 

uname -a            Complete system information. 
uname -s            Kernel name 
uname -n            Network node name 
uname -r             Kernel release. 
uname -m           Machine h/w name. 
uname -p            Processor type or unknown.
uname -i             H/W platform or unknown.

Command 12: hostname
It displays the system name. 

Command 13: clear
Clear the current screen

Command 14: date

Command 15: hwclock

Command 16: cal 

Command 17: uptime 

Command 18: which 
which command shows the absolute path of the command that will be executed if run without using the absolute path. 
---------------------------------------------------------------------------------------
[root@instructor ~]# which passwd
/usr/bin/passwd
---------------------------------------------------------------------------------------

Command 19: whereis 
whereis command displays the binary path, full path of command and man pages of the command.
---------------------------------------------------------------------------------------
[root@instructor ~]# whereis cat
cat: /bin/cat /usr/share/man/man1p/cat.1p.gz /usr/share/man/man1/cat.1.gz
----------------------------------------------------------------------------------------

Command 20: wc
wc commands displays number of lines, word and character contained in text file.
---------------------------------------------------------------------------------------
[root@instructor ~]# wc /etc/profile
  58  157 1029 /etc/profile

1st column indicates no of lines.
2nd column indicates no of words.
3rd column indicates no of character.
4th column indicates the file type.
--------------------------------------------------------------------------------------

Command 21: wall
Its used to broadcast the message to all the people logged into the server.


Command 22: man
man command is used for help and to obtain the information for any specific commands for which you are not completly aware.

Searching by keyword :
Sometimes you need to use a command but you dont know its name.
Then man -k and apropos command can help you find your command.

The man -k and apropos command, the search is performed in whatis database that contains details about commands and files, and it list all the man pages which include the specified keywords.

The whatis database is once in a day by /etc/cron.daily/makewhatis.cron script.
makewhatis command can be used to update whatis .
------------------------------------------------------------------------------------------
[root@instructor ~]# apropos password
chage                (1)  - change user password expiry information
chpasswd             (8)  - update passwords in batch mode
cracklib            (rpm) - A password-checking library.
crypt                (3)  - password and data encryption
------------------------------------------------------------------------------------------

man -f passwd 
passwd --help 
passwd -?












Sunday, July 10, 2011

DNS Server Interview Questions And Answers for linux admin


Q: - which are the important configuration files for DNS server ?
BIND uses /etc/named.conf as its main configuration file, the /etc/rndc.conf file as the
configuration file for name server control utility rndc, and the /var/named/ directory for zone files and the like.

Q: - What is BIND ?

BIND stands for Berkeley Internet Name Domain which is the most commonly used Domain Name System (DNS) server on the Internet.

Q: - On which version of bind u have worked ?

BIND 9

Q: - What is the role of DNS ?

A DNS server, or name server, is used to resolve an IP address to a hostname or vice versa.

Q: - On which port DNS server works ?

DNS servers use port 53 by default. Incoming and outgoing packets should be allowed on
port 53. Also allow connections on port 921 if you configure a lightweight resolver server.
The DNS control utility, rndc, connects to the DNS server with TCP port 953 by default. If
you are running rndc on the name server, connections on this TCP port from localhost
should be allowed. If you are running rndc on additional systems, allow connections to
port 953 (or whatever port you have chosen to configure) from these additional systems.
Q: - What is round robin DNS?

Round robin DNS is usually used for balancing the load of geographically distributed Web servers. For example, a company has one domain name and three identical home pages residing on three servers with three different IP addresses. When one user accesses the home page it will be sent to the first IP address. The second user who accesses the home page will be sent to the next IP address, and the third user will be sent to the third IP address. In each case, once the IP address is given out, it goes to the end of the list. The fourth user, therefore, will be sent to the first IP address, and so forth. 
Q: - What is Name Server?

A name server keeps information for the translation of domain names to IP addresses   and IP addresses to domain names. The name server is a program that performs the translation at the request of a resolver or another name server.
Q: - What is Primary name server or primary master server?

Primary name server/primary master is the main data source for the zone. It is the authoritative server for the zone. This server acquires data about its zone from databases saved on a local disk. The primary server must be published as an authoritative name server for the domain in the SOA resource record, while the primary master server does not need to be published.
Q: - What is Secondary name server/slave name server?

Secondary name server/slave name server acquires data about the zone by copying the data from the primary name server (respectively from the master server) at regular time intervals. It makes no sense to edit these databases on the secondary name servers, although they are saved on the local server disk because they will be rewritten during further copying.
Q: - what is Root name server?

Root name server is an authoritative name server for the root domain (for the dot). Each root name server is a primary server, which differentiates it from other name servers.
Q: - what is Stealth name server?

Stealth name server is a secret server. This type of name server is not published anywhere. It is only known to the servers that have its IP address statically listed in their configuration. It is an authoritative server. It acquires the data for the zone with the help of a zone transfer. It can be the main server for the zone. Stealth servers can be used as a local backup if the local servers are unavailable.

Q: - What do you mean by "Resource Records"?

Information on domain names and their IP addresses, as well as all the other information distributed via DNS is stored in the memory of name servers as Resource Records (RR). 

Q: - Explain "TTL"? 

Time to live. A 32-bit number indicating the time the particular RR can be kept valid in a server cache. When this time expires, the record has to be considered invalid. The value 0 keeps nonauthoritative servers from saving the RR to their cache memory.
Q: - Tell me 5 Types of DNS records?

A, NS, CNAME, SOA, PTR, MX.
Q:- explain "SOA Record"?

The Start of Authority (SOA) record determines the name server that is an authoritative source of information for the particular domain. There is always only one SOA record in the file, and it is placed at the beginning of the file of authoritative resource records. 
Q: - what is "A Record"

A (Address) records assign IP addresses to domain names of computers. The IP address cannot have a dot at the end.
Q: - Explain "CNAME Record"?

Synonyms to domain names can be created using CNAME records. This is often referred to as 'creating aliases for computer names'.
Q: - What are "HINFO and TXT Records"?

HINFO and TXT records are for information only. An HINFO record has two items in its data part. The first item is information about hardware, and the second one is information about software. A TXT record contains a general data string in its data part.
Example :
test.com IN SOA ...
...
mail IN A 192.1.1.2
IN HINFO My_Server UNIX
IN TXT my server
Q: - what are "MX Records"?

MX records specify the mailing server of the domain. An MX record shows to which computer a mail of a particular domain should be sent. The MX record also includes a priority number, which can be used to determine several computers where the mail for the domain can be sent. The first attempt is to deliver the mail to the computer with the highest priority (lowest value). If this attempt fails, the mail goes to the next computer (with a higher priority value), and so on.

test.com IN SOA ...
...
mail               IN        A         192.1.1.2
                       IN       HINFO    AlphaServer UNIX
                       IN        TXT       my  server
                       IN         MX   30    mail2.nextstep4it.com
                       IN         MX   20    mail3.nextstep4it.com
                       IN         MX   10    mail2.nextstep4it.com

Q: - Explain "PTR Records"?

A Pointer Record (PTR) is used to translate an IP address into a domain name.
Q: - What is Dynamic DNS?

Dynamic DNS a method of keeping a domain name linked to a changing IP address as not all computers use static IP addresses. Typically, when a user connects to the Internet, the user's ISP assigns an unused IP address from a pool of IP addresses, and this address is used only for the duration of that specific connection. This method of dynamically assigning addresses extends the usable pool of available IP addresses. A dynamic DNS service provider uses a special program that runs on the user's computer, contacting the DNS service each time the IP address provided by the ISP changes and subsequently updating the DNS database to reflect the change in IP address.
Q: - What is the role of "named-checkconf Utility"?

The named-checkconf utility checks the syntax of the named.conf configuration file.
Syntax: named-checkconf    [-t directory] [filename] 

Q: - what is the role of "named-checkzone Utility"?

The named-checkzone utility checks the syntax and consistency of the zone file.
Syntax:     named-checkzone [-dgv]   [-c class] zone   [filename]


Linux File Systems: Ext2 vs Ext3 vs Ext4


ext2, ext3 and ext4 are all filesystems created for Linux. This article explains the following:
  • High level difference between these filesystems.
  • How to create these filesystems.
  • How to convert from one filesystem type to another.

Ext2

  • Ext2 stands for second extended file system.
  • It was introduced in 1993. Developed by Rémy Card.
  • This was developed to overcome the limitation of the original ext file system.
  • Ext2 does not have journaling feature.
  • On flash drives, usb drives, ext2 is recommended, as it doesn’t need to do the over head of journaling.
  • Maximum individual file size can be from 16 GB to 2 TB
  • Overall ext2 file system size can be from 2 TB to 32 TB

Ext3

  • Ext3 stands for third extended file system.
  • It was introduced in 2001. Developed by Stephen Tweedie.
  • Starting from Linux Kernel 2.4.15 ext3 was available.
  • The main benefit of ext3 is that it allows journaling.
  • Journaling has a dedicated area in the file system, where all the changes are tracked. When the system crashes, the possibility of file system corruption is less because of journaling.
  • Maximum individual file size can be from 16 GB to 2 TB
  • Overall ext3 file system size can be from 2 TB to 32 TB
  • There are three types of journaling available in ext3 file system.
    • Journal – Metadata and content are saved in the journal.
    • Ordered – Only metadata is saved in the journal. Metadata are journaled only after writing the content to disk. This is the default.
    • Writeback – Only metadata is saved in the journal. Metadata might be journaled either before or after the content is written to the disk.
  • You can convert a ext2 file system to ext3 file system directly (without backup/restore).

Ext4

  • Ext4 stands for fourth extended file system.
  • It was introduced in 2008.
  • Starting from Linux Kernel 2.6.19 ext4 was available.
  • Supports huge individual file size and overall file system size.
  • Maximum individual file size can be from 16 GB to 16 TB
  • Overall maximum ext3 file system size is 1 EB (exabyte). 1 EB = 1024 PB (petabyte). 1 PB = 1024 TB (terabyte).
  • Directory can contain a maximum of 64,000 subdirectories (as opposed to 32,000 in ext3)
  • You can also mount an existing ext3 fs as ext4 fs (without having to upgrade it).
  • Several other new features are introduced in ext4: multiblock allocation, delayed allocation, journal checksum. fast fsck, etc. All you need to know is that these new features have improved the performance and reliability of the filesystem when compared to ext3.
  • In ext4, you also have the option of turning the journaling feature “off”.

Warning: Don’t execute any of the commands given below, if you don’t know what you are doing. You will lose your data!

Creating an ext2, or ext3, or ext4 filesystem

Once you’ve partitioned your hard disk using fdisk command, use mke2fs to create either ext2, ext3, or ext4 file system.
Create an ext2 file system:
mke2fs /dev/sda1
Create an ext3 file system:
mkfs.ext3 /dev/sda1

(or)

mke2fs –j /dev/sda1
Create an ext4 file system:
mkfs.ext4 /dev/sda1

(or)

mke2fs -t ext4 /dev/sda1

Converting ext2 to ext3

For example, if you are upgrading /dev/sda2 that is mounted as /home, from ext2 to ext3, do the following.
umount /dev/sda2

tune2fs -j /dev/sda2

mount /dev/sda2 /home
Note: You really don’t need to umount and mount it, as ext2 to ext3 conversion can happen on a live file system. But, I feel better doing the conversion offline.

Converting ext3 to ext4

If you are upgrading /dev/sda2 that is mounted as /home, from ext3 to ext4, do the following.
umount /dev/sda2

tune2fs -O extents,uninit_bg,dir_index /dev/sda2

e2fsck -pf /dev/sda2

mount /dev/sda2 /home
 
Again, try all of the above commands only on a test system, where you can afford to lose all your data.

15 Examples To Master Linux Command Line History

When you are using Linux command line frequently, using the history effectively can be a major productivity boost. In fact, once you have mastered the 15 examples that I've provided here, you’ll find using command line more enjoyable and fun.


1. Display timestamp using HISTTIMEFORMAT

Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.
# export HISTTIMEFORMAT='%F %T '
# history | more
1  2008-08-05 19:02:39 service network restart
2  2008-08-05 19:02:39 exit
3  2008-08-05 19:02:39 id
4  2008-08-05 19:02:39 cat /etc/redhat-release

2. Search the history using Control+R

I strongly believe, this may be your most frequently used feature of history. When you’ve already executed a very long command, you can simply search history using a keyword and re-execute the same command without having to type it fully. Press Control+R and type the keyword. In the following example, I searched for red, which displayed the previous command “cat /etc/redhat-release” in the history that contained the word red.

# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`red': cat /etc/redhat-release
[Note: Press enter when you see your command,
which will execute the command from the history]
# cat /etc/redhat-release
CentOS release 5.4 (Final)

Sometimes you want to edit a command from history before executing it.
For e.g. you can search for httpd, which will display service httpd stop 
from the command history, select this command and change the stop to start 
and re-execute it again as shown below.
 
# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`httpd': service httpd stop
[Note: Press either left arrow or right arrow key when you see your
command, which will display the command for you to edit, before executing it]
# service httpd start 
 

3. Repeat previous command quickly using 4 different methods

Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.
  1. Use the up arrow to view the previous command and press enter to execute it.
  2. Type !! and press enter from the command line
  3. Type !-1 and press enter from the command line.
  4. Press Control+P will display the previous command, press enter to execute it

4. Execute a specific command from history

In the following example, If you want to repeat the command #4, you can do !4 as shown below.
 
# history | more
1  service network restart
2  exit
3  id
4  cat /etc/redhat-release

# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)
 

5. Execute previous command that starts with a specific word

Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !ps and enter, executed the previous command starting with ps, which is ‘ps aux | grep firefox’.

# !ps
ps -ef |grep -i firefox
root      7851  8897  0 14:41 pts/4    00:00:00 grep -i firefox
root     18828     1  2 11:34 ?        00:03:52 /opt/firefox/firefox-bin
root     18870 18828  0 11:34 ?        00:01:51 /opt/firefox/plugin-container /usr/lib/flash-plugin/libflashplayer.so
18828 plugin true

6. Control the total number of lines in the history using HISTSIZE

Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 450 command will be stored in the bash history.
 
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450

7. Change the history file name using HISTFILE

By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file. I’m yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.

# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
 
If you have a good reason to change the name of the history file, please
share it with me, as I’m interested in finding out how you are using this feature. 

8. Eliminate the continuous repeated entry from history using HISTCONTROL

In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.

# pwd
# pwd
# pwd
# history | tail -4
44  pwd
45  pwd
46  pwd [Note that there are three pwd commands in history, after
executing pwd 3 times as shown above]
47  history | tail -4

# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
56  export HISTCONTROL=ignoredups
57  pwd [Note that there is only one pwd command in the history, even after
executing pwd 3 times as shown above]
58  history | tail -4


9. Erase duplicates across the whole history using HISTCONTROL

The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.

# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38  pwd
39  service httpd stop
40  history | tail -3

# ls -ltr
# service httpd stop
# history | tail -6
35  export HISTCONTROL=erasedups
36  pwd
37  history | tail -3
38  ls -ltr
39  service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40  history | tail -6

10. Force history not to remember a particular command using HISTCONTROL

When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history. It is good to understand how ignorespace works. But, as a best practice, don’t hide purposefully anything from history.

# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
#  service httpd stop [Note that there is a space at the beginning of service,
to ignore this command from history]
# history | tail -3
67  ls -ltr
68  pwd
69  history | tail -3

11. Clear all the previous history using option -c

Sometime you may want to clear all the previous history, but want to keep the history moving forward.
 
# history -c

12. Subtitute words from history commands

When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.
In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.

# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg

In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).
 
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi  !^
vi anaconda-ks.cfg

13. Substitute a specific argument for a specific command.

In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.
# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt
 
In the example below, !cp:$ searches for the previous command in history that 
starts with cp and takes the last argument (in this case, which is also the second 
argument as shown above) of cp and substitutes it for the ls -l command 
as shown below. 
# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt
 

14. Disable the usage of history using HISTSIZE

If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.
# export HISTSIZE=0
# history
# [Note that history did not display anything]
 

15. Ignore specific commands from the history using HISTIGNORE

Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.
 
# export HISTIGNORE="pwd:ls:ls -ltr:"
# pwd
# ls
# ls -ltr
# service httpd stop
# history | tail -3
79  export HISTIGNORE="pwd:ls:ls -ltr:"
80  service httpd stop
81  history
[Note that history did not record pwd, ls and ls -ltr]

 !Enjoyy..........