About nslookup

So here we wil learn about NSLOOKUP (Name Server Lookup) and we will see some of it’s examples.

So we will start with, What is nslookup

nslookup is the tool which lets you the enter the host-name or domain name and see it’s corresponding IP address. It also lets you to pass IP address and get corresponding hostname. (reverse DNS)

Installation, if your CentOS machine says nslookup command not found.

[root@server /]# nslookup google.com
-bash: nslookup: command not found

so here you will have to Install bind-utils to get nslookup command. Simply run yum install bind-utils to take advantage of nslookup command.

[root@server /]# yum install bind-utils

nslookup command examples.

nslookup command followed by domain/hostname will display IP address of the domain.

[root@server /]# nslookup unix.com
Non-authoritative answer:
Name:   unix.com

You can use -type option to get various dns details. For example to get MX records.

[root@server /]# nslookup -type=mx redhat.com

Non-authoritative answer:
redhat.com      mail exchanger = 10 us-smtp-inbound-2.mimecast.com.
redhat.com      mail exchanger = 10 us-smtp-inbound-1.mimecast.com.

To get nameserver of the domain.

[root@server /]# nslookup -type=ns google.com

Non-authoritative answer:
google.com      nameserver = ns3.google.com.
google.com      nameserver = ns2.google.com.
google.com      nameserver = ns1.google.com.
google.com      nameserver = ns4.google.com.

Reverse DNS

[root@server /]#  nslookup    name = origin-www2.redhat.com.

Authoritative answers can be found from:

To get Information using specific DNS server.

[root@server /]# nslookup redhat.com ns1.redhat.com
Server:         ns1.redhat.com

Name:   redhat.com

we have used the ns1.redhat.com as the DNS server. Here you may notice that, we don’t get any “Non-authoritative answer:” header, since ns1.redhat.com has all the zone information of redhat.com

Shell Script to reboot multiple servers.

Here is simple bash script where you can add the server names in the command line and then it will go on rebooting every server which you have listed.

Script Usage:-

[root@server /]# ./server-reboot.sh appserver1 sqlserver1 dataserver1

Above command will reboot appserver1 sqlserver1 and dataserver1, if you have not setup ssh keys it will ask for password.

To use this script create a file called server-reboot.sh and following contents.

for server in "${@}"
scriptPath=$(dirname $0)
command=`base64 -w0 $script`
ssh -t $server "echo $command | base64 -d | sudo bash"

Create a another file named boot.txt in the same location where you have created server-reboot.sh.

for ((i=03; i>0; i--)); do sleep 1 ; printf "\rREBOOTING SERVER in $i Second. PRESS CTRL + C to cancel " ; done
shutdown -rf now

Save both the files, give execute permissions to server-reboot.sh and start rebooting.

It’s just not reboot script but you can add any commands which you want to run on multiple Linux servers in the file boot.txt

There are many other ways to do the same task but this works best for my environment. Add in comments what works best for you.

Cut Command examples in Linux.

What is “cut” command in Linux?

“Cut” Command is a utility for cutting sections from each line of the Files. It can be used to cut parts of a line by byte position, character and delimiter. It can also be used to cut data from file formats like CSV.

Examples of Cut Command.
You can specify byte position using the option -b, it will show only that letter, I am using the letter JustGeek here.
If you want to display only first letter i.e. J

[root@server ]# echo "JustGeek" | cut -b 1

If you want to display the letter J,S,G i.e. 1,3 and 5th Letter then you can specify using “,”

[root@server ]# echo "JustGeek" | cut -b 1,3,5
[root@server ]#

Cut based on a delimiter:-
To cut using a delimiter use the -d option. This is normally used in conjunction with the -f option to specify the field that should be cut.
Create a file called names.csv with content below


The delimiter can be set to a comma with -d ‘,’. cut can then pull out the fields you want with the -f flag. Example

[root@server ]# cat names.csv | cut -d ',' -f 1
[root@server ]#

You can comma “,” separate for multiple entries. Example.

[root@server ]# cat names.csv | cut -d ',' -f 1,2

If you want display age then you can just specify position with -f. Example

[root@server ]# cat names.csv | cut -d ',' -f 3

And if you want to Display City, then:

[root@server ]# cat names.csv | cut -d ',' -f 4

If you want to Change Delimeter in display then you can use the option –output-delimiter as shown in example below.

[root@server ]# cat names.csv | cut -d ',' -f 1,2 --output-delimiter='---'
[root@server ]#

How to Check “cut” command version

 [root@server ]# cut --version
 cut (GNU coreutils) 8.22
 Copyright (C) 2013 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.
 Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

Difference between Softlink and Hardlink

Here we will see what’s exactly is soft-link first.

  1. Symbolic Link – Links the files and directories located across diferent file systems.
  2. A Softlink has different inode number, Original File and Soflinked file don’t have same inode number.
  3. Softlink can be created across different Filesystems.
  4. Deleting original folder will break Symlink

How to Identify Softlink
You can do ls -ltr, you will see a “l” in the start of the permissions.

[root@server ]# ls -ltr
lrwxrwxrwx 1 root root 24 Dec  9 08:57 symlink -> /usr/local/orginalfolder

How to create a softlink
Create a directory

[root@server ]# mkdir /usr/local/orginalfolder

and now you can create symlink using.

[root@server ]# ln -s /usr/local/orginalfolder symlink

In the output below, you will notice that Soft link and Orginal folder are different.

[root@server ]# stat symlink
  File: ‘symlink’ -> ‘/usr/local/orginalfolder’
  Size: 24              Blocks: 0          IO Block: 4096   symbolic link
Device: fd01h/64769d    Inode: 256014      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 08:57:22.942621767 +0000
Modify: 2019-12-09 08:57:19.394593754 +0000
Change: 2019-12-09 08:57:19.394593754 +0000
 Birth: -

[root@server ]# stat /usr/local/orginalfolder
  File: ‘/usr/local/orginalfolder’
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fd01h/64769d    Inode: 395887      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 08:56:45.194323755 +0000
Modify: 2019-12-09 08:56:44.055314766 +0000
Change: 2019-12-09 08:56:44.055314766 +0000
 Birth: -

What’s the Hard-link.

  1. The main thing is you can’t create Harlink for Directories.
  2. Unlike Softlink, Hardlink can’t be created across different file-systems.
  3. Hardlinks share same inode number.

How to create a Hardlink

[root@server ]# touch originalFile.txt
[root@server ]# ln originalFile.txt hardLink.txt

Now you see a hard-link is created with same-content.

[root@server ]# cat originalFile.txt hardLink.txt
Hey Buddy!!
Hey Buddy!!

You will also notice that both have same inode number.

[root@server ]# stat originalFile.txt hardLink.txt
  File: ‘originalFile.txt’
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 256018      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
 Birth: -
 File: ‘hardLink.txt’
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 256018      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
 Birth: -

and if you remove original file it does not affect Hardlink

[root@server ]#  rm -fv originalFile.txt
removed ‘originalFile.txt’

[root@server ]#  ll
total 4
-rw-r--r-- 1 root root 12 Dec  9 09:07 hardLink.txt

[root@server ]#  cat hardLink.txt
Hey Buddy!!
[root@server ]# 

Error: /bin/bash^M: bad interpreter

How to fix below error?

[root@server /]# ./script test
 bash: ./script: /bin/bash^M: bad interpreter: No such file or directory

There nultiple way to fix this error, the problem is with the file format, you need to convert it to Unix format and your error should be gone.
You can fire the command below to fix.

[root@server /]# dos2unix filename

You can try steps below

 Open file, vi script
 then press ESC and enter :set fileformat=unix
 save file and try to run script again.