Linux Tricks

Linux Command-line Tricks

In this article, I am going to share with you the collection of several interesting, unique command-line tricks.
If you are a Linux administrator or Linux user, let’s start with some Linux command line tricks to enhance the creativeness at the terminal.

1) Only one user to allow to switch on root account as an administration user.

Sometimes, a sysadmin have some kind of the requirements to allow only one user to switch on root account. Use the following steps,

a.) Add user on “wheel” group

#gpasswd -a <user name> <group name> (Where, “a” Add the user to the named group.)

[[email protected] ~]# gpasswd -a gaurav wheel 
Adding user gaurav to group wheel  
[[email protected] ~]# cat /etc/group | grep wheel 
wheel:x:10:gaurav 
[[email protected] ~]#

We added “gaurav” user on “wheel” group.

b.) Edit the /etc/pam.d/su file.

#vim /etc/pam.d/su Uncomment the following line(line no. 6).

#%PAM-1.0 
auth            sufficient      pam_rootok.so 
# Uncomment the following line to implicitly trust users in the "wheel" group. 
#auth           sufficient      pam_wheel.so trust use_uid 
# Uncomment the following line to require a user to be in the "wheel" group. 
auth            required        pam_wheel.so use_uid 
auth            substack        system-auth 
auth            include         postlogin 
account         sufficient      pam_succeed_if.so uid = 0 use_uid quiet 
account         include         system-auth 
password        include         system-auth 
session         include         system-auth 
session         include         postlogin 
session         optional        pam_xauth.so 
 
~ 
~ 
:wq

c.) Now try to check with user login.

[[email protected] ~]$ su - root 
Password:  
Last login: Fri Sep 23 06:13:30 EDT 2016 from 192.168.1.5 on pts/1 
[[email protected] ~]# exit 
logout 
[[email protected] ~]$ su - anil 
Password:  
Last login: Mon Sep 19 11:32:05 EDT 2016 on pts/1 
[[email protected] ~]$ su - root 
Password:  
su: Permission denied 
[[email protected] ~]$

As shown, “gaurav” user can switch to root account but “anil” user not able to login on root account and showing Permission denied.

2) Run the last command as any user.

#!!

[[email protected] ~]# cat /etc/group | grep wheel 
wheel:x:10:gaurav 
[[email protected] ~]# !! 
cat /etc/group | grep wheel 
wheel:x:10:gaurav 
[[email protected] ~]#

3) Find out the current shell name.

You can identify your current shell using following command, #echo $SHELL or echo $0

[[email protected] ~]# echo $SHELL 
/bin/bash 
[[email protected] ~]# echo $0 
-bash 
[[email protected] ~]#

Where, “$SHELL” is the environment variable that stores the name of current shell and  “$0” shows the name of program.

4) Some Tip and Tricks for shutdown command.

# shutdown -r now or init 6

[[email protected] ~]# shutdown -r now 
PolicyKit daemon disconnected from the bus. 
We are no longer a registered authentication agent. 
Connection to 192.168.1.10 closed by remote host. 
 
It will reboot the system.

#shutdown -h now or init 0

[[email protected] ~]# shutdown -h now 
 
It will shutdown the server now (i.e power off)

# shutdown 06:45

[[email protected] ~]# date 
Fri Sep 23 06:39:54 EDT 2016 
[[email protected] ~]# shutdown 06:45 
Shutdown scheduled for Fri 2016-09-23 06:45:00 EDT, use 'shutdown -c' to cancel. 
[[email protected] ~]#

System will shutdown at a specific time.

# shutdown -c

[[email protected] ~]# shutdown -c 
Broadcast message from [email protected] (Fri 2016-09-23 06:41:44 EDT): 
The system shutdown has been cancelled at Fri 2016-09-23 06:42:44 EDT! 
[[email protected] ~]#

As shown, system shutdown has been cancelled.

5) Download rpm file in specific directory by using downloadonly plugin.

Make specific directory and install “yum-utils“. (If you want, how to configure the yum server, refer below link.)

Configure the Yum Server in Centos7/RHEL7/Fedora22

[[email protected] /]# mkdir /package 
[[email protected] ]# yum install yum-utils 
Loaded plugins: fastestmirror 
Loading mirror speeds from cached hostfile 
Resolving Dependencies 
--> Running transaction check 
 
. . . .  
 
 
Transaction Summary 
============================================ 
Install  1 Package (+2 Dependent packages) 
 
Total download size: 607 k 
Installed size: 2.8 M 
Is this ok [y/d/N]: y

Now, Download the rpm file.

[[email protected] sambapackages]# yumdownloader --destdir=/package  httpd 
Loaded plugins: fastestmirror 
Loading mirror speeds from cached hostfile 
httpd-2.4.6-40.el7.centos.x86_64.rpm                                                                   | 2.7 MB  00:00:00      
[[email protected] sambapackages]# ls 
httpd-2.4.6-40.el7.centos.x86_64.rpm 
[[email protected] sambapackages]# yumdownloader --destdir=/package  samba 
Loaded plugins: fastestmirror 
Loading mirror speeds from cached hostfile 
samba-4.2.3-10.el7.x86_64.rpm                                                                          | 601 kB  00:00:00      
[[email protected] sambapackages]# ls 
httpd-2.4.6-40.el7.centos.x86_64.rpm  samba-4.2.3-10.el7.x86_64.rpm 
[[email protected] sambapackages]#

6) Close a terminal without saving the history in Linux.

Your shell’s history is saved in the file indicated by the HISTFILE variable.

#unset HISTFILE and #history -c (Where, “c” is used to Clear current session history.)

[[email protected] ~]$ unset HISTFILE 
[[email protected] ~]$ free -m 
              total        used        free      shared  buff/cache   available 
Mem:            993          96         688           6         207         747 
Swap:          2047           0        2047 
[[email protected] ~]$ w -u 
 07:11:23 up 33 min,  1 user,  load average: 0.00, 0.01, 0.05 
USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT 
root     pts/0    192.168.1.5      06:39    3.00s  0.35s  0.03s w -u 
[[email protected] ~]$ exit 
logout 
[[email protected] ~]# su - gaurav 
Last login: Fri Sep 23 07:10:57 EDT 2016 on pts/0 
[[email protected] ~]$ history  
    1  history  
[[email protected] ~]$

As show above, User “gaurav” not find any history for previous session.

7) Make a Multiple directories using one command.

Make some directories according to the following in one command.

[[email protected] ~]# mkdir -p linux/{Storage/{san,nas,das},Network/{security,firewall,switch},Database/{oracle,mysql},Application/{tomcat,apache}}
[[email protected] ~]# tree linux/
linux/
├── Application
│   ├── apache
│   └── tomcat
├── Database
│   ├── mysql
│   └── oracle
├── Network
│   ├── firewall
│   ├── security
│   └── switch
└── Storage
    ├── das
    ├── nas
    └── san

14 directories, 0 files
[[email protected] ~]#

Note: “tee” or “ls -R” command is used to check the status.If “tree” package is not installed then install it, using ” yum install -y tree” command.

8)  check the system’s reboot date and time.

#who -b

[[email protected] ~]# who -b 
         system boot  2016-09-23 06:38 
[[email protected] ~]#

#last reboot

[[email protected] ~]# last reboot 
reboot   system boot  3.10.0-327.el7.x Fri Sep 23 06:38 - 07:16  (00:38)     
reboot   system boot  3.10.0-327.el7.x Fri Sep 23 05:59 - 07:16  (01:17)     
reboot   system boot  3.10.0-327.el7.x Fri Sep 23 05:56 - 07:16  (01:20)

9) Set Hostname permanenelty in centos7/rhel7/fedora22.

#hostnamectl set-hostname <hostname> (Where, hostnamectl is used to control the system hostname).

[[email protected] ~]# hostname 
localhost 
[[email protected] ~]# hostnamectl set-hostname server1 
[[email protected] ~]# hostname 
server1 
[[email protected] ~]#

Note: Re-login on the session, it will display “server1” instead of “localhost”.

10) Delete the files within the ranges between 20 to 100 days.

# find <directory location> -mtime +20 -mtime -100 -iname “*.dbf” -delete

[[email protected] log]# date 
Fri Sep 23 16:58:50 EDT 2016 
[[email protected] log]# ll 
total 0 
-rw-r--r--. 1 root root 0 Aug  2 18:01 aug1.dbf 
-rw-r--r--. 1 root root 0 Aug  2 18:01 aug2.dbf 
-rw-r--r--. 1 root root 0 Aug  2 18:01 aug3.dbf 
-rw-r--r--. 1 root root 0 Aug  2 18:01 aug4.dbf 
-rw-r--r--. 1 root root 0 Jul 15 18:00 jul1.dbf 
-rw-r--r--. 1 root root 0 Jul 15 18:00 jul2.dbf 
-rw-r--r--. 1 root root 0 Jul 15 18:00 jul3.dbf 
-rw-r--r--. 1 root root 0 Jul 15 18:00 jul4.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep10.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep1.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep2.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep3.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep4.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep5.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep6.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep7.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep8.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep9.dbf 
[[email protected] log]# find /root/log/ -mtime +20 -mtime -100 -iname "*.dbf" -delete 
[[email protected] log]# ll 
total 0 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep10.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep1.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep2.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep3.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep4.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep5.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep6.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep7.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep8.dbf 
-rw-r--r--. 1 root root 0 Sep 23 16:57 sep9.dbf 
[[email protected] log]#

As shown, all the files within the ranges beween 20 to 100 days are deleted.