History Command Examples In Linux

To check history of the commands which has been executed.

[[email protected] ~]# history
    1  whoami
    2  clear
    3  ls
    4  whoaami
    5  ls -ltr
    6  top -c

To check last 5 commands in history use history 5

[[email protected] ~]# history 5
   92  clear
   93  whoami
   94  top -c
   95  history
   96  history 5

To clear history use history -c

[[email protected] ~]# history -c
[[email protected] ~]#
[[email protected] ~]# history
    1  history

To delete a particular command in history use history -d

[[email protected] ~]# history 5
1 history
2 whoami
3 clear
4 hisry 5
5 history 5

here I'm deleting the command which I fired at 4th Number
[[email protected] ~]# history -d 4
[[email protected] ~]# history
1 history
2 whoami
3 clear
4 history 5
5 history -d 4
6 history

To run a command at particular number in history you can use ! followed by the number. Example you want to run the command at number 2 the run !2

[[email protected] ~]# !2

TEE Command in Linux

TEE command is useful when you want to redirect the standard output to a file. Tee command should be already installed on your Linux Machine.

How to use Tee Command

[[email protected] ~]# cat file-1.txt | tee file-2.txt

This will overwrite all the contents of the file-2.txt with file-1.txt.
if want to keep the contents of the file-2.txt intact so you need -a option to append. As shown below.

[[email protected] ~]# cat file-1.txt | tee -a file-2.txt

Redirecting out of a command to a file. Most of us have habit of using >> to redirect output of a command to a file. But that’s work all the time where there is a error in the script.

So, to redirect output of a command to a file you need to use below command.
Example, my script has error but when I use >> to redirect output it won’t work.

[[email protected] scripts]# ./servicestatus.sh >> error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[[email protected] scripts]#

[[email protected] scripts]# ls -al error.log
-rw-r--r-- 1 root root 0 May 29 06:05 error.log
[[email protected] scripts]#

My error log file is blank, so here I will use TEE command which works perfect. It will re-direct stdout and stderr into a file.

[[email protected] scripts]# ./servicestatus.sh 2>&1 | tee error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[[email protected] scripts]#

[[email protected] scripts]# ls -al error.log
-rw-r--r-- 1 root root 66 May 29 06:08 error.log
[[email protected] scripts]#

[[email protected] scripts]# cat error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[[email protected] scripts]#

Generate and Add SSH keys to remote host.

Example: If you want to login from serverA to serverB without password then you need to follow the step below.
First Login to Server-A and generate SSH keys using ssh-keygen

[[email protected] /]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/justgeek/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/justgeek/.ssh/id_rsa.
Your public key has been saved in /home/justgeek/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|    .      ..oo..|
|   . . .  . .o.X.|
|    . . o.  ..+ B|
|   .   o.o  .+ ..|
|    ..o.S   o..  |
|   . %o=      .  |
|    @.B...     . |
|   o.=. o. . .  .|
|    .oo  E. . .. |

So you have successfully generated keys and now you need to copy public key to the remote host which you wish to login without password.

You need to append the key mentioned in /home/justgeek/.ssh/id_rsa.pub to server-b in the file /home/justgeek/.ssh/authorized_keys

So the file (/home/justgeek/.ssh/authorized_keys) on server-b will look something like this.

[[email protected]]$ cat /home/justgeek/.ssh/authorized_keys
ssh-rsa QFSJZPPFWWFa4sLrqXPNyY2gJWtef7ZBYFEc19sl6BjnhwMMRnBrcGX1JBlm3fWW8+DwmwrG73LEomYk5KZNKV1nCNjwhLCanmmZwv8R6TIOrMASV4aOIFvVWgYDlKfQsmqZFKQm2H5Pem7qUGdJ962I9ZeC8pqPwYPR2YMrWiffMBlBXfqhfjiZlxyhuPeBr2YwPEyPoJ1iSdMarG3HgbCTkcfYHn4L9RMLvN4wrgkN3n1b8ArR3JV7kg0IIvxAAYlTQaZtl0f70yLSSO0SI1ZTQryPC0hWCS5Uz5T12YtEC85ymYhA\4vOnKebfXhuCsiGiCY5zVWNfXBNdXcXyeUrqV9HyKtjHdpcH6iB7MNSiIRn5F74== [email protected]

Note: Key mentioned above is just an example, it’s not a real key.

Another, simple command to copy key to server-b is using ssh-copy-id

[[email protected] /]# ssh-copy-id [email protected]
[email protected]'s password:
X11 forwarding request failed on channel 0

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'server-b'"
and check to make sure that only the key(s) you wanted were added.

Bingo !! Now just do ssh [email protected] from server-a and password won’t be asked.

Dig Command Examples

Simple Dig Command

[[email protected] ~]# dig justgeek.net

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> justgeek.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53092
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

; EDNS: version: 0, flags:; udp: 4096
;justgeek.net.                  IN      A

justgeek.net.           267     IN      A
justgeek.net.           267     IN      A
justgeek.net.           267     IN      A

;; Query time: 10 msec
;; WHEN: Wed May 27 07:13:17 UTC 2020
;; MSG SIZE  rcvd: 89

How to check A record of the website using DIG command

[[email protected] ~]# dig redhat.com +noall +answer

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> redhat.com +noall +answer
;; global options: +cmd
redhat.com. 3547 IN A

Check MX record using dig command

[[email protected] ~]# dig redhat.com MX +noall +answer

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> redhat.com MX +noall +answer
;; global options: +cmd
redhat.com. 600 IN MX 10 us-smtp-inbound-2.mimecast.com.
redhat.com. 600 IN MX 10 us-smtp-inbound-1.mimecast.com.
[[email protected] ~]#

How to check NS record of the website using DIG command.

[[email protected] ~]# dig justgeek.net NS +noall +answer

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> justgeek.net NS +noall +answer
;; global options: +cmd
justgeek.net. 86400 IN NS kia.ns.cloudflare.com.
justgeek.net. 86400 IN NS jerome.ns.cloudflare.com.
[[email protected] ~]#