Tuesday, March 21, 2017

lsof command exapmles in linux ?

lsof a utility command every system admin and developer love. The lsof command stands for list open file descriptors and as the name suggest, it is used to find open files by process. Since almost everything in UNIX are file, you can use lsof command to find an open regular file, a directory, a symbolic link, a block special file, a NFS mounted file, a socket stream, a shared library, a character special file, a regular pipe, a named pipe, an internet socket, a UNIX domain socket and many others. It's an incredibly useful tool to do debugging and troubleshooting in UNIX and Linux environment. Since most of the production system runs on the UNIX-based operating system, knowledge of this tool become even more important.


lsof command generally comes pre-installed in many UNIX system. If you are getting -bash: lsof: command not found an error while using lsof then it could be that lsof is not in your PATH. just check /usr/bin or /usr/sbin folder for this command. If you don't find there then you can install it from source or you can ask your UNIX admin to do that for you.

1)      How to list all open files by all process

syntax: lsof

Simply running lsof without any argument print all opened file and process. This is not particularly useful but a good starting point.

Example:

dev@linuxforfreshers.com:~$ lsof | tail -10
tail      23119                  dev   1u      CHR             136,52       0t0       55 /dev/pts/52
tail      23119                  dev   2u      CHR             136,52       0t0       55 /dev/pts/52
lsof      23120                  dev cwd       DIR                8,6      4096  5767170 /home/dev
lsof      23120                  dev rtd       DIR                8,6      4096        2 /
lsof      23120                  dev txt       REG                8,6    163224 28574379 /usr/bin/lsof
lsof      23120                  dev mem       REG                8,6   7216688 28580440 /usr/lib/locale/locale-archive
lsof      23120                  dev mem       REG                8,6   1840928  2622444 /lib/x86_64-linux-gnu/libc-2.19.so
lsof      23120                  dev mem       REG                8,6    149120  2622402 /lib/x86_64-linux-gnu/ld-2.19.so
lsof      23120                  dev   4r     FIFO               0,10       0t0  2094341 pipe
lsof      23120                  dev   7w     FIFO               0,10       0t0  2094342 pipe

2) How to list all process which has opened a file

Syntax: lsof /home/someuser/somefile

will list all the process which has opened this file. you can see the command, PID, user and full file path to find out the process.

Example:

dev@linuxforfreshers.com:~$ lsof /home/dev/ | grep ssh
ssh        8222 dev cwd    DIR    8,6     4096 5767170 /home/dev
ssh.expec  8266 dev cwd    DIR    8,6     4096 5767170 /home/dev
ssh.expec  8268 dev cwd    DIR    8,6     4096 5767170 /home/dev
ssh.expec  8274 dev cwd    DIR    8,6     4096 5767170 /home/dev

3) How to find all opened files by a user

You can use lsof -u command to list all opened file by a user as shown below
Syntax: lsof -u username

Example:          

dev@linuxforfreshers.com:~$ lsof -u dev| tail -10
lsof      23353 dev txt       REG                8,6    163224 28574379 /usr/bin/lsof
lsof      23353 dev mem       REG                8,6     43616  2622456 /lib/x86_64-linux-gnu/libnss_files-2.19.so
lsof      23353 dev mem       REG                8,6     47760  2622438 /lib/x86_64-linux-gnu/libnss_nis-2.19.so
lsof      23353 dev mem       REG                8,6     97296  2622400 /lib/x86_64-linux-gnu/libnsl-2.19.so
lsof      23353 dev mem       REG                8,6     39824  2622399 /lib/x86_64-linux-gnu/libnss_compat-2.19.so
lsof      23353 dev mem       REG                8,6   7216688 28580440 /usr/lib/locale/locale-archive
lsof      23353 dev mem       REG                8,6   1840928  2622444 /lib/x86_64-linux-gnu/libc-2.19.so
lsof      23353 dev mem       REG                8,6    149120  2622402 /lib/x86_64-linux-gnu/ld-2.19.so
lsof      23353 dev   4r     FIFO               0,10       0t0  2119980 pipe
lsof      23353 dev   7w     FIFO               0,10       0t0  2119981 pipe

4) How to list all files opened by a particular command
You can use lsof -c option to provide name of command and list down all the files opened by that command, for example, to list all file opened by mysql process, you can do this :
Syntax : lsof -c process_name


Example :
root@linuxforfreshers.com:~# lsof -c mysql | tail -10
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
mysqld  2149 mysql   54u   REG                8,6     1024 16515193 /var/lib/mysql/mysql/time_zone.MYI
mysqld  2149 mysql   55u   REG                8,6        0 16515194 /var/lib/mysql/mysql/time_zone.MYD
mysqld  2149 mysql   56u   REG                8,6     1024 16515202 /var/lib/mysql/mysql/time_zone_leap_second.MYI
mysqld  2149 mysql   57u   REG                8,6        0 16515203 /var/lib/mysql/mysql/time_zone_leap_second.MYD
mysqld  2149 mysql   58u   REG                8,6     1024 16515190 /var/lib/mysql/mysql/time_zone_name.MYI
mysqld  2149 mysql   59u   REG                8,6        0 16515191 /var/lib/mysql/mysql/time_zone_name.MYD
mysqld  2149 mysql   60u   REG                8,6     1024 16515196 /var/lib/mysql/mysql/time_zone_transition.MYI
mysqld  2149 mysql   61u   REG                8,6        0 16515197 /var/lib/mysql/mysql/time_zone_transition.MYD
mysqld  2149 mysql   62u   REG                8,6     1024 16515199 /var/lib/mysql/mysql/time_zone_transition_type.MYI
mysqld  2149 mysql   63u   REG                8,6        0 16515200 /var/lib/mysql/mysql/time_zone_transition_type.MYD

Example 2:
root@linuxforfreshers.com:~# lsof -c chrome| tail -10
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
chrome  22949 dev  74u      REG               0,22   4198400      167 /run/shm/.com.google.Chrome.frd729 (deleted)
chrome  22949 dev  75r      REG                8,6    333900  3670118 /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf
chrome  22949 dev  78r      REG                8,6    123828  3670122 /usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf
chrome  22949 dev  79r      REG                8,6    136032  3670125 /usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf
chrome  22949 dev  80u      REG               0,22   4198400       27 /run/shm/.com.google.Chrome.reBCyQ (deleted)
chrome  22949 dev  81r      REG                8,6    123828  3670122 /usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf
chrome  22949 dev  86u      REG               0,22   4198400      172 /run/shm/.com.google.Chrome.opHb4S (deleted)
chrome  22949 dev  87r      REG                8,6    330412  3670117 /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf
chrome  22949 dev  90r      REG                8,6    330412  3670117 /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf
chrome  22949 dev 104u      REG               0,22   4198400      193 /run/shm/.com.google.Chrome.Y05n64 (deleted)

5) How to find all files opened by a particular user and command

You can combine users and process name in one lsof command to list down all the files opened by a particular process or a particular user as shown below :

Syntax: $ lsof -u dev -c chrome

root@linuxforfreshers.com:~# lsof -u dev-c chrome | tail -10
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
chrome    22949 dev  74u      REG               0,22   4198400      167 /run/shm/.com.google.Chrome.frd729 (deleted)
chrome    22949 dev  75r      REG                8,6    333900  3670118 /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf
chrome    22949 dev  78r      REG                8,6    123828  3670122 /usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf
chrome    22949 dev  79r      REG                8,6    136032  3670125 /usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf
chrome    22949 dev  80u      REG               0,22   4198400       27 /run/shm/.com.google.Chrome.reBCyQ (deleted)
chrome    22949 dev  81r      REG                8,6    123828  3670122 /usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf
chrome    22949 dev  86u      REG               0,22   4198400      172 /run/shm/.com.google.Chrome.opHb4S (deleted)
chrome    22949 dev  87r      REG                8,6    330412  3670117 /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf
chrome    22949 dev  90r      REG                8,6    330412  3670117 /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf
chrome    22949 dev 104u      REG               0,22   4198400      193 /run/shm/.com.google.Chrome.Y05n64 (deleted)

6) How to list all open files by a process using PID
As I told, I mostly use lsof command to find all files opened by a particular process. In order to do that sometimes, I usually use grep command to filter lsof output by PID, but you can also use lsof -p option to do the same, as shown below :

$ lsof -p 17783

will list all files opened by the process with PID 17783.

List users and processes, you can also supply multiple PIDs to find files opened by multiple processes e.g. :

$ lsof -p 17783,17754,17984


7) How to list all network connection
You can use lsof - i option to find all open network connections which is nothing but open internet sockets (TCP and UDP), for example

Syntax: $ lsof -i

you can further find all TPC connection by using tcp option as shown below :

$ lsof -i tcp

Similarly, to find all open udp connections you can do :

$ lsof -i udp

will list all process with open internet sockets.



8) How to find which process is using a port
Though you can do this with netstat command as well, you would be surprised to know that you can find all process using a particular TCP or UDP port using lsof command.

Syntax: lsof -i :portnumber

Example:

root@linuxforfreshers.com:~# lsof -i :3306
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  2149 mysql   10u  IPv4  12927      0t0  TCP localhost:mysql (LISTEN)


9) To list all open files on device /dev/sda, use:

     Syantax:    lsof /dev/sda

10) To find any open file, including an open UNIX domain socket file, with the name /dev/log, use:

    Syntax:        lsof /dev/log

11)  To find an IP version 4 socket file by its associated numeric dot-form address, use:

 Syntax: lsof -i@ipaddress

Example: lsof -i@192.168.101.1







Tuesday, March 14, 2017

How to find PID of the current working shell in linux ?

Using Following methods u can get the  PID of shell in linux.

Method 1:

echo $$
Example:

dev@linuxforfreshers.com:~$ echo $$
29215

Where $$  is the PID of the current shell.

Method 2:

dev@linuxforfreshers.com:~$ ps -p $$
  PID TTY          TIME CMD
29215 pts/40   00:00:00 bash

Or

dev@linuxforfreshers.com:~$ ps -p $$ | awk '{ print $1 }'
PID
29215


Method 3:
It will give all the shell’s PID numbers.

Example:

dev@linuxforfreshers.com:~$ sudo pidof /bin/bash
31396 29248 29215 27940 15796 15764

Method 4:
Using BASHPID

Example:

dev@linuxforfreshers.com:~$ echo $BASHPID
29215




Tuesday, March 7, 2017

How to find complete system and hardware details on linux using inxi?

Inxi is a command line tool that can be used to find the complete system and hardware details such as;
     Hardware,
     CPU,
     Drivers,
     Xorg,
     Desktop,
     Kernel,
     GCC version,
     Processes,
     RAM usage,
     and other useful information.
  
      Installation
Inxi is available in the default repositories of most modern GNU/Linux operating systems. So, we can simply install it by running the following commands.
On Debian based system:
sudo apt-get install inxi
On Fedora:
sudo yum install inxi
On RHEL based systems:
Install EPEL repository:
sudo yum install epel-release
Then, install inxi using command:
sudo yum install inxi


Before we start using it, we can run the command that follows to check all application dependencies plus recommends, and various directories, and display what package(s) we need to install to add support for a given feature.

inxi --recommends
Example :
root@linuxforfreshers.com:~$ inxi --recommends
inxi will now begin checking for the programs it needs to operate. First a check of
the main languages and tools inxi uses. Python is only for debugging data collection.
-----------------------------------------------------------------------------------------
Bash version: 4.3.11(1)-release
Gawk version: 4.0.1
Sed version:
Sudo version: 1.8.9p5
Python version: 2.7.6
-----------------------------------------------------------------------------------------
Test One: Required System Directories.
If one of these system directories is missing, inxi cannot operate:

Required file system: /proc................................................. Present
Required file system: /sys.................................................. Present

All the required directories are present.
-----------------------------------------------------------------------------------------
Test Two: Required Core Applications.
If one of these applications is missing, inxi cannot operate:

Required application: df (info: partition data)............................. /bin/df
Required application: gawk (info: core tool)................................ /usr/bin/gawk
Required application: grep (info: string search)............................ /bin/grep
Required application: lspci (info: hardware data)........................... /usr/bin/lspci
Required application: ps (info: process data)............................... /bin/ps
Required application: readlink.............................................. /bin/readlink
Required application: sed (info: string replace)............................ /bin/sed
Required application: tr (info: character replace).......................... /usr/bin/tr
Required application: uname (info: kernel data)............................. /bin/uname
Required application: uptime................................................ /usr/bin/uptime
Required application: wc (info: word character count)....................... /usr/bin/wc

All the required applications are present.
-----------------------------------------------------------------------------------------
Test Three: Script Recommends for Graphics Features. If you do not use X these do not matter.
If one of these applications is missing, inxi will have incomplete output:

Recommended X application: glxinfo (info: -G glx info)...................... /usr/bin/glxinfo
Recommended X application: xdpyinfo (info: -G multi screen resolution)...... /usr/bin/xdpyinfo
Recommended X application: xprop (info: -S desktop data).................... /usr/bin/xprop
Recommended X application: xrandr (info: -G single screen resolution)....... /usr/bin/xrandr

All the recommended applications are present.
-----------------------------------------------------------------------------------------
Test Four: Script Recommends for Remaining Features.
If one of these applications is missing, inxi will have incomplete output:

Recommended application: dmidecode (info: -M if no sys machine data)........ /usr/sbin/dmidecode
Recommended application: file (info: -o unmounted file system).............. /usr/bin/file
Recommended application: hddtemp (info: -Dx show hdd temp).................. /usr/sbin/hddtemp
Recommended application: ifconfig (info: -i ip lan-deprecated).............. /sbin/ifconfig
Recommended application: ip (info: -i ip lan)............................... /sbin/ip
Recommended application: sensors (info: -s sensors output).................. /usr/bin/sensors
Recommended application: lsusb (info: -A usb audio;-N usb networking)....... /usr/bin/lsusb
Recommended application: modinfo (info: -Ax,-Nx module version)............. /sbin/modinfo
Recommended application: runlevel (info: -I runlevel)....................... /sbin/runlevel
Recommended application: sudo (info: -Dx hddtemp-user;-o file-user)......... /usr/bin/sudo

All the recommended applications are present.
-----------------------------------------------------------------------------------------
Test Five: System Directories for Various Information.
If one of these directories is missing, inxi will have incomplete output:

System directory: /sys/class/dmi/id (info: -M system, motherboard, bios).... Present
System directory: /dev (info: -l,-u,-o,-p,-P,-D disk partition data)........ Present
System directory: /dev/disk/by-label (info: -l,-o,-p,-P partition labels)... Present
System directory: /dev/disk/by-uuid (info: -u,-o,-p,-P partition uuid)...... Present
System directory: /var/run/dmesg.boot (info: -C,-f (BSD only)).............. Missing

The following directories are missing from your system:
Directory: /var/run/dmesg.boot
These directories are created by the kernel, so don't worry if they are not present.
-----------------------------------------------------------------------------------------
All tests completed.

Basic Usage of inix
Show Linux System Information

To find the quick view of the system information, run the following command from Terminal.
Inxi
Example:
root@linuxforfreshers.com:~$ inxi
CPU~Dual core Intel Core2 CPU 6400 (-MCP-) clocked at 2133.000 Mhz Kernel~3.16.0-76-generic x86_64 Up~4 days Mem~2591.5/3888.8MB HDD~515.8GB(47.0% used) Procs~292 Client~Shell inxi~1.9.17 

Show Linux Kernel and Distribution Info
The below command will gives hostname, kernel version and Distro information usinf flag -S ( s is capital letter)

Inxi -S

Example:
root@linuxforfreshers.com:~$ inxi -S
System:    Host: LINUXFORFRESHERS.COM Kernel: 4.4.0-59-generic x86_64 (64 bit) Desktop: Gnome Distro: Ubuntu 14.04 trusty

Find Audio/Sound hardware details


we can retrieve a particular hardware details. For example to retrieve the Audio/Sound hardware details, run the following command using -A flag:
inxi –A

Example:
root@linuxforfreshers.com:~$ inxi -A
Audio:     Card: NVIDIA High Definition Audio Controller driver: snd_hda_intel Sound: ALSA ver: k3.16.0-76-generic

Find Graphic card details

Using flag -G get the Graphic card information.

root@linuxforfreshers.com:~$ inxi -G
Graphics:  Card: NVIDIA GT218 [GeForce 210]
           X.Org: 1.16.0 drivers: nouveau (unloaded: fbdev,vesa) Resolution: 1920x1080@60.0hz, 1920x1080@60.0hz
           GLX Renderer: Gallium 0.4 on NVA8 GLX Version: 3.0 Mesa 10.3.2

Find Linux Hard Disk Information
To view the full hard disk information, run the following command.
inxi -D
root@linuxforfreshers.com:~$ inxi -D
Drives:    HDD Total Size: 515.8GB (47.0% used) 1: id: /dev/sda model: TOSHIBA_MQ01ACF0 size: 500.1GB
           2: USB id: /dev/sdb model: Cruzer_Blade size: 15.7GB

Find Linux system or PC Model Information

To display the Bios and Motherboard details using -M flag.
inxi -M
root@linuxforfreshers.com:~$ inxi -M
Machine:   System: Hewlett-Packard product: HP Compaq dc7700 Small Form Factor
           Mobo: Hewlett-Packard model: 0A54h Bios: Hewlett-Packard version: 786E1 v01.10 date: 04/13/2007

Find list of repositories

list of available repositories in our system using flag -r.
inxi -r
root@linuxforfreshers.com:~$ inxi -r
Repos:     Active apt sources in file: /etc/apt/sources.list
           deb http://in.archive.ubuntu.com/ubuntu/ trusty main restricted
           deb-src http://in.archive.ubuntu.com/ubuntu/ trusty main restricted
           deb http://in.archive.ubuntu.com/ubuntu/ trusty-updates main restricted
           deb-src http://in.archive.ubuntu.com/ubuntu/ trusty-updates main restricted
           deb http://in.archive.ubuntu.com/ubuntu/ trusty universe
           deb-src http://in.archive.ubuntu.com/ubuntu/ trusty universe
           deb http://in.archive.ubuntu.com/ubuntu/ trusty-updates universe
           deb-src http://in.archive.ubuntu.com/ubuntu/ trusty-updates universe
           deb http://in.archive.ubuntu.com/ubuntu/ trusty multiverse
           deb-src http://in.archive.ubuntu.com/ubuntu/ trusty multiverse
           deb http://in.archive.ubuntu.com/ubuntu/ trusty-updates multiverse
           deb-src http://in.archive.ubuntu.com/ubuntu/ trusty-updates multiverse
           deb http://in.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
           deb-src http://in.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
           deb http://security.ubuntu.com/ubuntu trusty-security main restricted
           deb-src http://security.ubuntu.com/ubuntu trusty-security main restricted
           deb http://security.ubuntu.com/ubuntu trusty-security universe
           deb-src http://security.ubuntu.com/ubuntu trusty-security universe
           deb http://security.ubuntu.com/ubuntu trusty-security multiverse
           deb-src http://security.ubuntu.com/ubuntu trusty-security multiverse

Find Linux Hard Disk Partition Details
To print the number of partitions use the flag -p

 inxi -p

Example:
root@linuxforfreshers.com:~$ inxi -p
Partition: ID: / size: 451G used: 225G (53%) fs: ext4 ID: /boot size: 361M used: 219M (65%) fs: ext4
           ID: swap-1 size: 8.00GB used: 0.08GB (1%) fs: swap

Find the Network Card information

To print the NIC details use the flag -N

Inxi -N

Example:
root@linuxforfreshers.com:~$ inxi -N
Network:   Card: Intel 82566DM Gigabit Network Connection driver: e1000e

Finding linux Memory process usage

Using flag -I u will get number of processes , uptime and memory usage.

Inxi –I
Example:
root@linuxforfreshers.com:~$ inxi -I
Info:      Processes: 271 Uptime: 7:19 Memory: 2258.1/3887.7MB Client: Shell (bash) inxi: 1.9.17

Monitor Linux CPU Temperature and Fan Speed
Using flag -s u ge the cpu sensors information
Inxi -s

Example:

root@linuxforfreshers.com:~$ inxi -s
Sensors:   System Temperatures: cpu: 51.0C mobo: 61.0C gpu: 61.0
           Fan Speeds (in rpm): cpu: 2132 fan-2: 0 fan-3: 1430 fan-4: 1173


Find the top 5 active process running on linux

Using following command to get top 5 active process and cpu utilization.
Inxi -t c

Example:
root@linuxforfreshers.com:~$ inxi -t c
Processes: CPU - % used - top  5 active
           1: cpu: 41.5% command: chrome pid: 21733
           2: cpu: 13.6% command: chrome pid: 3949
           3: cpu: 4.5% command: chrome pid: 3792
           4: cpu: 3.9% command: compiz pid: 3497
           5: cpu: 3.2% command: chrome pid: 4989

Find weather report

Inxi will also display the Weather details of your location.

inxi -w

root@linuxforfreshers.com:~$ inxi -W vijayawada,andhrapradesh
Weather:   Conditions: 86 F (30 C) - haze Time: December 16, 4:55 PM IST


Finding total complete Hardware details
Viewing Complete Hardware details using -F flag.
inxi -F
root@linuxforfreshers.com:~$ inxi -F
System:    Host: LINUXFORFRESHERS.COM Kernel: 3.16.0-76-generic x86_64 (64 bit) Desktop: Gnome Distro: Ubuntu 14.04 trusty
Machine:   System: Hewlett-Packard product: HP Compaq dc2200 Small Form Factor
           Mobo: Hewlett-Packard model: 0A54h Bios: Hewlett-Packard version: 678E1 v01.10 date: 04/13/2006
CPU:       Dual core Intel Core2 CPU 6400 (-MCP-) cache: 2048 KB flags: (lm nx sse sse2 sse3 ssse3 vmx)
           Clock Speeds: 1: 1596.00 MHz 2: 1596.00 MHz
Graphics:  Card: NVIDIA GT218 [GeForce 210]
           X.Org: 1.16.0 drivers: nouveau (unloaded: fbdev,vesa) Resolution: 1920x1080@60.0hz, 1920x1080@60.0hz
           GLX Renderer: Gallium 0.4 on NVA8 GLX Version: 3.0 Mesa 10.3.2
Audio:     Card: NVIDIA High Definition Audio Controller driver: snd_hda_intel Sound: ALSA ver: k3.16.0-76-generic
Network:   Card: Intel 82566DM Gigabit Network Connection driver: e1000e
           IF: eth0 state: up speed: 100 Mbps duplex: full mac: 00:1b:78:8b:33:c4
Drives:    HDD Total Size: 515.8GB (47.0% used) 1: id: /dev/sda model: TOSHIBA_MQ01ACF0 size: 500.1GB
           2: USB id: /dev/sdb model: Cruzer_Blade size: 15.7GB
Partition: ID: / size: 451G used: 226G (53%) fs: ext4 ID: /boot size: 361M used: 273M (81%) fs: ext4
           ID: swap-1 size: 8.00GB used: 0.37GB (5%) fs: swap
RAID:      No RAID devices detected - /proc/mdstat and md_mod kernel raid module present
Sensors:   System Temperatures: cpu: 48.0C mobo: N/A gpu: 62.0
           Fan Speeds (in rpm): cpu: N/A
Info:      Processes: 276 Uptime: 4 days Memory: 2555.3/3888.8MB Client: Shell (bash) inxi: 1.9.17