6 Basic Network Commands in Linux

network commands in linux

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:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
#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 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 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       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

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.

4 thoughts on “6 Basic Network Commands in Linux”

  1. The netstat and route command were both obsoleted by the iproute2 package more than a decade ago. Use the ss and ip route commands respectively instead.

    Reply
  2. You might want to mention the ip and ss commands for newer distributions that don’t come with nestat/ifconfig/etc.. installed by default.

    Reply
    • Yes, iproute2 replaced the net-tools package. Socket statistics, or ss is the equivalent for netstat. Also, the options used with the ss command are very similar to netstat.

      Reply
  3. Excellent article about basic network commands in linux…thank you for share this awesome post….Will you please keep it updated every time.I’ll make a note of this article for sure. Keep it up!

    Reply

Leave a Comment