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.)

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

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.

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

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.

#!!

[root@server1 ~]# cat /etc/group | grep wheel 
wheel:x:10:gaurav 
[root@server1 ~]# !! 
cat /etc/group | grep wheel 
wheel:x:10:gaurav 
[root@server1 ~]#

3) Find out the current shell name.

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

[root@server1 ~]# echo $SHELL 
/bin/bash 
[root@server1 ~]# echo $0 
-bash 
[root@server1 ~]#

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

[root@server1 ~]# 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

[root@server1 ~]# shutdown -h now 
 
It will shutdown the server now (i.e power off)

# shutdown 06:45

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

System will shutdown at a specific time.

# shutdown -c

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

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

[root@server1 /]# mkdir /package 
[root@server1 ]# 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.

[root@server1 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      
[root@server1 sambapackages]# ls 
httpd-2.4.6-40.el7.centos.x86_64.rpm 
[root@server1 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      
[root@server1 sambapackages]# ls 
httpd-2.4.6-40.el7.centos.x86_64.rpm  samba-4.2.3-10.el7.x86_64.rpm 
[root@server1 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.)

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

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.

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

14 directories, 0 files
[root@server1 ~]#

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

[root@server1 ~]# who -b 
         system boot  2016-09-23 06:38 
[root@server1 ~]#

#last reboot

[root@server1 ~]# 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).

[root@localhost ~]# hostname 
localhost 
[root@localhost ~]# hostnamectl set-hostname server1 
[root@localhost ~]# hostname 
server1 
[root@localhost ~]#

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

[root@server1 log]# date 
Fri Sep 23 16:58:50 EDT 2016 
[root@server1 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 
[root@server1 log]# find /root/log/ -mtime +20 -mtime -100 -iname "*.dbf" -delete 
[root@server1 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 
[root@server1 log]#

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