6 Basic Network Commands in Linux

 

 

This Linux tutorial covers some basic network commands which can be useful when troubleshooting networking problems with other servers both within the network and across the Internet, obtaining more information about other servers.
1. The ping command sends ICMP echo requests to the server you specify on the command line, and is used to quickly test network connectivity to another server. If the packets are received, the destination device sends packets back:

# ping 192.168.1.2
PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.141 ms
64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.136 ms
64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.109 ms

Please note, ping is not a reliable way to test network connectivity because many servers block ICMP echo packets by default, so if some server is not responding to pings, it doesn’t always mean it is down or unavailable.
2. Netstat (Network Statistic) command display network connections to and from the server, routing tables, networking interface statistics, masquerade connections etc.

netstat -tunlp | less

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      15501/sshd
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      29709/httpd

-t List all tcp ports;
-u List all udp ports;
-n Show numerical addresses instead of trying to determine symbolic host;
-l Show only listening sockets;
-p Show the PID and name of the program to which each socket belongs.
3. Traceroute is a network troubleshooting command which will show the route of a packet to the destination server (the number of hops) and response time to get to the destination server.

# traceroute 192.168.1.2
traceroute to 192.168.1.2 (192.168.1.2), 30 hops max, 60 byte packets
1  192.168.1.2 (192.168.1.2)  0.471 ms  0.401 ms  0.402 ms

4. The hostname command shows the host name of the server, and it is also used to set (or change) the server hostname:

# hostname
test.com

To set a new hostname for the server use:

#sudo hostname new-hostname.com
# hostname
new-hostname.com

5. The route command is a network utility used to display or modify the routing table.

# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0    *               255.255.255.0    U     0      0        0 eth0
link-local     *               255.255.0.0      U     1002   0        0 eth0
default        192.168.1.1     0.0.0.0          UG    0      0        0 eth0

To add a new route use:

# route add -net 172.0.0.0 netmask 255.255.255.0 dev eth0

To delete a route use:

# route del -net 172.0.0.0 netmask 255.255.255.0 dev eth0

To delete the default gateway and add a new gateway as the default use:

# route delete default gw 192.168.1.1 eth0
# route add default gw 192.168.10.254 eth0

6. Dig (domain information groper) is a useful tool for network troubleshooting, primarily used to query DNS related information like A Record, CNAME, NS, MX Records etc.

# dig google.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6_8.4 <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25916
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 4

;; QUESTION SECTION:
;google.com.                    IN      A

;; ANSWER SECTION:
google.com.             106     IN      A       216.58.216.78

;; AUTHORITY SECTION:
google.com.             33959   IN      NS      ns4.google.com.
google.com.             33959   IN      NS      ns3.google.com.
google.com.             33959   IN      NS      ns2.google.com.
google.com.             33959   IN      NS      ns1.google.com.

;; ADDITIONAL SECTION:
ns3.google.com.         206816  IN      A       216.239.36.10
ns4.google.com.         206816  IN      A       216.239.38.10
ns2.google.com.         206816  IN      A       216.239.34.10
ns1.google.com.         206816  IN      A       216.239.32.10

;; Query time: 0 msec
;; SERVER: 209.135.140.42#53(209.135.140.42)
;; WHEN: Sun Mar  5 18:37:21 2017
;; MSG SIZE  rcvd: 180

 

Source