In this tutorial, we will show you some basic network commands in Linux, 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.
Today we are going to cover 6 Basic Network Commands in Linux
1. PING COMMAND
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 COMMAND
Netstat (Network Statistic) command display network connections to and from the server, routing tables, networking interface statistics, masquerade connections etc. The netstat command has been replaced with ss. The command-line parameters are almost identical.
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 COMMAND
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. HOSTNAME COMMAND
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. ROUTE COMMAND
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 126.96.36.199 netmask 255.255.255.0 dev eth0
To delete a route use:
# route del -net 188.8.131.52 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 COMMAND
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 184.108.40.206 ;; 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 220.127.116.11 ns4.google.com. 206816 IN A 18.104.22.168 ns2.google.com. 206816 IN A 22.214.171.124 ns1.google.com. 206816 IN A 126.96.36.199 ;; Query time: 0 msec ;; SERVER: 188.8.131.52#53(184.108.40.206) ;; WHEN: Sun Mar 5 18:37:21 2017 ;; MSG SIZE rcvd: 180
Of course, you don’t have to know Network Commands in Linux if you use one of our Ultra-Fast SSD Hosting services, in which case you can simply ask our expert Linux admins to troubleshoot any networking problems for you, or to help you with the Network Commands in Linux. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.