This tutorial is a continuation of Basic Linux Network Commands, despite the fact that it starts with easy commands for DNS lookup, all the network commands expressed here are essential in diagnosing problems that might be encountered in the Linux experience.
The nslookup and host commands to query DNS records
The nslookup is a network administration command-line tool available in many computers operating systems for querying the Domain Name System (DNS) to obtain domain name or IP address mapping, or other DNS records.
In the following example, the command nslookup is used to resolve the IP address of putorius.net
nslookup putorius.net
mcherisi@putorius:/$ nslookup putorius.net
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
Name: putorius.net
Address: 64.91.238.144
mcherisi@putorius.net:/$
The following example shows the mx records for putorius.net by applying the option -type=mx.
nslookup -type=mx putorius.net
mcherisi@putorius:/$ nslookup -type=mx putorius.net
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
putorius.net mail exchanger = 5 ALT2.ASPMX.L.GOOGLE.com.
putorius.net mail exchanger = 5 ALT1.ASPMX.L.GOOGLE.com.
putorius.net mail exchanger = 1 ASPMX.L.GOOGLE.com.
putorius.net mail exchanger = 10 ALT4.ASPMX.L.GOOGLE.com.
putorius.net mail exchanger = 10 ALT3.ASPMX.L.GOOGLE.com.
Authoritative answers can be found from:
mcherisi@putorius.net:/$
Alternatively, you could use the command host to get both lookup resolution and mx records:
host putorius.net
mcherisi@putorius:/$ host putorius.net
putorius.net has address 64.91.238.144
putorius.net is handled by 10 ALT3.ASPMX.L.GOOGLE.com.
putorius.net is handled by 5 ALT1.ASPMX.L.GOOGLE.com.
putorius.net is handled by 10 ALT4.ASPMX.L.GOOGLE.com.
putorius.net is handled by 1 ASPMX.L.GOOGLE.com.
putorius.net is handled by 5 ALT2.ASPMX.L.GOOGLE.com.
mcherisi@putorius:/$
The dig command to query DNS records
The dig command is an acronym for (Domain Information Groper). It is a utility that allows you to query DNS servers for information. This information is really advantageous when troubleshooting. Below we will show you basic usage and how to understand the output. For a deeper dive, you can read our Using Dig for DNS Troubleshooting article.
dig putorius.net
mcherisi@putorius:/$ dig putorius.net
; <<>> DIG 9.11.5-P4-5.1-Debian <<>> putorius.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6034
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;putorius.net. IN A
;; ANSWER SECTION:
putorius.net. 3599 IN A 64.91.238.144
;; Query time: 192 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Tue Sep 10 22:45:54 -03 2019
;; MSG SIZE rcvd: 58
How to understand the dig command output:
The first line ; <<>> DiG 9.11.5-P4-5.1-Debian <<>> putorius.net shows the dig version used.
The following lines specify dig got an answer to a query without errors, its ID number, flags section, in this case, show it is printing a query response (qr), Recursion Desired (rd) and Recursion Available (ra). The rest prints the number of results.
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6034
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
The next lines show EDNS (Extension mechanisms for DNS) compatibility, while the Question section shows what was asked (putorius.net Address).
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;putorius.net.
The answer section gives the result, putorius.net IP address (A).
;; ANSWER SECTION:
Putorius.net. 3599 IN A 64.91.238.144
The rest is an additional description of how the query was carried out, time, DNS server and size.
;; Query time: 192 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Tue Sep 10 22:45:54 -03 2019
;; MSG SIZE rcvd: 58
It is important to highlight all from all mentioned above only the lines without “;” are relevant, lines starting with “;” are comments.
By default, dig works through our local DNS (on Linux specified in the file /etc/resolv.conf). We can specify a DNS by adding a @ prefix.
The following example instructs dig to use the 1.1.1.1 DNS and avoid all comments with the option +short.
dig @1.1.1.1 putorius.net +short
mcherisi@putorius:/$ dig @1.1.1.1 putorius.net +short
64.91.238.144
mcherisi@putorius.net:/$
To end the introduction to dig, a final example showing the CNAME records for PUTORIUS:
dig putorius.net CNAME
mcherisi@putorius:/$ dig putorius.net CNAME
; <<>> DIG 9.11.5-P4-5.1-Debian <<>> putorius.net CNAME
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7851
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;putorius.net. IN CNAME
;; AUTHORITY SECTION:
putorius.net. 1799 IN SDA ns.liquidweb.com. admin.liquidweb.com. 2018121801 86400 7200 3600000 14400
;; Query time: 223 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Oct 28 11:22:06 -03 2019
;; MSG SIZE rcvd: 97
mcherisi@putorius.net:/$
You can use dig to get any kind of DNS record like MX records.
The traceroute command to diagnose network problems
All modern operating systems include the traceroute feature. The function of this advanced Linux network command is to diagnose network problems, including speed tests and packets which can’t reach the destination. When traffic goes through the internet each routing device intermediating is called “hop”, traceroute helps to identify what hops are affecting our traffic in some manner, for example, delaying it or blocking it.
The following example shows a simple traceroute, without options, for putorius.net
traceroute putorius.net
mcherisi@putorius:/$ traceroute putorius.net
traceroute to putorius.net (64.91.238.144). 30 hops max, 60 byte packets
1 192.168.0.1 (192.168.0.1) 1.85ms 1.579 ms
2 * * *
3 10.172.102.109 (10.172.102.109) 16.695 ms 20.178 ms 22.081 ms
4 10.200.0.10 (10.200.0.10) 20.851 ms 20.627 ms 20.374 ms
5 10.200.0.78 (10.200.0.78) 19.857 ms 19.505 ms 19.330 ms
6 10.169.26.1 (10.169.26.1) 20.207 ms 20.908 ms 19.248 ms
7 10.2.186.117 (10.2.186.117) 20.348 ms 19.994 ms 10.2.186.121 (10.2.186.121) 29.389 ms
8 be4-1-cf223-br-04-bsas.claro.com.ar (131.100.186.98) 29.814 ms 30.154 ms 29.201 ms
9 ae-20.a00.miamfl02.us.bb.gin.ntt.net (168.143.228.100) 143.146 ms 146.938ms 146.484 ms
10 * * *
11 * * *
12 4.14.99.142 (4.14.99.142) 171.693 ms 170.852 ms 168.003 ms
13 209.59.157.114 (209.59.157.114) 189.882 ms lw-dc3-core2-eth2-19.rtr.liquidweb.com (209.59.157.229)
14 lw-dc3-storm1.rtr.liquidweb.com (69.167.128.141) 188.433 ms lw-dc3-storm1.rtr.liquidweb.com (69.167.128.89) 194.069 ms lw-dc3-storm1.rtr.liquidweb.com (69.167.128.145) 194.503 ms
15 64.91.238.144 (64.91.238.144) 195.125 ms 193.387 ms 192.991 ms
mcherisi@putorius.net:/$
In the example above you see 15 hops, or routing devices from my local router 192.168.0.1 to reach putorius.net, some of the hops, 2, 10 and 11 are not responding to the traceroute packets (* * *).
Let’s take a single line to check:
3 10.172.102.109 (10.172.102.109) 16.695 ms 20.178 ms 22.081 ms
On the side right, you can see the round-trip time (RTT), it gives three times because by default traceroute sends 3 packets, you can modify it by applying the -q option.
The following example will show only one RTT result per line:
traceroute -q 1 putorius.net
mcherisi@putorius:/$ traceroute -q 1 putorius.net
traceroute to putorius.net (64.91.238.144). 30 hops max, 60 byte packets
1 192.168.0.1 (192.168.0.1) 1.013 ms
2 *
3 10.172.102.109 (10.172.102.109) 18.404 ms
4 10.200.0.10 (10.200.0.10) 22.826 ms
5 10.200.0.78 (10.200.0.78) 21.659 ms
6 10.169.26.1 (10.169.26.1) 23.369 ms
7 10.2.186.121 (10.2.186.121) 23.735 ms
8 be4-1-cf223-br-04-bsas.claro.com.ar (131.100.186.98) 27.030 ms
9 ae-20.a00.miamfl02.us.bb.gin.ntt.net (168.143.228.100) 147.063
10 *
11 *
12 4.14.99.142 (4.14.99.142) 167.708 ms
13 209.59.157.114 (209.59.157.114) 188.934 ms
14 lw-dc3-storm2.rtr.liquidweb.com (69.167.128.137) 187.726 ms
15 64.91.238.144 (64.91.238.144) 188.343 ms
mcherisi@putorius.net:/$
In case a connection is slow, or you couldn’t reach the destination by using traceroute you can determine what hop is generating the problem.
The scp command to transfer files between devices
The scp command is a command every admin or user should know to transfer files between Linux devices, it was deeply explained at Using the scp Command, but will be quoted in this tutorial too.
The first example shows how to copy a file from a remote computer to the local computer, the syntax is:
scp username@X.X.X.X:/path/to/remote/file /local/directory/where/to/save/the/file
Where:
- scp = calls the program
- Username = replace it for the proper username
- @ = separates username and host/ip
- X.X.X.X = replace it for the proper host/ip.
- :/path/to/remote/file = determine the remote location of the file to fetch.
- /local/directory/where/to/save/the/file = replace it for the local directory where you want to store the file.
In my case, I connect to the remote device with IP 192.168.0.1 using the user putorius to copy the Vagrantfile file which is stored in the home directory of the user putorius, and I copy it to the home of the local user called shared.
scp putorius@192.168.0.3:/home/putorius/Vagrantfile /home/shared/
mcherisi@putorius:/$ scp putorius@192.168.0.3:/home/putorius/Vagrantfile /home/shared/
putorius@192.168.0.3's password:
Vagrantfile 100% 1550 189.34KB/s 00:00
mcherisi@putorius.net:/$
The following example shows how to copy a local file to a remote device:
scp /home/shared/Vagrantfile putorius@192.168.0.3:/home/putorius/Desktop
mcherisi@putorius:/$ scp /home/shared/Vagrantfile putorius@192.168.0.3:/home/putorius/Desktop
putorius@192.168.0.3's password:
Vagrantfile 100% 1550 4.7KB/s 00:00
mcherisi@putorius.net:/$
Where:
- scp = calls the program
- FILENAME = name of the file to transfer
- Username = replace it for the proper username
- @ = separates username and host/ip
- X.X.X.X = replace it for the proper host/ip
- :/remote/directory = determine the remote location to store the transferred file.
The nc command to transfer files and scan ports
The nc (netcat) command is among the most useful tools for sysadmins, nc is useful for many tasks like redirecting, port scan, server diagnostics, file transferences, network monitoring and more. This tutorial shows two examples of the nc command use to scan ports and transfer files over the network.
The following example shows how nc is used to send a file called “putorius” from a computer to another through port 22.
On the destination computer, run the following command:
mcherisi@putorius:/ncfile$ nc -l 22 > putorius
On the sending computer execute:
nc 192.168.0.14 22 < putorious
mcherisi@putorius:/home/putorius$ nc 192.168.0.14 22 < putorious
SSH-2.0-OpenSSH_7.9p1 Debian-10
mcherisi@putorius.net:/$
The file is checked by running “ls“
mcherisi@putorius:/ncfile$ ls
putorius
mcherisi@putorius.net:/$
The following example shows how nc is used to scan the range port 22-80 on putorius.net
nc -z -v putorius.net 22-80
mcherisi@putorius:/$ nc -z -v putorius.net 22-80
putorius.net [64.91.238.144] 80 (http) open
putorius.net [64.91.238.144] 25 (smtp) open
putorius.net [64.91.238.144] 22 (ssh) open
mcherisi@putorius.net:/$
The rsync command to clone and files directories between devices
The rsync command is a great tool to clone files and directories from a device to another, it can be useful to keep real-time synchronization between devices, you can keep updated back up, create a script for Continuous Data Protection and more. The following 2 examples show how to synchronize files and directories from and to a server.
The syntax to copy a file from a server to a directory is:
rsync <RemoteUser>@<ServerHostIP>:</RemoteDirectory><LocalDirectory>
In the following example, the file named putorius is copied to the current or working directory (.)
rsync putorius@192.168.0.3:/home/putorius/putorius
mcherisi@putorius22:/rsync$ rsync putorius@192.168.0.3:/home/putorius/putorius
putorius@192.168.0.3's password:
mcherisi@putorius.net:/rsync$
The file transference is checked by running ls on the working directory
mcherisi@putorius:/rsync$ rsync putorius@192.168.0.3:/home/putorius/putorius
putorius@192.168.0.3's password:
mcherisi@putorius:/rsync$ ls
putorius
mcherisi@putorius:/rsync$
The following example shows how to clone a local directory to a remote server, the syntax is:
rsync -avz <LocalDirectory> <RemoteUser>@<ServerHostIP>:</RemoteDirectory>
In this practical example, a directory named rsync is cloned to the Desktop directory of the putorius user at the remote server.
rsync -avz rsync/ putorius@192.168.0.3:/home/putorius/Desktop
mcherisi@putorius:/$ rsync -avz rsync/ putorius@192.168.0.3:/home/putorius/Desktop
putorius@192.168.0.3's password:
sending incremental file list
./
putorius
sent 109 bytes received 41 bytes 40.57 bytes/sec
total size is 0 speedup is 0.00
Where:
- a = archive
- z = compress
- v = verbosity
These are just some advanced network tools for Linux. If you want to learn more about these commands or any other network tools please let us know in the comments.
