Skip to content

Posts from the ‘Linux’ Category


Clear dns cache in Linux / Unix machine

Most DNS clients will cache the results of name resolution request to speed up multiple lookups to the same URL. Just think about how many requests are made to the same domain when visiting a single web page. Every file, image, style sheet, etc. that is on that page and served from the same domain requires a DNS lookup. This process speeds up name resolution if multiple lookups are done to the same address.

Why we need to clear DNS cache.

So if you have an invalid DNS entry cached on your local client you’ll need to flush it out of the cache so your client can do a new lookup and get the correct information. Or your other option is to wait until that DNS entry expires and the cache flushes it automatically… which typically takes about 24 hours.

Consider another situation, if you updated your DNS values for your domain and because of the DNS cache in your machine/server the DNS resolution is not effecting, here you can clear the DNS cache and suddently it will resolve the new vales.

1.Flush nscd dns cache

Nscd  is  a  daemon  that  provides a cache for the most common name service requests. The default configuration file, /etc/nscd.conf, determines the behavior of the cache daemon.

Nscd provides caching for accesses of the passwd, group, and hosts databases through standard libc interfaces, such as getpwnam, getpwuid, getgrnam, getgrgid, gethostbyname, and others.

There are two caches for each database: a positive one for items found, and a negative one for items not found. Each cache has a separate TTL (time-to-live) period for its data.  Note that the shadow file is specifically not cached.  getspnam(3) calls remain uncached as  a result.  As a result of this behavior there is not possible to change non-nscd user to another non-nscd user via su service when nscd is running.

To clear the nscd cache restart the service with the following command.

/etc/init.d/nscd restart


service nscd restart

2.Flush dnsmasq dns cache

dnsmasq is a lightweight DNS, TFTP and DHCP server. It is intended to provide coupled DNS and DHCP service to a LAN.

Dnsmasq  accepts  DNS queries and either answers them from a small, local, cache or forwards them to a real, recursive, DNS server. It loads the contents of /etc/hosts so that local hostnames which do not appear in the global DNS can be resolved and  also  answers  DNS queries for DHCP configured hosts.

The dnsmasq DHCP server supports static address assignments, multiple networks, DHCP-relay and RFC3011 subnet specifiers. It automatically sends a sensible default set of DHCP options, and can be configured to send any desired set of DHCP options,  including  vendor encapsulated options.

It includes a secure, read-only, TFTP server to allow net/PXE boot of DHCP hosts and also supports BOOTP.

Dnsmasq supports IPv6 for DNS, but not DHCP.

To clear the dnsmasq cache restart the service with the following command.

/etc/init.d/dnsmasq restart


service dnsmasq restart

3.Flush caching BIND server dns cache

A caching BIND server obtains information from another server (a Zone Master) in response to a host query and then saves (caches) the data locally. All you have to do is restart bind to clear its cache:

/etc/init.d/named restart

Above methods willl clear cache from your local machine.


Proxy Support for YUM

All of us know the Package Management System of Linux distributions makes it very easy to Install, Update and Upgrade softwares. In a network environment there may be a proxy server (eg.Squid) present in most of the cases. If the proxy is configured over the network there is a chance to block the yum  and it can not directly communicated with the Yum repository or Yum server.

To make sure that  Proxy server is aware of the Packet Management System  we should do the following  settings  in the configuration file, here we are explain in the case of  YUM.

The following changes are to be done in the master configuration file of YUM

Open the Yum configuration file and add the following lines,


Serch for the following entry in the yum.conf


Consider the below given exmple, make sure to replace the proxy server name and port number to replace with your original one.


In the case of proxy server with authentication, the following two directives can be used to specify the username and password of the squid server.

proxy_username = squid_acc     #username

proxy_password = asd123         #password

Now you can use YUM with squid and note that this is the global configuration and all those users who can use YUM will use this username and password for YUM.


Create bootable USB for Network Installation

Nowadays in most of all the environments Linux systems are used, so there comes the need of installation over the network and here we  need a bootable CD (Boot CD). In common these CDs are know as “netinstall” CD’s. A boot CD is one which contains only the resources for booting up a system and the essentials packages that supports networking. Sometimes we need in a boot CD and it may be required for only time use here we can use the bootable USB (OS boot from USB).

Commonly in RHEL and CentOs the installation tree consists of a directory called “images”, here we can find the file named “boot.img” or in another name “bootdisk.img”.

We can use these images with the “dd” command to make the USB stick bootable. Consider the file as boot.img,

Steps to make bootable USB stick.

dd if=<path to boot.img> of=<usb device node>
example: dd if=boot.img of=/dev/sdb

Zip and copy files from one directory to another with Perl script

Consider the condition some times we need to copy and Zip files from one location to another location everyday. Manually Zipping and copying is a tedious and time consuming process. As the part of automation we can do the same with the help of Perl script to Zip and copy file from one location to another.

In this script we need to specify the $base_path and  backup path $bkp_path. Base path means from where we need to backup the data and backup path means the destination after Zipping wehre we need to move the data (backup media or Backup drive).


use DBI;

use POSIX;
$argc = @ARGV;

if ($argc == 2) {

$base_path = $ARGV[0];

$bkp_path = $ARGV[1];

}else {

print "Usage:perl $PROGRAM <src_path> <dest_path>\n";



opendir(LOGDIR, "$base_path") || print "Could not open directory $base_dir - $!\n";

while (my $entry = readdir(LOGDIR)) {

if ($entry =~ /^\./) {



chdir( $base_path ) or die "Cant chdir to $base_path $!";

if($entry =~ m/.gz/) {

print "The file $entry is already zipped \n";

$zipped_filename = $entry;

} else {

print "The file is $entry \n";

`gzip $entry`;

$zipped_filename = $entry.".".gz;

`mv $zipped_filename $bkp_path`


This script is used to zip files in one directory and move that files to another folder. It will take base path and backup path from command line. Here it is $base_path and $bkp_path. Then it will zip the files in $base_path  and move it $bkp_path.

Suppose the script name is

For e.g. perl /home/praveen/ /home/l3admins/

After running the script you can verify the same in the backup path.

Here $base_path is  /home/Praveen and backup path is /home/l3admins/

You can automate the same with the help of cron tab by putting the following entries,

Consider the cast your Script in the location /root and the Script name is ,

5 6 * * * /root/

According to the above crontab entries the script is scheduled to run everyday at 6.05 AM


Locating files using the find command with 15 examples

Find is a versatile tool which can be used to locate files and directories satisfying different user criteria. But the sheer number of options for this command line tool makes it at the same time both powerful and encumbering for the user. Here I will list a few combinations which one can use to get useful results using find command.

Find all HTML files starting with letter ‘a‘ in your current directory (Case sensitive)

$ find . -name a\*.html

Same as above but case insensitive search.

$ find . -iname a\*.html

Find files which are larger than 5 MB in size.

$ find . -size +5000k -type f

Here the ‘+’ in ‘+5000k’ indicates greater than and k is kilobytes. And the dot ‘.’ indicates the current directory. The -type option can take any of the following values:

    f - file
    d - directory
    l - symbolic link
    c - character
    p - named pipe (FIFO)
    s - socket
    b - block device

Find all empty files in your directory

$ find . -size 0c -type f

… Which is all files with 0 bytes size. The option -size can take the following:

    c - bytes
    w - 2 byte words
    k - kilo bytes
    b - 512 byte blocks

Note: The above command can also take the -empty parameter.

Find is very powerful in that you can combine it with other commands. For example, to find all empty files in the current directory and delete them, do the following:

$ find . -empty -maxdepth 1 -exec rm {} \;

To search for a html file having the text ‘Web sites‘ in it, you can combine find with grep as follows:

$ find . -type f -iname \*.html -exec grep -s "Web sites" {} \;

The  -s option in grep suppresses errors about non-existent or unreadable files. And {} is a placeholder for the files found. The semicolon ‘;’ is escaped using backslash so as not to be interpreted by bash shell.

Note: You can use the -exec option to combine any command in Linux with the find command. Some of the useful things you can do with it are as follows:

Compress log files on an individual basis

$ find /var -iname \*.log -exec bzip {} \;

Find all files which belong to user pravi and change its ownership to pcuser

# find / -user pravi -exec chown pcuser {} \;

Note: You can also use xargs command instead of the -exec option as follows:

$ find /var -iname \*.log | xargs bzip –

Find all files which do not belong to any user:

$ find . -nouser

Find files which have permissions rwx for user and rw for group and others :

$ find . -perm 766

List the output of the above command  find . -perm 766

$ find . -perm 766 -exec ls -l {} \;

Find all directories with name bug_files

$ find . -type d -iname \*bug_files\*

Suppose you want to find files of size between 800k and 900k, do the following:

$ find . \( -size +800k -and -size -900k \)

And how about getting a formatted output of the above command with the size of each file listed ?

$ find . \( -size +700k -and -size -1000k \) -exec du -Hs {} \; 2>/dev/null

Here, the ‘2>/dev/null’ means all the error messages are discarded or suppressed.

You can also limit your search by file system type. For example, to restrict search to files residing only in the NTFS and VFAT filesystem, do the following:

$ find / -maxdepth 2 \( -fstype vfat -or -fstype ntfs \) 2> /dev/null

These are the most common uses of the find command. You can see additional uses by reading the find manual.


10 System info Commands

10 System info Commands

If you are developing an application for Linux desktop and would like to automatically find out more information about system, use the following commands in shell scripts to gather information about system. Before applying a command you must know how to use the command effectively in Shell. Here we are explaing basich linux system commands and their usage.


This command will help you to find out your present working direcotry.

Command Usage:

[root@l3admins ~]# pwd
[root@l3admins ~]#

2. hostname

The kernel maintains the current (unqualified) hostname of the system. Before trying to set your hostname, it’s important to know how to tell when you’ve got it right. Here are a few of the numerous ways to obtain it:

Command Usage:

[root@l3admins ~]# hostname
[root@l3admins ~]#

3. whoami

This will display the login name like admin,root or your user name.

Command Usage:

[root@l3admins ~]# whoami
[root@l3admins ~]#

4. id
This command will display all the informations about a user like user ID, memeber of which Group and prints other user identity information

Command Usage:

 [root@l3admins ~]# id root
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[root@l3admins ~]#

5. date

This command will display the current date and timezone of your machine

Command Usage:

[root@l3admins ~]# date
Tue Jan  4 01:01:14 PST 2011
[root@l3admins ~]#

6. W

Its a command which conveys so many informations like load, number of users logged in, Uptime for the system.

[root@l3admins ~]# w
 01:46:06 up 59 min,  3 users,  load average: 0.00, 0.00, 0.04
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       -                00:49   ?xdm?  25.17s  0.39s /usr/bin/gnome-session
root     pts/1    :0.0             00:50   43:23   0.12s  0.12s bash
root     pts/2     01:03    0.00s  0.16s  0.01s w
[root@l3admins ~]#

7. finger

This is a user information lookup program, Finger displays the userâs login name, real name, terminal name and write status (as a ââ*ââ after the terminal name if write permission is denied), idle time, login time, office location and office phone number.

Command Usage:

[root@l3admins ~]# finger root
Login: root                             Name: root
Directory: /root                        Shell: /bin/bash
On since Tue Jan  4 00:49 (PST) on :0 (messages off)
On since Tue Jan  4 00:50 (PST) on pts/1 from :0.0
   49 minutes 26 seconds idle
On since Tue Jan  4 01:03 (PST) on pts/2 from
New mail received Fri Dec 31 02:30 2010 (PST)
     Unread since Sun Dec  5 08:36 2010 (PST)
No Plan.
[root@l3admins ~]#

8. last

This command will bring you the information about the last loggedin user.

Command Usage:

[root@l3admins ~]# last
root     pts/2     Tue Jan  4 01:03   still logged in
root     pts/1        :0.0             Tue Jan  4 00:50   still logged in
root     :0                            Tue Jan  4 00:49   still logged in
root     :0                            Tue Jan  4 00:49 - 00:49  (00:00)
reboot   system boot  2.6.18-194.el5   Tue Jan  4 06:18          (-4:-23)

9. lastb

This command will give you the information about the last bad (unsuccessful) login attempts to the machine.

Command Usage:

[root@l3admins ~]# lastb
root     ssh:notty     Tue Jan  4 01:58 - 01:58  (00:00)
root     ssh:notty     Tue Jan  4 01:58 - 01:58  (00:00)

btmp begins Tue Jan  4 01:58:31 2011
[root@l3admins ~]#

10. free

Memory info (in kilobytes). “Shared” memory is the memory that can be shared between processes (e.g., executable code is “shared”). “Buffered” and “cashed” memory is the part that keeps parts of recently accessed files – it can be shrunk if more memory is needed by processes.

Command usage:

[root@l3admins ~]# free
             total       used       free     shared    buffers     cached
Mem:       1035108     655116     379992          0      88648     410024
-/+ buffers/cache:     156444     878664
Swap:      2096472          0    2096472
[root@l3admins ~]#

Changing mysql data path in Linux

MySQL is a widely used and fast growing SQL database server. Mysql is a Client/Server implementation that consists of (Mysqld) server daemon and various client programs and libraries.

Mysql Data Directory

Data directory is the important part of the Mysql server where all the Data files is getting stored inside the Data folder. By default the same is located in /var/lib/mysql.

Need of changing data path

Some times the database size will go beyond our calculation and this cause to /var partition to run out of space. In this case we need to change the Mysql data folder path another location where we have enough free space.

Follow the steps to Change mysql data path.

Before changing the data path folder stop the Mysql server.

/etc/init.d/mysqld stop

Make a new directory for the data path, where you have enough space.

mkdir /opt/mysql_data

Now we need to copy the existing database in to the new location /opt/mysql_data

cp -R /var/lib/mysql /opt/mysql_data

After copying the database we need to update the new data path file in the my.cnf file.

vi /etc/my.cnf

datadir = /var/lib/mysql

Change it to

datadir = /opt/mysql_data

Now we moved the mysql data directory in to the new location and start the Mysql server to write the DB files to the new location.

/etc/init.d/mysqld start

After staring the Mysql server go to the new location and make sure that all the DB files are updating properly with out any errors.


Disable ping in Linux Server

The PING ( Packet Internet Groper )command is used to test the connection and latency between two network connections. These connections can be either in a local area network or a wide area network or the internet as a whole. The PING command sends packets of information to a specified IP Address and then measures the time it takes to get a response from the specified computer or device.

Disable ping using apf firewall

  • To do the same make sure that apf is installed in your server

To disable ping using apf we need to enable the same in apf configuration file /etc/apf/conf.apf.

Open the configuration file /etc/apf/conf.apf with your favorite editor,

vi /etc/apf/config.apf

Check for the following line,
from the line remove the number 8 at the entry.

To reflect the changes restart the apf service

/etc/init.d/apf restart

Disable ping using iptables

iptables -I INPUT  -p icmp -m icmp --icmp-type echo-request -j REJECT

To save the changes and to reflcet the in iptables configuration file do the following command.

service iptables save

Disable ping from /etc/sysctl.conf

Inorder to disable ping from /etc/sysctl.conf, open the fille and add the following lines and save.

# Ignore Ping
net.ipv4.icmp_echo_ignore_all = 1

To reflect the change in the network setting of the server restart the network services with the following command.

/etc/rc.d/init.d/network restart

Do the above steps and after completing the same check wether the ping is disabled or not with the following command,


Disable Services in Linux

Disable Telnet

In older Linux distributions the telnet system is enabled by default. Ftp, rlogin and telnet are vulnerable to eavesdropping that’s why it is recommended to use the secure versions. (sftp,scp, ssh). If you want for any reason to use the telnet terminal you should at least hide the banner information although it is not recommended to use telnet at all.

How To:

Login to your machine as root user, and edit the file,

vi /etc/xinetd.d/telnet

Change the following line

Look for the line: disable = no and replace with disable = yes

To reflect the changes restart the inetd service,

 /etc/rc.d/init.d/xinetd restart

To turnoff the telnet service permenently use the chkconfig command

/sbin/chkconfig telnet off

Scan your machine to make sure that the telnet is disabled and no service is running on port:23 completely,

nmap -sT -O localhost

rpm basics

The RedHat Package Manager (RPM) is a popular package management system. Using RPM it’s easier to install, uninstall, and upgrade software packages. Apart from that, it’s possible to query a package for details and also can verify the integrity of installed packages.

Mainly there are two types of RPM packages

 1) Binary packages
 2) source packages

Binary package has been compiled for a particular architecture and is ready for installation, whereas source package contains source files to build a binary package.
Here am giving some examples for RPM installing, uninstalling, upgrading, querying, and verifying software packages.

Naming Convention used in RPM

Consider the Apache RPM package –> httpd-2.0.53-3.3.i386.rpm


httpd –> name of the software
2.0.53 –> software version (that of the original software source)
3.3 –> package release number
i386 –> architecture (like 32 or 64 bit)

Corresponding source package is httpd-2.0.53-3.3.src.rpm (note ’src’ in the name).

RPM Instalaltion

To install a package, we mainly use the “-ivh” option

$ rpm -ivh httpd-2.0.53-3.3.i386.rpm

Preparing… ########################################### [100%]
1:httpd ########################################### [100%]

Flags used are;

-v flag can be used to introduce some verbosity,
-h flag displays hash marked progress of the installation.
-i installing

RPM Installation over Network

$ rpm -ivh ftp:////package.rpm

$ rpm -ivh http:////package.rpm

Uninstalling RPM
To Uninstall an RPM, Use the -e option

$ rpm -e httpd-2.0.53-3.3.i386.rpm

Just have to specify name of the package to be removed.

If the pakages are not removed and it shows some dependencies over other installed packages, To ignore the error and uninstall the package use the following option,

rpm -e --nodeps httpd-2.0.53-3.3.i386.rpm

–nodeps –> Do Not Check Dependencies Before Erasing Package

“This is not a recomendaded usage, since it can break other installed packages”

Hope that this will help 🙂