Linux Commands - Network commands

Reference Linux

Following are some of the important network commands in Linux.

ping

This command sends echo request to a domain to test network connection.

In the following example we are sending echo request to google.com.

$ ping google.com
PING google.com (172.217.163.46): 56 data bytes
64 bytes from 172.217.163.46: icmp_seq=0 ttl=57 time=37.225 ms
64 bytes from 172.217.163.46: icmp_seq=1 ttl=57 time=35.177 ms
64 bytes from 172.217.163.46: icmp_seq=2 ttl=57 time=34.801 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 34.801/35.734/37.225/1.065 ms

The above command will keep printing response unless you hit Ctrl+C to exit.

ping domain -c N

This will send echo request N number of times to the domain.

In the following example we are sending echo request 3 times to google.com.

$ ping google.com -c 3
PING google.com (172.217.163.46): 56 data bytes
64 bytes from 172.217.163.46: icmp_seq=0 ttl=57 time=34.536 ms
64 bytes from 172.217.163.46: icmp_seq=1 ttl=57 time=34.687 ms
64 bytes from 172.217.163.46: icmp_seq=2 ttl=57 time=36.030 ms

--- google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 34.536/35.084/36.030/0.672 ms

ifconfig

This command is used to configure network interface.

ifconfig -a

This will list all the network interfaces currently available, even if some of them are down.

$ ifconfig -a

ifconfig eth0

This will display the eth0 ethernet port IP address and other details.

$ ifconfig -a

whois

This command will print the WHOIS information about a domain.

In the following example we are fetching the WHOIS information of google.com.

$ whois google.com
Domain Name: GOOGLE.COM
Registry Domain ID: 2138514_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.markmonitor.com
Registrar URL: http://www.markmonitor.com
Updated Date: 2018-02-21T18:36:40Z
Creation Date: 1997-09-15T04:00:00Z
Registry Expiry Date: 2020-09-14T04:00:00Z
...
...
...

host

This command will lookup DNS (Domain Name System) for the given domain.

In the following example we are checking the DNS information for google.com domain.

$ host google.com
google.com has address 172.217.163.46
google.com has IPv6 address 2404:6800:4007:80b::200e
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.

dig

This command gets us the DNS nameserver information for a given domain name.

DIG = Domain Information Groper

In the following example we are fetching the nameserver information for google.com.

$ dig google.com

; <<>> DiG 9.11.3-1ubuntu1-Ubuntu <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27551
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;google.com.                    IN      A

;; ANSWER SECTION:
google.com.             188     IN      A       172.217.161.14

;; Query time: 63 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Mon Jan 01 01:01:01 IST 2018
;; MSG SIZE  rcvd: 55

hostname -i

This will lookup the local IP address.

In the following example I am getting 127.0.1.1 as my local IP address as I am using my laptop.

$ hostname -i
127.0.1.1

netstat

This command will list all the active listening ports.

In the following example we are listing all the active ports.

$ netstat -tupl
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN      -                   
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN      -                   
udp    39936      0 localhost:domain        0.0.0.0:*                           -                   
udp     2176      0 0.0.0.0:bootpc          0.0.0.0:*                           -                   
udp        0      0 0.0.0.0:45148           0.0.0.0:*                           -                   
udp    51456      0 0.0.0.0:mdns            0.0.0.0:*                           -                   
udp        0      0 0.0.0.0:ipp             0.0.0.0:*                           -                   
udp6   23040      0 [::]:mdns               [::]:*                              -                   
udp6       0      0 [::]:38193              [::]:*                              -                   

nslookup

This command queries Internet nameserver and resolves domain name to IP addresses.

In the following example we are getting the IP address of google.com website.

$ nslookup google.com
Server:         127.0.0.53
Address:        127.0.0.53#53

Non-authoritative answer:
Name:   google.com
Address: 172.217.161.14
Name:   google.com
Address: 2404:6800:4002:807::200e

wget

This command is used to download files from the web.

In the following example we are downloading jQuery from their GitHub repository.

$ wget https://github.com/jquery/jquery/archive/3.3.1.tar.gz