Tuesday, October 4, 2016

who command examples in linux ?

You need to use who command to display users who are currently logged in your server

Purpose
Display who is on the system
Syntax
The basic syntax is as follows:
who
who am i
who [options] [File]
who --help
who --version
who | grep 'userNameHere'

who command examples
To show a list of all the users currently logged in to the system, type:
$ who
root@linuxforfreshers.com:~$ who
root     :0           2016-10-03 07:46 (:0)
root     pts/61       2016-10-04 08:08 (:0)
root     pts/46       2016-10-04 07:58 (:0)

To show only hostname and user associated with stdin (usually keyboard), enter:
$ who -m
root@linuxforfreshers.com:~$ who -m
root     pts/61       2016-10-04 08:08 (:0)

To show active processes spawned by init:
$ who -p

To show user’s message status as +, – or ?, enter:
$ who -T

root@linuxforfreshers.com:~$ who -T
root     ? :0           2016-10-03 07:46 (:0)
root     + pts/61       2016-10-04 08:08 (:0)
root     + pts/46       2016-10-04 07:58 (:0)

Show or list users logged in
Type the command:
$ who -u
root@linuxforfreshers.com:~$ who -u
root     :0           2016-10-03 07:46   ?          3431 (:0)
root     pts/61       2016-10-04 08:08   .         21700 (:0)
root     pts/46       2016-10-04 07:58 01:46       21700 (:0)

Show time of last system boot
To display time of last system boot pass the -b option to who command:
$ who -b
root@linuxforfreshers.com:~$ who -b
         system boot  2016-10-03 07:44

Show dead processes on the system
You need pass the -d option to who command:
$ who -d
OR
$ who -d -H
root@linuxforfreshers.com:~$ who -d
         pts/9        2016-10-03 16:56                 0 id=/9    term=0 exit=0
         pts/58       2016-10-03 16:56                 0 id=/58   term=0 exit=0
         pts/0        2016-10-03 16:56                 0 id=/0    term=0 exit=0
         pts/60       2016-10-04 15:55                 0 id=/60   term=0 exit=0
         pts/62       2016-10-04 15:55                 0 id=/62   term=0 exit=0

Show system login processes
To just display system login processes pass the -l option:
$ who -l
OR
$ who -l -H
root@linuxforfreshers.com:~$ who -l
LOGIN    tty4         2016-10-03 07:44              1675 id=4
LOGIN    tty5         2016-10-03 07:44              1679 id=5
LOGIN    tty2         2016-10-03 07:44              1687 id=2
LOGIN    tty3         2016-10-03 07:44              1688 id=3
LOGIN    tty6         2016-10-03 07:44              1691 id=6
LOGIN    tty1         2016-10-03 07:44              3142 id=1

Count all login names and number of users logged on the system
To count all login names and number of users logged on:
$ who -q
root@linuxforfreshers.com:~$  who -q
root root root
# users=3

Display the current runlevel
To display the current system runlevel, type:
$ who -r
root@linuxforfreshers.com:~$ who -r
         run-level 2  2016-10-03 07:44

You can combine -r and -b options as follows:
$ who -r -b
root@linuxforfreshers.com:~$ who -r -b
         system boot  2016-10-03 07:44
         run-level 2  2016-10-03 07:44
root@linuxforfreshers.com:~$ who am i
root     pts/61       2016-10-04 08:08 (:0)

Displays "all" information, and headers above each column of data, for example:

root@linuxforfreshers.com:~$ who -aH
NAME       LINE         TIME             IDLE          PID COMMENT  EXIT
           system boot  2016-10-03 07:44
           run-level 2  2016-10-03 07:44
LOGIN      tty4         2016-10-03 07:44              1675 id=4
LOGIN      tty5         2016-10-03 07:44              1679 id=5
LOGIN      tty2         2016-10-03 07:44              1687 id=2
LOGIN      tty3         2016-10-03 07:44              1688 id=3
LOGIN      tty6         2016-10-03 07:44              1691 id=6
LOGIN      tty1         2016-10-03 07:44              3142 id=1
root     ? :0           2016-10-03 07:46   ?          3431 (:0)
           pts/9        2016-10-03 16:56                 0 id=/9    term=0 exit=0
           pts/58       2016-10-03 16:56                 0 id=/58   term=0 exit=0
root     + pts/61       2016-10-04 08:08   .         21700 (:0)
           pts/0        2016-10-03 16:56                 0 id=/0    term=0 exit=0
root     + pts/46       2016-10-04 07:58 01:39       21700 (:0)
           pts/60       2016-10-04 15:55                 0 id=/60   term=0 exit=0
           pts/62       2016-10-04 15:55                 0 id=/62   term=0 exit=0







Sunday, October 2, 2016

lsblk command examples in linux ?

lsblk command is used to list information about all available block devices, however, it does not list information about RAM disks. Examples of block devices are hard disk, flash drives, CD-ROM…

Install lsblk
Ubuntu and Linux Mint installation
The command lsblk comes in the package util-linux.

sudo apt-get install util-linux -y
Fedora and CentOS installation

sudo yum install util-linux-ng
How to use lsblk command
lsblk command by default will list all block devices in a tree-like format.
Example: Type lsblk in your terminal:

root@Linuxforfreshers ~ $ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465,8G  0 disk
sda1   8:1    0   100M  0 part
sda2   8:2    0    80G  0 part
sda3   8:3    0 297,9G  0 part
sda4   8:4    0     1K  0 part
sda5   8:5    0    28G  0 part /
sda6   8:6    0   3,7G  0 part [SWAP]
└─sda7   8:7    0  56,2G  0 part /home
sr0     11:0    1  1024M  0 rom
There are seven columns namely:
NAME: This is the device name.
MAJ:MIN: This column shows the major and minor device number.
RM: This column shows whether the device is removable or not.
SIZE: This is column give information on the size of the device.
RO: This indicates whether a device is read-only.
TYPE:This column shows information whether the block device is a disk or a partition(part) within a disk.
MOUNTPOINT: This column indicates mount point on which the device is mounted.


List all devices

To show list all devices including empty devices use following command:

lsblk -a
Example output:

root@Linuxforfreshers ~ $ lsblk -a
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465,8G  0 disk
sda1   8:1    0   100M  0 part
sda2   8:2    0    80G  0 part
sda3   8:3    0 297,9G  0 part
sda4   8:4    0     1K  0 part
sda5   8:5    0    28G  0 part /
sda6   8:6    0   3,7G  0 part [SWAP]
└─sda7   8:7    0  56,2G  0 part /home
sr0     11:0    1  1024M  0 rom 
ram0     1:0    0    64M  0 disk
ram1     1:1    0    64M  0 disk
ram2     1:2    0    64M  0 disk
ram3     1:3    0    64M  0 disk
ram4     1:4    0    64M  0 disk
ram5     1:5    0    64M  0 disk
ram6     1:6    0    64M  0 disk
ram7     1:7    0    64M  0 disk
ram8     1:8    0    64M  0 disk
ram9     1:9    0    64M  0 disk
loop0    7:0    0         0 loop
loop1    7:1    0         0 loop
loop2    7:2    0         0 loop
loop3    7:3    0         0 loop
loop4    7:4    0         0 loop
loop5    7:5    0         0 loop
loop6    7:6    0         0 loop
loop7    7:7    0         0 loop
ram10    1:10   0    64M  0 disk
ram11    1:11   0    64M  0 disk
ram12    1:12   0    64M  0 disk
ram13    1:13   0    64M  0 disk
ram14    1:14   0    64M  0 disk
ram15    1:15   0    64M  0 disk
List Device Permissions and Owner

To display information related to the owner, group and mode of the block device, use the -m option.

root@Linuxforfreshers ~ $ lsblk -m
NAME     SIZE OWNER GROUP MODE
sda    465,8G root  disk  brw-rw----
sda1   100M root  disk  brw-rw----
sda2    80G root  disk  brw-rw----
sda3 297,9G root  disk  brw-rw----
─sda4     1K root  disk  brw-rw----
sda5    28G root  disk  brw-rw----
sda6   3,7G root  disk  brw-rw----
└─sda7  56,2G root  disk  brw-rw----
sr0     1024M root  cdrom brw-rw----
Print the SIZE column in bytes

Use the -b option to achieve this :

root@Linuxforfreshers ~ $ lsblk -b
NAME   MAJ:MIN RM         SIZE RO TYPE MOUNTPOINT
sda      8:0    0 500107862016  0 disk
sda1   8:1    0    104857600  0 part
sda2   8:2    0  85878374400  0 part
sda3   8:3    0 319815680000  0 part
sda4   8:4    0         1024  0 part
sda5   8:5    0  29998710784  0 part /
sda6   8:6    0   3999268864  0 part [SWAP]
└─sda7   8:7    0  60298362880  0 part /home
sr0     11:0    1   1073741312  0 rom
Hide the information related to slaves

If you do not want to display slave related information, use the -d option.

root@Linuxforfreshers ~ $ lsblk -d
NAME MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda    8:0    0 465,8G  0 disk

sr0   11:0    1  1024M  0 rom  

Saturday, October 1, 2016

How to Check the Connection to MySQL DB Server in linux?


I would share the way to check the connection to MySQL database server. We can use the telnet or nc command to connect to port 3306. This would be very useful when we do the troubleshooting or to test the connectivity between the application server and the database server. Sample outputs will show succeeded or connected as below :

Host A = 192.168.0.5
Host B = 192.168.0.6

1. Test connectivity using nc command :

[root@linuxforfreshers~]# nc -z -w1 192.168.0.6 3306
Connection to 192.168.0.6 3306 port [tcp/mysql] succeeded!
2. Test connectivity using telnet command :
syntax: telnet hostname portnumber

[root@linuxforfreshers~]#  telnet  192.168.0.6 3306
Telnet escape character is 'X'.
Trying 192.168.0.6...
Connected to 192.168.0.6.
Escape character is 'X'.


telnet> Connection closed.