Thursday, April 27, 2017

locate command examples in linux ?

When you need to search for some files, you might typically use find command. find is a good search utility but it is slow.
However locate can search for files very quickly.
Though the locate command works very fast, it still has not out-thrown the find command because it has some limitations.
This article explains everything you need to know about locate command .

How Locate Command Works? – updatedb and updatedb.conf

When we say that locate searches very quickly, then the first question that comes into mind is that what makes locate so fast?.
Well, locate does not search the files on disk rather it searches for file paths in a database.
The database is a file that contains information about the files and their path on your system. The locate database file is located at:
/var/lib/mlocate/mlocate.db
The next logical question is, what keeps this mlocate database updated?
Well, there is another utility known as updatedb. When you execute updatedb, it scans the whole system and updates the mlocate.db database file.
So one limitation of the ‘locate’ command is its dependency on the database which can be updated by another utility ‘updatedb’. Hence, in order to get the latest and reliable results from ‘locate’ command the database on which it works should be updated at regular intervals.
We can also configure the ‘updatedb’ utility as per our needs. This can be achieved by updating the updatedb.conf. This is a configuration file that updatedb reads before updating the database. updatedb.conf is located under /etc/ :

# cat /etc/updatedb.conf
PRUNE_BIND_MOUNTS="yes"
PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf fuse.glusterfs fuse.sshfs ecryptfs fusesmb devtmpfs"

updatedb.conf file contains information in the form of VARIABLES=VALUES. These variables can be classified into :
    PRUNEFS : A  whitespace-separated  list of file system types (as used in /etc/mtab) which should not be scanned by updatedb.  The file system type matching is case-insensitive.  By default, no file system types are skipped. When scanning a file system is skipped, all file systems mounted in the subtree are skipped too, even if their  type  does  not  match  any entry in PRUNEFS.
    PRUNENAMES : A  whitespace-separated list of directory names (without paths) which should not be scanned by updatedb.  By default, no directory names are skipped. Note that only directories can be specified, and no pattern mechanism (e.g.  globbing) is used.
    PRUNEPATHS : A whitespace-separated list of path names of directories which should not be scanned by updatedb.  Each path name must be exactly in the form in which the directory would be reported by locate.  By default, no paths are skipped.
    PRUNE_BIND_MOUNTS :  One  of  the  strings  0,  no,  1  or yes.  If PRUNE_BIND_MOUNTS is 1 or yes, bind mounts are not scanned by updatedb.  All file systems mounted in the subtree of a bind mount are skipped as well, even if they are not bind mounts. By default, bind mounts are not skipped.
Note that all of the above configuration information can also be changed or updated through the command line options to the utility ‘updatedb’.


Practical Examples of Locate Command

1. Search a File using locate

To search a particular file using locate, just do the following
srini@linuxforfreshers.com:~$ locate mysql.conf
/etc/init/mysql.conf

The following command searches for apache2.conf in the entire system.
srini@linuxforfreshers.com:~$ locate apache2.conf
/etc/apache2/apache2.conf

You can also use “locate -0” to display all the output in one line. For example:
 Locate -0 apache2.conf

2. Display only the Count

To get the count of number of matching entry, use locate -c as shown below.
srini@linuxforfreshers.com:~$ locate -c apache2.conf
1

3.Restrict the Locate Output

In the following example, locate command displayed several entries.
$ locate passwd
/etc/passwd
/etc/passwd-
/etc/dovecot/conf.d/auth-passwdfile.conf.ext
/etc/pam.d/passwd
/etc/security/opasswd
/etc/vsftpd/passwd
/lib64/security/pam_unix_passwd.so
/usr/bin/gpasswd
/usr/bin/htpasswd
/usr/bin/ldappasswd
/usr/bin/mksmbpasswd.sh
/usr/bin/passwd
If you want to display only certain number of records, use locate -l option and specify how many records you want to see in the locate command output.
For example, the following displays only 5 records (Even when locate command finds several records..)
srini@linuxforfreshers.com:~$ locate -l 5 passwd
/etc/passwd
/etc/passwd-
/etc/alternatives/vncpasswd
/etc/alternatives/vncpasswd.1.gz
/etc/cron.daily/passwd

4. Ignore Case in Locate Output

The locate command by default is configured to accept the file name in a case sensitive manner. In order to make the results case insensitive, we can use the -i option :
In the following example, we created two files with both lowercase and uppercase.
# cd /tmp
# touch new.txt NEW.txt

# updatedb
If you use the locate command only with the lowercase, it will find only the lowercase file.
# locate new.txt
/tmp/new.txt
Use locate -i, which will ignore case, and look for both lowercase and uppercase file.
$ locate -i new.txt
/tmp/NEW.txt
/tmp/new.txt
/usr/share/doc/samba-common/WHATSNEW.txt.gz




Tuesday, April 25, 2017

How to run a program on specific CPU core on linux ?

Now i am using watch command set  taskset to core 3.
Before going set the taskset first find out the pid of the particular process using following commands.

[root@linuxforfreshers.com~]# ps -ef | grep watch
root     14063 27683  0 16:29 pts/47   00:00:00 watch -db ifconfig
root     14172 14070  0 16:30 pts/49   00:00:00 grep --color=auto watch

or

[root@linuxforfreshers.com~]# pidof watch
14063

Example:

[root@linuxforfreshers.com~]# taskset 03 -p 14063

Verifying

[root@linuxforfreshers.com~]# taskset -p 14063
pid 14063's current affinity mask: 3

or

ps -o pid,psr,comm -p <pid>

Example:

[root@linuxforfreshers.com~]#  ps -o pid,psr,comm -p 14063
  PID PSR COMMAND
14063   3 watch

If u want to bind the particular process to particular core u can use following command.

taskset -pc 1 <PID>

Example:
[root@linuxforfreshers.com~]# taskset -pc 1 14063

Where p is -p, --pid
              Operate on an existing PID and do not launch a new task.
             -c, --cpu-list
              Specify a numerical list of processors instead of a bitmask.  The numbers are       separated by commas and may include ranges.  For example: 0,5,7,9-11.

Verifying

[root@linuxforfreshers.com~]# ps -o pid,psr,comm -p 14063
  PID PSR COMMAND
14063   1 watch

Or

[root@linuxforfreshers.com~]# taskset -p 14063
pid 14063's current affinity mask: 1


If We want to dedicate a whole CPU core to a particular program and no other then your process should use this core.
Then use "isolcpus" kernel parameter in grub option.

GRUB_CMDLINE_LINUX_DEFAULT="cpuidle.off=1 idle=poll isolcpus=1-5 nohz_full=5 maxcpus=6"

update-grub

Note: below are meaning

cpuidle.off=1 (Do not make cpu idle)
isolcpus=5  (Isolate cpu core are 1-5)
maxcpus=6 (Use only 6 core (out of 8 cores of system)) 
idle=poll (Poll forces a polling idle loop)

Sunday, April 16, 2017

Linux Common Interview Questions and Answers ?

Q.0 What does the last two sections define in fstab file?

Ans: The 5th column tells the dump information if whether the partition has to be backed up. It it is "0" the filesystem will be ignored
The 6th column tells the order in which fsck command would check the filesystem on boot. If it is "0" then fsck won't check the filesystem

Q:1 How To check the uptime of a Linux Server ?

Ans: Using uptime command we can determine how long a linux box has been running , also uptime can be viewed by the top & w command.

Q:2 How to check which Redhat version is installed on Server ?

Ans: Use the command cat /etc/redhat-release , output of this command will tell you the redhat version.

Q:3 How to install rpm packages in Redhat & CentOS linux ?

Ans: rpm and yum command are used to install packages in redhat linux and CentOS.

Q:4 How to check the ip address of LAN Card ?

Ans: Using ‘ifconfig’ & ‘ip address’ command we can determine the ip address of LAN Card.

Q:5 How to determine the hostname of a linux box ?

Ans: On typing the hostname command on terminal we can determine the hostname of a linux server.

Q:6 How To check the default gatway ?

Ans: Using ‘route -n’ command we can determine the default gateway in linux.

Q:7 Which Command is used to check the kernel Version ?

Ans: ‘uname -r’

Q:8 How to check the current runlevel of a linux box ?

Ans : ‘who -r’ and ‘runlevel’ , both of these command are used to find current run level.

Q:9 What is Initrd ?

Ans: Initrd stands for initial ram disk , which contains the temporary root filesystem and neccessary modules which helps in mounting the real root filesystem in read mode only.

Q:10 What is Bootloader ?

Ans: Bootloader is a program that boots the operating system and decides from which kernel OS will boot.

Q:11 How to list hidden files from the command line ?

Ans: ‘ls -a’ <Folder_Name>

Q:12 What is soft link ?

Ans: Soft link is a method to create short cuts in linux. It is similar to windows short cut feature.

Q:13 How to create a blank file in linux from command line ?

Ans: Using the command ‘touch <file-name>’

Q:14 What is run level 2 ?

Ans: Run level 2 is the multi-user mode without networking.

Q:15 Why linux is called OpenSource ?

Ans: Because One can customize the existing code and can redistribute it.

Q:16 How to check all the installed Kernel modules ?

Ans: Using the Command ‘lsmod’ we can see the installed kernel modules.

Q:17 What is the default uid & gid of root user ?

Ans: Default uid & gid of root user is 0.

Q:18 How To change the password of user from the Command Line ?

Ans: ‘passwd <User-Name>’

Q:19 What is a Process ?

Ans: Any program in execution is called a process.

Q:20 What is name of first process in linux ?

Ans: ‘init’ is the first process in linux which is started by kernel and whose pid is 1.


How to find out from which folder a process is running on linux ?

First step find out the pid of the particular process.
Example:

deb@linuxforfreshers.com:~$ pidof chrome

15499

Or

deb@linuxforfreshers.com:~$ ps -ef | grep chrome
deb     15499 15410  0 07:46 ?        00:02:33 /opt/google/chrome/chrome

Note: In above line second filed is pid number.

Method 1:
Using pwdx command.
Syntax:
pwdx  pid_number

Example:

deb@linuxforfreshers.com:~$ pwdx 15499
15499: /home/deb

Method 2:

Using lsof command.
Syntax: lsof -p PID | grep cwd

Example:

deb@linuxforfreshers.com:~$ lsof -p 15499| grep cwd
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /home/deb/.gvfs
      Output information may be incomplete.
chrome  15499 deb  cwd    DIR                8,6      4096  5767170 /home/deb

Method 3:

Using readlink command
Syntax: readlink -e /proc/PID/cwd

Example:

deb@linuxforfreshers.com:~$ readlink -e /proc/15499/cwd
/home/deb

Method 4:

Using ls command
Syntax: ls -l /proc/<PID>/cwd

Example:
deb@linuxforfreshers.com:~$ ls -l /proc/15499/cwd
lrwxrwxrwx 1 deb deb 0 Apr 13 16:21 /proc/15499/cwd -> /home/deb

Method 5:

Using ps command
Syntax : ps auxwwwe | grep process_name


It will give all the list of process running from current directory.