Tuesday, 4 September 2018

14 useful ‘ls’ command examples for linux beginners

ls Command, LPI Study Materials, LPI Guides, LPI Tutorial and Materials

The ls (list) command displays a list of files and directories. It supports several options some of them are listed in the below table along with a short description of each. In this post we will discuss 14 different examples of ls command which can be useful for beginners.

Syntax of ls command :

# ls [options] [file | directory]

Option Description 
-a Lists hidden files also. If a file or directory name starts with a dot, it is referred to as hidden. 
-F  Displays file types. Shows / for directories, * for executable files, @ for symbolic links and nothing for text files. 
-lh   Displays long listing with file sizes in human readable format. 
-l  Displays long listing with detailed file information including file type, permissions, link count, owner, group, size, date and time 
of  last modification, and name of the file.-ld Displays long listing of the specified directory, but hides its contents. 
-R  Lists contents of the specified directory and all its sub-directories (recursive listing). 
-lt  Lists all files sorted by date and time with the newest file first. 
-ltr  Lists all files sorted by date and time with the oldest file first. 

Note : For more detailed options of ls command please refer ls man page.

Example:1 list the files & directory of your current working directory


[root@localhost /]# ls
bin boot cgroup dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@localhost /]#

Example:2 To list files in the current directory with detailed information


[root@localhost /]# ls -l
total 94
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 bin
dr-xr-xr-x.  5 root root  1024 Nov 23 05:29 boot
drwxr-xr-x  10 root root  4096 Nov 23 03:42 cgroup
drwxr-xr-x  18 root root  3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root  4096 Dec 12 21:32 etc
drwxr-xr-x   8 root root  4096 Jul 19 02:43 home
dr-xr-xr-x.  8 root root  4096 May  3  2014 lib
dr-xr-xr-x.  8 root root 12288 Nov 23 03:13 lib64
-rw-r--r--   1 root root    72 Dec 12 21:40 linux-stuff
drwx------.  2 root root 16384 May  3  2014 lost+found
drwxr-xr-x.  2 root root  4096 Sep 23  2011 media
drwxr-xr-x.  3 root root  4096 Jun 22 01:14 mnt
..................................................

Example:3 List the contents of a specific directory


[root@localhost /]# ls -l /tmp/
total 8
-rw-r--r-- 1 root root 0 Aug 3 14:31 bad-blocks.txt
drwx------. 2 root root 4096 May 3 2014 gpg-auCCFn
-rw-------. 1 root root 0 May 3 2014 yum.log
-rw------- 1 root root 1428 Nov 23 03:58 yum_save_tx-2014-11-23-03-58BHO8Jg.yumtx

To list only directory entries instead of contents, use ‘-ld‘ option. Example is shown below

[root@localhost /]# ls -ld /tmp/
drwxrwxrwt. 4 root root 4096 Dec 12 21:34 /tmp/

Example:4 To display all files in the current directory with their file types


[root@localhost /]# ls -F
bin/ cgroup/ etc/ lib/ linux-stuff media/ opt/ root/ selinux/ sys/ usr/
boot/ dev/ home/ lib64/ lost+found/ mnt/ proc/ sbin/ srv/ tmp/ var/
[root@localhost /]#

When we use ‘-F‘ option with ls command , then in the output ‘/’ will appended at every directory and for normal files nothing is appended.

Example:5 To list all files in the current directory with detailed information and sorted by date & time with the newest file first.


[root@localhost /]# ls -lt
total 94
-rw-r--r--   1 root root    72 Dec 12 21:40 linux-stuff
dr-xr-xr-x  13 root root     0 Dec 12 21:36 sys
drwxrwxrwt.  4 root root  4096 Dec 12 21:34 tmp
drwxr-xr-x  18 root root  3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root  4096 Dec 12 21:32 etc
dr-xr-xr-x  95 root root     0 Dec 12 21:31 proc
dr-xr-xr-x.  5 root root  1024 Nov 23 05:29 boot
drwxr-xr-x  10 root root  4096 Nov 23 03:42 cgroup
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 sbin
dr-xr-xr-x.  8 root root 12288 Nov 23 03:13 lib64
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 bin
dr-xr-x---.  5 root root  4096 Nov 23 02:53 root
drwxr-xr-x. 18 root root  4096 Aug 24 05:14 var
drwxr-xr-x   8 root root  4096 Jul 19 02:43 home
............................

Example:6 To display all files in the current directory with their sizes in human readable format( e.g 1K, 234M, 2G )


[root@localhost /]# ls -lh
total 94K
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1.0K Nov 23 05:29 boot
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3.6K Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
-rw-r--r-- 1 root root 72 Dec 12 21:40 linux-stuff
drwx------. 2 root root 16K May 3 2014 lost+found
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
drwxr-xr-x. 2 root root 4.0K Jun 22 01:25 opt
......................................

Example:7 To list all files, including the hidden files, in the current directory with detailed information


[root@localhost /]# ls -la
total 102
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 .
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 ..
-rw-r--r-- 1 root root 0 Dec 12 21:32 .autofsck
-rw-r--r-- 1 root root 0 Jul 19 02:17 .autorelabel
dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
-rw-r--r-- 1 root root 0 Dec 12 21:48 .linux-rocks
-rw-r--r-- 1 root root 72 Dec 12 21:40 linux-stuff
-rw-r--r-- 1 root root 0 Dec 12 21:49 .linux-tips
drwx------. 2 root root 16384 May 3 2014 lost+found
drwxr-xr-x. 2 root root 4096 Sep 23 2011 media
..................................................................

Example:8 To list contents of the /etc directory recursively


[root@localhost /]# ls -R /etc/
/etc/:
adjtime
aliases
aliases.db
alternatives
anacrontab
audisp
audit
bash_completion.d
bashrc
blkid
centos-release
..........................

Example:9 List the files sorted by their size


[root@localhost /]# ls -lhS
total 13M
-rw-r--r-- 1 root root 12M Dec 12 22:05 linux-stuff
drwx------. 2 root root 16K May 3 2014 lost+found
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
..............................................
Above command will display those files and directory first which are bigger in size.

Example:10 List the inode numbers of files & directories using ‘-i’ option


[root@localhost /]# ls -li 
total 12378
 786436 dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
 2 dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
 655361 drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
 3 drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
 393217 drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
 2 drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
 917506 dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
 262146 dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
 2668 -rw-r--r-- 1 root root 12582912 Dec 12 22:05 linux-stuff
...................................................................

Example:11 Display the current version of ls command using ‘–version’


[root@localhost /]# ls --version 
ls (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Example:12 List uid & gid of files or directories using ‘-n’ option


[root@localhost /]# ls -n 
total 14476
-rw-rw-r-- 1 1000 1000 193 Aug 16 16:10 1
-rw-rw-r-- 1 1000 1000 262 Nov 28 12:35 Anjali and Aakash help Yohan-B3MzR5cTpd2mLBSZfkPKppip8vCTKUdd.mp4.part
-rw-rw-r-- 1 1000 1000 1849048 Jun 7 2014 BINGO from Super Simple Songs-9mmF8zOlh_g.mp4.part
drwxr-xr-x 18 1000 1000 4096 Dec 13 08:59 Desktop
drwxr-xr-x 2 1000 1000 4096 Sep 27 13:09 Documents
drwxr-xr-x 2 1000 1000 4096 Dec 2 10:10 Downloads
drwx------ 3 1000 1000 4096 Nov 30 15:36 Dropbox
-rw-rw-r-- 1 1000 1000 94296 Nov 20 02:00 dropbox_2.10.0_amd64.deb
-rw-r--r-- 1 1000 1000 8980 Apr 20 2014 examples.desktop
-rw-rw-r-- 1 1000 1000 203 Oct 26 16:02 fs.sh
drwxrwxr-x 2 1000 1000 4096 Sep 1 14:31 Google Drive
................................................................

Example:13 Default Aliases of ls command


Type the alias command on the terminal to display the default aliases set for ls command.

[root@localhost /]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

Example:14 Enable timestamps in the output of ls command using “–time-style” option


[root@localhost /]# ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
total 12378
dr-xr-xr-x. 2 root root 4096 2014-11-23 11-23 03:13 bin
dr-xr-xr-x. 5 root root 1024 2014-11-23 11-23 05:29 boot
drwxr-xr-x 10 root root 4096 2014-11-23 11-23 03:42 cgroup
drwxr-xr-x 18 root root 3680 2014-12-12 12-12 21:32 dev
drwxr-xr-x. 64 root root 4096 2014-12-12 12-12 21:32 etc
drwxr-xr-x 8 root root 4096 2014-07-19 07-19 02:43 home
dr-xr-xr-x. 8 root root 4096 2014-05-03 05-03 05:24 lib
dr-xr-xr-x. 8 root root 12288 2014-11-23 11-23 03:13 lib64
-rw-r--r-- 1 root root 12582912 2014-12-12 12-12 22:05 linux-stuff
drwx------. 2 root root 16384 2014-05-03 05-03 05:19 lost+found
drwxr-xr-x. 2 root root 4096 2011-09-23 09-23 07:50 media
......................................................................

Saturday, 1 September 2018

17 useful tar command with practical examples in Linux

Tar (tape archive ) is the most widely used command in Unix like operating system for creating archive of multiple files and folders into a single archive file and that archive file can be further compressed using  gzip and bzip2 techniques. In other words we can say that tar command is used to take backup by archiving multiple files and directory into a single tar or archive file and later on files & directories can be extracted from the tar compressed file.

Tar Command, Linux, Linux Command, LPI Study Materials

In this article we will discuss 17 practical examples of tar command in Linux.

Syntax of tar command:

# tar <options> <files>

Some of the commonly used options in tar command are listed below :

Tar Command, Linux, Linux Command, LPI Study Materials

Note : hyphen ( – ) in the tar command options are optional.

Example: 1 Create a tar archive file


Let’s create a tar file of /etc directory and ‘/root/ anaconda-ks.cfg’ file.

[root@lpicentral ~]# tar -cvf myarchive.tar /etc /root/anaconda-ks.cfg

Above command will create a tar file with the name “myarchive” in the current folder. Tar file contains all the files and directories of /etc folder and anaconda-ks.cfg file.

In the tar command ‘-c‘ option specify to create a tar file, ‘-v’ is used for verbose output and ‘-f’ option is used to specify the archive file name.

[root@lpicentral ~]# ls -l myarchive.tar
-rw-r--r--. 1 root root 22947840 Sep 7 00:24 myarchive.tar
[root@lpicentral ~]#

Example:2 List the contents of tar archive file.


Using ‘–t‘ option in tar command we can view the contents of tar files without extracting it.

[root@lpicentral ~]# tar -tvf myarchive.tar

Listing a specific file or directory from tar file. In the below example i am trying to view whether ‘anaconda-ks.cfg’ file is there in the tar file or not.

[root@lpicentral ~]# tar -tvf myarchive.tar root/anaconda-ks.cfg
-rw------- root/root 953 2016-08-24 01:33 root/anaconda-ks.cfg
[root@lpicentral ~]#

Example:3 Append or add files to end of archive or tar file.


‘-r‘ option in the tar command is used to append or add file to existing tar file. Let’s add /etc/fstab file in ‘data.tar‘

[root@lpicentral ~]# tar -rvf data.tar /etc/fstab

Note: In the Compressed tar file we can’t append file or directory.

Example:4 Extracting files and directories from tar file.


‘-x‘ option is used to extract the files and directories from the tar file. Let’s extract the content of above created tar file.

[root@lpicentral ~]# tar -xvf myarchive.tar

This command will extract all the files and directories of myarchive tar file in the current working directory.

Example:5 Extracting tar file to a particular folder.


In case you want to extract tar file to a particular folder or directory then use ‘-C‘ option and after that specify the path of a folder.

[root@lpicentral ~]# tar -xvf myarchive.tar -C /tmp/

Example:6 Extracting particular file or directory from tar file.


Let’s assume you want to extract only anaconda-ks.cfg file from the tar file under /tmp folder.

Syntax :

# tar –xvf {tar-file } {file-to-be-extracted } -C {path-where-to-extract}

[root@lpicentral tmp]# tar -xvf /root/myarchive.tar root/anaconda-ks.cfg -C /tmp/
root/anaconda-ks.cfg
[root@lpicentral tmp]# ls -l /tmp/root/anaconda-ks.cfg
-rw-------. 1 root root 953 Aug 24 01:33 /tmp/root/anaconda-ks.cfg
[root@lpicentral tmp]#

Example:7 Creating and compressing tar file (tar.gz or .tgz )


Let’s assume that we want to create a tar file of /etc and /opt folder and also want to compress it using gzip tool. This can be achieved using ‘-z‘ option in tar command. Extensions of such tar files will be either tar.gz or .tgz

[root@lpicentral ~]# tar -zcpvf myarchive.tar.gz /etc/ /opt/

Or

[root@lpicentral ~]# tar -zcpvf myarchive.tgz /etc/ /opt/

Example:8 Creating and compressing tar file ( tar.bz2 or .tbz2 )


Let’s assume that we want to create compressed (bzip2) tar file of /etc and /opt folder. This can be achieved by using the option ( -j) in the tar command.Extensions of such tar files will be either tar.bz2 or .tbz

[root@lpicentral ~]# tar -jcpvf myarchive.tar.bz2 /etc/ /opt/

Or

[root@lpicentral ~]# tar -jcpvf myarchive.tbz2 /etc/ /opt/

Example:9 Excluding particular files or type while creating tar file.


Using “–exclude” option in tar command we can exclude the particular file or file type while creating tar file. Let’s assume we want to exclude the file type of html while creating the compressed tar file.

[root@lpicentral ~]# tar -zcpvf myarchive.tgz /etc/ /opt/ --exclude=*.html

Example:10 Listing the contents of tar.gz or .tgz file


Contents of tar file with the extensions tar.gz or .tgz is viewed by using the option ‘-t’. Example is shown below :

[root@lpicentral ~]# tar -tvf myarchive.tgz  | more 
............................................. 
drwxr-xr-x root/root         0 2016-09-07 08:41 etc/ 
-rw-r--r-- root/root       541 2016-08-24 01:23 etc/fstab 
-rw------- root/root         0 2016-08-24 01:23 etc/crypttab 
lrwxrwxrwx root/root         0 2016-08-24 01:23 etc/mtab -> /proc/self/mounts 
-rw-r--r-- root/root       149 2016-09-07 08:41 etc/resolv.conf 
drwxr-xr-x root/root         0 2016-09-06 03:55 etc/pki/ 
drwxr-xr-x root/root         0 2016-09-06 03:15 etc/pki/rpm-gpg/ 
-rw-r--r-- root/root      1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 
-rw-r--r-- root/root      1004 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7 
-rw-r--r-- root/root      1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7 
-rw-r--r-- root/root      3140 2015-09-15 06:53 etc/pki/rpm-gpg/RPM-GPG-KEY-foreman 
..........................................................

Example:11 Listing the contents of tar.bz2 or .tbz2 file.


Contents of tar file with the extensions tar.bz2 or .tbz2 is viewed by using the option ‘-t’. Example is shown below :

[root@lpicentral ~]# tar -tvf myarchive.tbz2  | more 
........................................................ 
rwxr-xr-x root/root         0 2016-08-24 01:25 etc/pki/java/ 
lrwxrwxrwx root/root         0 2016-08-24 01:25 etc/pki/java/cacerts -> /etc/pki/ca-trust/extracted/java/cacerts 
drwxr-xr-x root/root         0 2016-09-06 02:54 etc/pki/nssdb/ 
-rw-r--r-- root/root     65536 2010-01-12 15:09 etc/pki/nssdb/cert8.db 
-rw-r--r-- root/root      9216 2016-09-06 02:54 etc/pki/nssdb/cert9.db 
-rw-r--r-- root/root     16384 2010-01-12 16:21 etc/pki/nssdb/key3.db 
-rw-r--r-- root/root     11264 2016-09-06 02:54 etc/pki/nssdb/key4.db 
-rw-r--r-- root/root       451 2015-10-21 09:42 etc/pki/nssdb/pkcs11.txt 
-rw-r--r-- root/root     16384 2010-01-12 15:45 etc/pki/nssdb/secmod.db 
drwxr-xr-x root/root         0 2016-08-24 01:26 etc/pki/CA/ 
drwxr-xr-x root/root         0 2015-06-29 08:48 etc/pki/CA/certs/ 
drwxr-xr-x root/root         0 2015-06-29 08:48 etc/pki/CA/crl/ 
drwxr-xr-x root/root         0 2015-06-29 08:48 etc/pki/CA/newcerts/ 
drwx------ root/root         0 2015-06-29 08:48 etc/pki/CA/private/ 
drwx------ root/root         0 2015-11-20 06:34 etc/pki/rsyslog/ 
drwxr-xr-x root/root         0 2016-09-06 03:44 etc/pki/pulp/ 
..............................................................

Example:12 Extracting or unzip tar.gz or .tgz files.


tar files with extension tar.gz or .tgz is extracted or unzipped with option ‘-x’ and ‘-z’. Example is shown below :

[root@lpicentral ~]# tar -zxpvf myarchive.tgz -C /tmp/

Above command will extract tar file under /tmp folder.

Note : Now a days tar command will take care compression file types automatically while extracting, it means it is optional for us to specify compression type in tar command. Example is shown below :

[root@lpicentral ~]# tar -xpvf myarchive.tgz -C /tmp/

Example:13 Extracting or unzip tar.bz2 or .tbz2 files


tar files with the extension tar.bz2 or .tbz2 is extract with option ‘-j’ and ‘-x’. Example is shown below:

[root@lpicentral ~]# tar -jxpvf myarchive.tbz2 -C /tmp/

Or

[root@lpicentral ~]# tar xpvf myarchive.tbz2 -C /tmp/

Example:14 Scheduling backup with tar command


There are some real time scenarios where we have to create the tar files of particular files and directories for backup purpose on daily basis. Let’s suppose we have to take backup of whole /opt folder on daily basis, this can be achieved by creating a cron job of tar command. Example is shown below :

[root@lpicentral ~]# tar -zcvf optbackup-$(date +%Y-%m-%d).tgz /opt/

Create a cron job for above command.

Example:15 Creating compressed archive or tar file with -T and -X option.


There are some real time scenarios where we want tar command to take input from a file and that file will consists of path of files & directory that are to be archived and compressed, there might some files that we would like to exclude in the archive which are mentioned in input file.

In the tar command input file is specified after ‘-T’ option and file which consists of exclude list is specified after ‘-X’ option.

Let’s suppose we want to archive and compress the directories like /etc , /opt and /home and want to exclude the file ‘/etc/sysconfig/kdump’ and ‘/etc/sysconfig/foreman‘, Create a text file ‘/root/tar-include’ and ‘/root/tar-exclude’ and put the following contents in respective file.

[root@lpicentral ~]# cat /root/tar-include
/etc
/opt
/home
[root@lpicentral ~]#
[root@lpicentral ~]# cat /root/tar-exclude
/etc/sysconfig/kdump
/etc/sysconfig/foreman
[root@lpicentral ~]#

Now run the below command to create and compress archive file.

[root@lpicentral ~]# tar zcpvf mybackup-$(date +%Y-%m-%d).tgz -T /root/tar-include -X /root/tar-exclude

Example:16 View the size of .tar , .tgz and .tbz2 file.


Use the below commands to view the size of tar and compressed tar files.

[root@lpicentral ~]# tar -czf - data.tar | wc -c
427
[root@lpicentral ~]# tar -czf - mybackup-2016-09-09.tgz | wc -c
37956009
[root@lpicentral ~]# tar -czf - myarchive.tbz2 | wc -c
30835317
[root@lpicentral ~]#

Example:17 Splitting big tar file into smaller files.


In Unix like operating big file is divided or split into smaller files using split command. Big tar file can also be divided into the smaller parts using split command.

Let’s assume we want to split ‘mybackup-2016-09-09.tgz‘ file into smaller parts of each 6 MB.

Syntax :  split -b <Size-in-MB> <tar-file-name>.<extension> “prefix-name”

[root@lpicentral ~]# split -b 6M mybackup-2016-09-09.tgz mybackup-parts

Above command will split the mybackup compressed tar file into the smaller files each of size 6 MB in current working directory and split file names will starts from mybackup-partsaa … mybackup-partsag. In case if you want to append numbers in place of alphabets then use ‘-d’ option in above split command.

[root@lpicentral ~]# ls -l mybackup-parts*
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaa
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsab
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsac
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsad
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsae
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaf
-rw-r--r--. 1 root root 637219  Sep 10 03:05 mybackup-partsag
[root@lpicentral ~]#

Now we can move these files into another server over the network and then we can merge all the files into a single tar compressed file using below mentioned command.

[root@lpicentral ~]# cat mybackup-partsa* > mybackup-2016-09-09.tgz
[root@lpicentral ~]#

Thursday, 30 August 2018

20 Sed (Stream Editor) Command Examples for Linux Users

Sed Command, Linux Tutorial and Materials, Linux Guides, Linux Study Materials

Sed command or Stream Editor is very powerful utility offered by Linux/Unix systems. It is mainly used for text substitution , find & replace but it can also perform other text manipulations like insertion, deletion, search etc. With SED, we can edit complete files without actually having to open it. Sed also supports the use of regular expressions, which makes sed an even more powerful test manipulation tool.

In this article, we will learn to use SED command with the help some examples. Basic syntax for using sed command is,

sed OPTIONS… [SCRIPT] [INPUTFILE…]

Sed Command, Linux Tutorial and Materials, Linux Guides, Linux Study Materials

Now let’s see some examples.

Example :1) Displaying partial text of a file


With sed, we can view only some part of a file rather than seeing whole file. To see some lines of the file, use the following command,

[lpicentral@localhost ~]$ sed -n 22,29p testfile.txt

here, option ‘n’ suppresses printing of whole file & option ‘p’ will print only line lines from 22 to 29.

Example :2) Display all except some lines


To display all content of a file except for some portion, use the following command,

[lpicentral@localhost ~]$ sed 22,29d testfile.txt

Option ‘d’ will remove the mentioned lines from output.

Example :3) Display every 3rd line starting with Nth line


Do display content of every 3rd line starting with line number 2 or any other line, use the following command

[lpicentral@localhost ~]$ sed -n '2-3p' file.txt

Example :4 ) Deleting a line using sed command


To delete a line with sed from a file, use the following command,

[lpicentral@localhost ~]$ sed Nd testfile.txt

where ‘N’ is the line number & option ‘d’ will delete the mentioned line number. To delete the last line of the file, use

[lpicentral@localhost ~]$ sed $d testfile.txt

Example :5) Deleting a range of lines


To delete a range of lines from the file, run

[lpicentral@localhost ~]$ sed '29-34d' testfile.txt

This will delete lines 29 to 34 from testfile.txt file.

Example :6) Deleting lines other than the mentioned


To delete lines other than the mentioned lines from a file, we will use ‘!’

[lpicentral@localhost ~]$ sed '29-34!d' testfile.txt

here ‘!’ option is used as not, so it will reverse the condition i.e. will not delete the lines mentioned. All the lines other 29-34 will be deleted from the files testfile.txt.

Example :7) Adding Blank lines/spaces


To add a blank line after every non-blank line, we will use option ‘G’,

[lpicentral@localhost ~]$ sed G testfile.txt

Example :8) Search and Replacing a string using sed


To search & replace a string from the file, we will use the following example,

[lpicentral@localhost ~]$ sed 's/danger/safety/' testfile.txt

here option ‘s’ will search for word ‘danger’ & replace it with ‘safety’ on every line for the first occurrence only.

Example :9) Search and replace a string from whole file using sed


To replace the word completely from the file, we will use option ‘g’  with ‘s’,

[lpicentral@localhost ~]$ sed 's/danger/safety/g' testfile.txt

Example :10) Replace the nth occurrence of string pattern


We can also substitute a string on nth occurrence from a file. Like replace ‘danger’ with ‘safety’ only on second occurrence,

[lpicentral@localhost ~]$ sed ‘s/danger/safety/2’ testfile.txt

To replace ‘danger’ on 2nd occurrence of every line from whole file, use

[lpicentral@localhost ~]$ sed 's/danger/safety/2g' testfile.txt

Example :11) Replace a string on a particular line


To replace a string only from a particular line, use

[lpicentral@localhost ~]$ sed '4 s/danger/safety/' testfile.txt

This will only substitute the string from 4th line of the file. We can also mention a range of lines instead of a single line,

[lpicentral@localhost ~]$  sed '4-9 s/danger/safety/' testfile.txt

Example :12) Add a line after/before the matched search


To add a new line with some content after every pattern match, use option ‘a’ ,

[lpicentral@localhost ~]$ sed '/danger/a "This is new line with text after match"' testfile.txt

To add a new line with some content a before every pattern match, use option ‘i’,

[lpicentral@localhost ~]$ sed '/danger/i "This is new line with text before match" ' testfile.txt

Example :13) Change a whole line with matched pattern


To change a whole line to a new line when a search pattern matches we need to use option ‘c’ with sed,

[lpicentral@localhost ~]$ sed '/danger/c "This will be the new line" ' testfile.txt

So when the pattern matches ‘danger’, whole line will be changed to the mentioned line.

Advanced options with sed


Up until now we were only using simple expressions with sed, now we will discuss some advanced uses of sed with regex,

Example :14) Running multiple sed commands


If we need to perform multiple sed expressions, we can use option ‘e’ to chain the sed commands,

[lpicentral@localhost ~]$  sed -e 's/danger/safety/g' -e 's/hate/love/' testfile.txt

Example :15) Making a backup copy before editing a file


To create a backup copy of a file before we edit it, use option ‘-i.bak’,

[lpicentral@localhost ~]$ sed -i.bak -e 's/danger/safety/g'  testfile.txt

This will create a backup copy of the file with extension .bak. You can also use other extension if you like.

Example :16) Delete a file line starting with & ending with a pattern


To delete a file line starting with a particular string & ending with another string, use

[lpicentral@localhost ~]$ sed -e 's/danger.*stops//g' testfile.txt

This will delete the line with ‘danger’ on start & ‘stops’ in the end & it can have any number of words in between , ‘.*’ defines that part.

Example :17) Appending lines


To add some content before every line with sed & regex, use

[lpicentral@localhost ~]$ sed -e 's/.*/testing sed &/' testfile.txt

So now every line will have ‘testing sed’ before it.

Example :18) Removing all commented lines & empty lines


To remove all commented lines i.e. lines with # & all the empty lines,  use

[lpicentral@localhost ~]$ sed -e 's/#.*//;/^$/d' testfile.txt

To only remove commented lines, use

[lpicentral@localhost ~]$ sed -e 's/#.*//' testfile.txt

Example :19) Get list of all usernames from /etc/passwd


To get the list of all usernames from /etc/passwd file, use

[lpicentral@localhost ~]$  sed 's/\([^:]*\).*/\1/' /etc/passwd

a complete list all usernames will be generated on screen as output.

Example :20) Prevent overwriting of system links with sed command


‘sed -i’ command has been know to remove system links & create only regular files in place of the link file. So to avoid such a situation & prevent ‘sed -i‘ from destroying the links, use ‘–follow-symklinks‘ options with the command being executed.

Let’s assume i want to disable SELinux on CentOS or RHEL Severs

[lpicentral@localhost ~]# sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

These were some examples to show sed, we can use these reference to employ them as & when needed.

Tuesday, 28 August 2018

10 useful ncat (nc) Command Examples for Linux Systems

ncat or nc is networking utility with functionality similar to cat command but for network. It  is a general purpose CLI tool for reading, writing, redirecting data across a network. It is  designed to be a reliable back-end tool that can be used with scripts or other programs.  It’s also a great tool for network debugging, as it can create any kind of connect one can need.

ncat (nc) Command, Linux Systems, LPI Study Materials, LPI Guides, LPI Learning

ncat/nc can be a port scanning tool, or a security tool, or monitoring tool and is also a simple TCP proxy.  Since it has so many features, it is known as a network swiss army knife. It’s one of those tools that every System Admin should know & master.

In most of Debian distributions ‘nc’ is available and its package is automatically installed during installation. But in minimal CentOS 7 / RHEL 7 installation you will not find nc as a default package. You need to install using the following command.

[root@lpicentral ~]# yum install nmap-ncat -y

System admins can use it audit their system security, they can use it find the ports that are opened & than secure them. Admins can also use it as a client for auditing web servers, telnet servers, mail servers etc, with ‘nc’ we can control every character sent & can also view the responses to sent queries.

We can also cause it to capture data being sent by client to understand what they are upto.

In this tutorial, we are going to learn about how to use ‘nc’ command with 10 examples,

Example: 1) Listen to inbound connections


Ncat can work in listen mode & we can listen for inbound connections on port number with option ‘l’. Complete command is,

$ ncat -l port_number

For example,

$ ncat -l 8080

Server will now start listening to port 8080 for inbound connections.

Example: 2) Connect to a remote system


To connect to a remote system with nc, we can use the following command,

$ ncat IP_address port_number

Let’s take an example,

$ ncat 192.168.1.100 80

Now a connection to server with IP address 192.168.1.100 will be made at port 80 & we can now send instructions to server. Like we can get the complete page content with

GET / HTTP/1.1

or get the page name,

GET / HTTP/1.1

or we can get banner for OS fingerprinting with the following,

HEAD / HTTP/1.1

This will tell what software is being used to run the web Server.

Example: 3) Connecting to UDP ports


By default , the nc utility makes connections only to TCP ports. But we can also make connections to UDP ports, for that we can use option ‘u’,

$ ncat -l -u 1234

Now our system will start listening a udp port ‘1234’, we can verify this using below netstat command,

$ netstat -tunlp | grep 1234
udp           0          0 0.0.0.0:1234                 0.0.0.0:*               17341/nc
udp6          0          0 :::1234                      :::*                    17341/nc

Let’s assume we want to send or test UDP port connectivity to a specific remote host, then use the following command,

$ ncat -v -u {host-ip} {udp-port}

example:

[root@localhost ~]# ncat -v -u 192.168.105.150 53
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Connected to 192.168.105.150:53.


Example: 4) NC as chat tool


NC can also be used as chat tool, we can configure server to listen to a port & than can make connection to server from a remote machine on same port & start sending message. On server side, run

$ ncat -l 8080

On remote client machine, run

$ ncat 192.168.1.100 8080

Than start sending messages & they will be displayed on server terminal.

Example: 5) NC as a proxy


NC can also be used as a proxy with a simple command. Let’s take an example,

$ ncat -l 8080 | ncat 192.168.1.200 80

Now all the connections coming to our server on port 8080 will be automatically redirected to 192.168.1.200 server on port 80. But since we are using a pipe, data can only be transferred & to be able to receive the data back, we need to create a two way pipe. Use the following commands to do so,

$ mkfifo 2way
$ ncat -l 8080 0<2way | ncat 192.168.1.200 80 1>2way

Now you will be able to send & receive data over nc proxy.

Example: 6) Copying Files using nc/ncat


NC can also be used to copy the files from one system to another, though it is not recommended & mostly all systems have ssh/scp installed by default. But none the less if you have come across a system with no ssh/scp, you can also use nc as last ditch effort.

Start with machine on which data is to be received & start nc is listener mode,

$ ncat -l  8080 > file.txt

Now on the machine from where data is to be copied, run the following command,

$ ncat 192.168.1.100 8080 --send-only < data.txt

Here, data.txt is the file that has to be sent. –send-only option will close the connection once the file has been copied. If not using this option, than we will have press ctrl+c to close the connection manually.

We can also copy entire disk partitions using this method, but it should be done with caution.

Example: 7) Create a backdoor via nc/nact


NC command can also be used to create backdoor to your systems & this technique is actually used by hackers a lot. We should know how it works in order to secure our system. To create a backdoor, the command is,

$ ncat -l 10000 -e /bin/bash

‘e‘ flag attaches a bash to port 10000. Now a client can connect to port 10000 on server & will have complete access to our system via bash,

$ ncat 192.168.1.100 1000


Example: 8) Port forwarding via nc/ncat


We can also use NC for port forwarding with the help of option ‘c’ , syntax for accomplishing port forwarding is,

$ ncat -u -l  80 -c  'ncat -u -l 8080'

Now all the connections for port 80 will be forwarded to port 8080.

Example: 9) Set Connection timeouts


Listener mode in ncat will continue to run & would have to be terminated manually. But we can configure timeouts with option ‘w’,

$ ncat -w 10 192.168.1.100 8080

This will cause connection to be terminated in 10 seconds, but it can only be used on client side & not on server side.

Example: 10) Force server to stay up using -k option in ncat


When client disconnects from server, after sometime server also stops listening. But we can force server to stay connected & continuing port listening with option ‘k’. Run the following command,

$ ncat -l -k 8080

Now server will stay up, even if a connection from client is broken.

Saturday, 25 August 2018

11 Useful split command examples for Linux/UNIX systems

As the name suggests ‘split‘ command is used to split or break a file into the pieces in Linux and UNIX systems. Whenever we split a large file with split command then split output file’s default size is 1000 lines and its default prefix would be ‘x’.

In this article we will discuss 11 useful split command examples for Linux Users. Apart from this we will also discuss how split files can be merge or reassembled into a single file. Syntax for split command:

# split {options} {file_name} {prefix}

Some of the important options of split command is shown below:

Linux/UNIX Systems, Split Command, Linux Guides, Linux Certifications, LPI Study Materials

Example: 1) Split File into Pieces


Let’s assume we have file name with tuxlap.txt, Use below split command to break into the pieces

[root@lpicentral ~]# split tuxlap.txt
[root@lpicentral ~]# ll
total 32
-rw-------. 1 root root  980 Aug 12 00:11 anaconda-ks.cfg
-rw-r--r--. 1 root root 9607 Nov 11 03:22 tuxlap.txt
-rw-r--r--. 1 root root 8744 Nov 11 03:23 xaa
-rw-r--r--. 1 root root  863 Nov 11 03:23 xab
[root@lpicentral ~]#

As we can see the above output ‘tuxlab.txt‘ is split into two pieces with the name ‘xaa’ and ‘xab’.

Example: 2) Split Command with verbose option


We can run split command in verbose mode with option ‘–verbose‘, example is shown below:

[root@lpicentral ~]# split tuxlap.txt --verbose
creating file ‘xaa’
creating file ‘xab’
[root@lpicentral ~]#

Example: 3) Split files with customize line numbers (-l)


Let’s suppose we want to split a file with customize line numbers, let say I want max 200 lines per file.

To achieve this, use  ‘-l’ option in split command.

[root@lpicentral ~]# split -l200 tuxlap.txt --verbose
creating file ‘xaa’
creating file ‘xab’
creating file ‘xac’
creating file ‘xad’
creating file ‘xae’
creating file ‘xaf’
[root@lpicentral ~]#

Verify the lines of each file using below command

[root@lpicentral ~]# wc -l xa*
 200 xaa
 200 xab
 200 xac
 200 xad
 200 xae
  91 xaf
1091 total
[root@lpicentral ~]#

Example: 4) Split files with file size using option  -b


Using Split command we can split a file with file size. Use the following syntax to split files with size in bytes, KB , MB and GB

# split  -b{bytes}  {file_name}

# split  -b  nK      {file_name}    // n is the numeric value

# split  -b   nM    {file_name}      // n is the numeric value

# split  -b   nG     {file_name}     // n is the numeric value

Split file based on bytes:

[root@lpicentral ~]# split -b2000000 tuxlap.txt

Split file based on KB:

[root@lpicentral ~]# split -b 50K tuxlap.txt

Split file based on MB:

[root@lpicentral ~]# split -b 50M tuxlap.txt

Split file based on GB:

[root@lpicentral ~]# split -b 1G tuxlap.txt

Example: 5) Create Split files with numeric suffix instead of alphabetic (-d)


In the above examples we have seen that split command output files are created with alphabetic suffix like xaa, xab….. xan , Use ‘-d’ option with split command to create split output files with numeric suffix like x00, x01, … x0n

[root@lpicentral ~]# split -d tuxlap.txt

[root@lpicentral ~]# ll
total 1024256
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rwx------. 1 root root 1048576000 Nov 11 03:54 linux-lite.iso
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
-rw-r--r--. 1 root root      11998 Nov 11 04:41 x00
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x01
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x02
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x03
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x04
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x05
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x06
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x07
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x08
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x09
-rw-r--r--. 1 root root         12 Nov 11 04:41 x10
[root@lpicentral ~]#

Example: 6) Split file with Customize Suffix


With split command we can create split output files with customize suffix. Let’s assume we want to create split output files with customize suffix

Syntax:

# split  {file_name}  {prefix_name}

[root@lpicentral ~]# split tuxlap.txt split_file_

[root@lpicentral ~]# ll
total 1024248
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rwx------. 1 root root 1048576000 Nov 11 03:54 linux-lite.iso
-rw-r--r--. 1 root root      11998 Nov 11 04:56 split_file_aa
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ab
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ac
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ad
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ae
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_af
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ag
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ah
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ai
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_aj
-rw-r--r--. 1 root root         12 Nov 11 04:56 split_file_ak
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
[root@lpicentral ~]#

Example: 7) Generate n chunks output files with split command (-n)


Let’s suppose we want to split an iso file into 4 chunk output files. Use ‘-n’ option with split command limit the number of split output files.

[root@lpicentral ~]# split -n5 linux-lite.iso

Verify the Split out files using ll command.

[root@lpicentral ~]# ll
total 2048124
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rwx------. 1 root root 1048576000 Nov 11 03:54 linux-lite.iso
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
-rw-r--r--. 1 root root  209715200 Nov 11 05:22 xaa
-rw-r--r--. 1 root root  209715200 Nov 11 05:22 xab
-rw-r--r--. 1 root root  209715200 Nov 11 05:22 xac
-rw-r--r--. 1 root root  209715200 Nov 11 05:23 xad
-rw-r--r--. 1 root root  209715200 Nov 11 05:23 xae
[root@lpicentral ~]#

Example: 8) Prevent Zero Size Split output files with option (-e)


There can be some scenarios where we split a small file into a large number of chunk files and zero size split output files can be created in such cases, so to avoid zero size split output file, use the option ‘-e’

[root@lpicentral ~]# split -n60 -e tuxlap.txt
[root@lpicentral ~]# ls -l x*
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xaa
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xab
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xac
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xad
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xae
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xaf
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xag
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xah
.............
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xce
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xcf
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xcg
-rw-r--r--. 1 root root 2010 Nov 11 05:34 xch
[root@lpicentral ~]#

Example:9) Create Split output files of customize suffix length (-a option)


Let’s suppose we want to split an iso file and where size of each split output file is 500MB and suffix length is to be 3.  Use the following split command:

[root@lpicentral ~]# split -b 500M linux-lite.iso -a 3

[root@lpicentral ~]# ll
total 2048124
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rwx------. 1 root root 1048576000 Nov 11 03:54 linux-lite.iso
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
-rw-r--r--. 1 root root  524288000 Nov 11 05:43 xaaa
-rw-r--r--. 1 root root  524288000 Nov 11 05:43 xaab
[root@lpicentral ~]#

Example: 10) Split ISO file and merge it into a single file.


Let’s suppose we have a Windows Server ISO file of size 4.2 GB and we are unable to scp this file to remote server because of its size.

To resolve such type of issues we can split the ISO into n number of pieces and will copy these pieces to remote sever and on the remote server we can merge these pieces into a single file using cat command,

[root@lpicentral ~]# split -b 800M Windows2012r2.iso Split_IS0_

View the split output files using ll command,

[root@lpicentral ~]# ll
total 8871788
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_aa
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_ab
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_ac
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_ad
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_ae
-rw-r--r--. 1 root root  347987968 Nov 11 06:29 Split_IS0_af
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
-rwx------. 1 root root 4542291968 Nov 11 06:03 Windows2012r2.iso
[root@lpicentral ~]#

Now scp these files to remote server and merge these files into a single using cat command

[root@lpicentral ~]# cat Split_IS0_a* > Windows_Server.iso
[root@lpicentral ~]#

Example: 11) Verify the Integrity of Merge file using md5sum utility


As per Example 10, once the split output files are merged into a single file, then we can check the integrity of actual & merge file with md5sum utility. Example is shown below:

[root@lpicentral ~]# md5sum Windows2012r2.iso
5b5e08c490ad16b59b1d9fab0def883a  Windows2012r2.iso
[root@lpicentral ~]#

[root@lpicentral ~]# md5sum Windows_Server.iso
5b5e08c490ad16b59b1d9fab0def883a  Windows_Server.iso
[root@lpicentral ~]#

As per the above output, it is confirm that integrity is maintained and we can also say split file are successfully restored to a single file.

Friday, 24 August 2018

10 Quick Tips About sudo command for Linux systems

Overview


sudo stands for superuser do. It allows authorized users to execute command as an another user. Another user can be regular user or superuser. However, most of the time we use it to execute command with elevated privileges.

LPI Certification, LPI Guides, LPI Tutorial and Material, Sudo Command, LPI Linux Systems

sudo command works in conjunction with security policies, default security policy is sudoers and it is configurable via /etc/sudoers file. Its security policies are highly extendable. One can develop and distribute their own policies as plugins.

How it’s different than su


In GNU/Linux there are two ways to run command with elevated privileges:

◈ Using su command
◈ Using sudo command

su stands for switch user. Using su, we can switch to root user and execute command. But there are few drawbacks with this approach.

◈ We need to share root password with another user.
◈ We cannot give controlled access as root user is superuser
◈ We cannot audit what user is doing.

sudo addresses these problems in unique way.

1. First of all, we don’t need to compromise root user password. Regular user uses its own password to execute command with elevated privileges.
2. We can control access of sudo user meaning we can restrict user to execute only certain commands.
3. In addition to this all activities of sudo user are logged hence we can always audit what actions were done. On Debian based GNU/Linux all activities are logged in /var/log/auth.log file.

Later sections of this tutorial sheds light on these points.

Hands on with sudo


Now, we have fair understanding about sudo. Let us get our hands dirty with practical. For demonstration, I am using Ubuntu. However, behavior with another distribution should be identical.

Allow sudo access


Let us add regular user as a sudo user. In my case user’s name is lpicentral

1) Edit /etc/sudoers file as follows:

$ sudo visudo

2) Add below line to allow sudo access to user lpicentral:

lpicentral ALL=(ALL) ALL

In above command:

◈ lpicentral indicates user name
◈ First ALL instructs to permit sudo access from any terminal/machine
◈ Second (ALL) instructs sudo command to be allowed to execute as any user
◈ Third ALL indicates all command can be executed as root

Execute command with elevated privileges


To execute command with elevated privileges, just prepend sudo word to command as follows:

$ sudo cat /etc/passwd

When you execute this command, it will ask lpicentral’s password and not root user password.

Execute command as an another user


In addition to this we can use sudo to execute command as another user. For instance, in below command, user lpicentral executes command as a devesh user:

$ sudo -u devesh whoami
[sudo] password for lpicentral:
devesh

Built in command behavior


One of the limitation of sudo is – Shell’s built in command doesn’t work with it. For instance, history is built in command, if you try to execute this command with sudo then command not found error will be reported as follows:

$ sudo history
[sudo] password for lpicentral:
sudo: history: command not found

Access root shell

To overcome above problem, we can get access to root shell and execute any command from there including Shell’s built in.

To access root shell, execute below command:

$ sudo bash

After executing this command – you will observe that prompt sign changes to pound (#) character.

Recipes


In this section we’ll discuss some useful recipes which will help you to improve productivity. Most of the commands can be used to complete day-to-day task.

Execute previous command as a sudo user


Let us suppose you want to execute previous command with elevated privileges, then below trick will be useful:

$ sudo !4

Above command will execute 4th command from history with elevated privileges.

sudo command with Vim


Many times we edit system’s configuration files and while saving we realize that we need root access to do this. Because this we may lose our changes. There is no need to get panic, we can use below command in Vim to rescue from this situation:

:w !sudo tee %

In above command:

◈ Colon (:) indicates we are in Vim’s ex mode
◈ Exclamation (!) mark indicates that we are running shell command
◈ sudo and tee are the shell commands
◈ Percentage (%) sign indicates all lines from current line

Execute multiple commands using sudo


So far we have executed only single command with sudo but we can execute multiple commands with it. Just separate commands using semicolon (;) as follows:

$ sudo -- bash -c 'pwd; hostname; whoami'

In above command:

◈ Double hyphen (–) stops processing of command line switches
◈ bash indicates shell name to be used for execution
◈ Commands to be executed are followed by –c option

Run sudo command without password


When sudo command is executed first time then it will prompt for password and by default password will be cached for next 15 minutes. However, we can override this behavior and disable password authentication using NOPASSWD keyword as follows:

lpicentral ALL=(ALL) NOPASSWD: ALL


Restrict user to execute certain commands


To provide controlled access we can restrict sudo user to execute only certain commands. For instance, below line allows execution of echo and ls commands only

lpicentral ALL=(ALL) NOPASSWD: /bin/echo /bin/ls

Insights about sudo


Let us dig more about sudo command to get insights about it.

$ ls -l /usr/bin/sudo
-rwsr-xr-x 1 root root 145040 Jun 13  2017 /usr/bin/sudo

If you observe file permissions carefully, setuid bit is enabled on sudo. When any user runs this binary it will run with the privileges of the user that owns the file. In this case it is root user.

To demonstrate this, we can use id command with it as follows:

$ id
uid=1002(lpicentral) gid=1002(lpicentral) groups=1002(lpicentral)

When we execute id command without sudo then id of user lpicentral will be displayed.

$ sudo id
uid=0(root) gid=0(root) groups=0(root)

But if we execute id command with sudo then id of root user will be displayed.