Tuesday, April 05, 2016

Top 10 linux commands

Hello and welcome to our blog. In this tutorial we are going to learn 14 useful linux commands
like Uptime,Users,Sudo,cmp,lsusb, lsmod,lsblk ,ifconfig interface up,ifconfig interface down,dd,lsop,bc,last reboot,Vim and Watch. Let us start with command uptime

Uptime

The command uptime will show you the how long system has been running, how many users currently logged on and system load averages.
[root@faraz ~]# uptime
14:23:55 up  2:36,  3 users,  load average: 0.05, 0.18, 0.12
[root@faraz ~]#

Users

This command will print the names of users currently logged in to current host. Here it is showing two times login fahmed users and one time root.
[root@faraz ~]# users
fahmed fahmed root
[root@faraz ~]#

Sudo

This will execute the command as another user with privileges and allow the user to work as superuser as specified by a security policy. First you need to edit the visudo file and make an entry for user with privileges.  In the section group wheel make entry for fahmed user (%fahmed ALL=(ALL)       ALL)
[root@faraz ~]# visudo

## Allows people in group wheel to run all commands

%wheel  ALL=(ALL)       ALL
%fahmed ALL=(ALL)       ALL
Now you are able to use commands with privileges and you have the rights to execute.
[fahmed@faraz ~]$ sudo systemctl start smb
[fahmed@faraz ~]$
So far we have been starting/stopping services start with root user but now  here sudo user can also start/stop the service as well.

cmp

This command compare two files byte by byte and will display difference.
Syntax cmp file1 file2
[fahmed@faraz ~]$ cmp newstring.file upload.txt
newstring.file upload.txt differ: byte 1, line 1
[fahmed@faraz ~]$

lsusb

This command will list the usb devices. As you can see in below image I have connected my usb with Kingston technology.
[fahmed@faraz ~]$ lsusb
Bus 001 Device 002: ID 0951:16a3 Kingston Technology
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
[fahmed@faraz ~]$

lsmod

This command will show the status of module in the Linux Kernel.
[fahmed@faraz ~]$ lsmod
Module                  Size  Used by
bnep                   19704  2
bluetooth             372944  5 bnep
rfkill                 26536  2 bluetooth
fuse                   87741  3
xt_CHECKSUM            12549  1
ipt_MASQUERADE         12678  3
nf_nat_masquerade_ipv4    13412  1 ipt_MASQUERADE
tun                    27141  1
ip6t_rpfilter          12546  1
ip6t_REJECT            12939  2
ipt_REJECT             12541  4
xt_conntrack           12760  18
ebtable_nat            12807  0
ebtable_broute         12731  0
bridge                119562  1 ebtable_broute
stp                    12976  1 bridge
llc                    14552  2 stp,bridge

lsblk

This command will lists the information about all available or specified block devices gather information from filesystem.
[fahmed@faraz ~]$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   15G  0 disk
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 14.5G  0 part
  ├─centos-root 253:0    0   13G  0 lvm  /
  └─centos-swap 253:1    0  1.5G  0 lvm  [SWAP]
sr0              11:0    1 1024M  0 rom

[fahmed@faraz ~]$ lsblk -l
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   15G  0 disk
sda1          8:1    0  500M  0 part /boot
sda2          8:2    0 14.5G  0 part
centos-root 253:0    0   13G  0 lvm  /
centos-swap 253:1    0  1.5G  0 lvm  [SWAP]
sr0          11:0    1 1024M  0 rom
[fahmed@faraz ~]$

Enable an Interface

You can enable system’s interface up by defining the name of the interface with ifconfig command. In CentOS/RedHat 7 interface card name is enp0s3. Older version has eth0.
Syntax ifconfig  interface-name  up
[fahmed@faraz ~]$ sudo ifconfig enp0s3 up
[fahmed@faraz ~]$

Disable an Interface

You can also disable the interface card.
Syntax ifconfig interface-name down
[fahmed@faraz ~]$ sudo ifconfig enp0s3 down
[fahmed@faraz ~]$

dd

This command will copy a file, converts and formats data according to the operands. This command is mostly used to copy the iso image into usb device or other media devices through if= input file and of=output file.
[root@faraz ~]# dd if=/dev/dvdrw of=/home/fahmed/Downloads/CentOS.iso
Be careful when you are defining the name of device (dvd or usb etc)

lsop

This command will list all the information about opened file in a file system (Like regular file, library file, network related file etc). Here I am going to show first top ten opened file because of lots of files are opened there  in backend.
[root@faraz ~]# lsof | head -10
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
COMMAND     PID   TID           USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
systemd       1                 root  cwd       DIR              253,0      4096        128 /
systemd       1                 root  rtd       DIR              253,0      4096        128 /
systemd       1                 root  txt       REG              253,0   1494056   51226345 /usr/lib/systemd/systemd
systemd       1                 root  mem       REG              253,0     20032   33730331 /usr/lib64/libuuid.so.1.3.0
systemd       1                 root  mem       REG              253,0    252704   34397999 /usr/lib64/libblkid.so.1.1.0
systemd       1                 root  mem       REG              253,0     90632   33730310 /usr/lib64/libz.so.1.2.7
systemd       1                 root  mem       REG              253,0     19888   33870478 /usr/lib64/libattr.so.1.1.0
systemd       1                 root  mem       REG              253,0     19520   33596345 /usr/lib64/libdl-2.17.so
systemd       1                 root  mem       REG              253,0    153192   33660599 /usr/lib64/liblzma.so.5.0.99
[root@faraz ~]#
bc
This command is basically to calculate two variables in a CLI mode its work just like a calculator.
[root@faraz ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.

6+2
8

6*11
66
Last reboot
This command will show the last reboot times or system reboots history.
[root@faraz ~]# last reboot
reboot   system boot  3.10.0-327.4.4.e Fri Mar 18 11:47 - 14:58  (03:10)
reboot   system boot  3.10.0-327.10.1. Tue Mar 15 22:40 - 14:58 (2+16:17)
reboot   system boot  3.10.0-327.4.4.e Tue Mar 15 16:39 - 16:51  (00:11)
reboot   system boot  3.10.0-327.4.4.e Tue Mar 15 00:59 - 01:24  (00:25)
reboot   system boot  3.10.0-327.4.4.e Tue Mar 15 00:05 - 00:54  (00:49)
reboot   system boot  3.10.0-327.10.1. Mon Mar 14 06:03 - 00:54  (18:50)
reboot   system boot  3.10.0-327.4.4.e Mon Mar 14 04:10 - 05:36  (01:25)
reboot   system boot  3.10.0-327.4.4.e Sun Mar 13 23:02 - 05:36  (06:33)
reboot   system boot  3.10.0-327.4.4.e Thu Mar 10 23:06 - 23:24  (00:18)
Vim
You can open file with vi or vim but how can we open file with or go to that particular line. Here I am going to open file with particular line. Syntax vi +line-number file-name
[fahmed@faraz ~]$ vi +10 newstring.file
vim number line filename
Watch
This command is to execute a program and watch the program output changing over time. By default its run on every 2 seconds. Its display output and errors in full screen. It is basically watching the program in continuous form with time interval. You can run multiple commands at a time inside the quotes.
[root@faraz ~]# watch -n 1 'ls -l; echo; df'
watch program command
In this tutorial we have learnt 14 useful linux commands
like Uptime,Users,Sudo,cmp,lsusb, lsmod,lsblk ,ifconfig interface up,ifconfig interface down,dd,lsop,bc,last reboot,Vim and Watch. I hope you have enjoyed this tutorial and will see you in

No comments :