Basic Linux networking commands that you should know

By working with numerous clients I discovered that many users struggle with Linux networking and do not know how to perform basic troubleshooting steps in order to identify and correct the issue. In this post, I will show you the most useful commands that you can use to test if the networking stack is working properly on your Linux host.

Gulian Technology

Linux Networking

How to check the IP address of your Linux machine

If you want to find out the IP address of your Linux host, you need to run one of these commands.

petru@ubuntu-dev:~$ ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 00:0c:29:d8:1b:d0 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d8:1b:da brd ff:ff:ff:ff:ff:ff
    altname enp2s5
    inet 192.168.1.140/24 brd 192.168.1.255 scope global dynamic noprefixroute ens37
       valid_lft 77556sec preferred_lft 77556sec
    inet6 fe80::7f1c:2aa6:d71f:38ae/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:f7:5d:d3:64 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
petru@ubuntu-dev:~$ 
Find the IP address of your Linux machine
Find the IP address of your Linux machine

By running the above command you can find out how many interfaces are present on your Linux host. You can also find the IP address of your machine, both IPv4 and IPv6, and you can see the physical address (MAC address) for the network interface card (NIC).

ens37 – name of the interface

status of the interface – UP

IPv4 address: 192.168.1.140

Subnet mask: /24 or in decimal notation 255.255.255.0

Broadcast address: 192.168.1.255

IPV6 address: fe80::7f1c:2aa6:d71f:38ae

Prefix length: /64

Scope: Link-local address

The second command that you can run to find some similar information is ifconfig.

petru@ubuntu-dev:~$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:f7:5d:d3:64  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 00:0c:29:d8:1b:d0  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.140  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::7f1c:2aa6:d71f:38ae  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d8:1b:da  txqueuelen 1000  (Ethernet)
        RX packets 6978  bytes 1124274 (1.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2866  bytes 261611 (261.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 147  bytes 14897 (14.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 147  bytes 14897 (14.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

petru@ubuntu-dev:~$ 
Check the IP details for the Linux machine
Check the IP details for the Linux machine

In addition to the IP address and subnet mask, you can find some statistics regarding the packets sent, received and dropped. Furthermore, you can confirm the maximum transmission unit for the (MTU) interface. In the above example it is 1500 bytes.

How to check the routing table for your Linux machine

If you want to find the routing details for your Linux host, you can run one of the below commands. All of them give you similar information.

petru@ubuntu-dev:~$ ip route
default via 192.168.1.1 dev ens37 proto dhcp metric 100 
169.254.0.0/16 dev ens37 scope link metric 1000 
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown 
192.168.1.0/24 dev ens37 proto kernel scope link src 192.168.1.140 metric 100 
petru@ubuntu-dev:~$
Check the routing table for your Linux machine
Check the routing table for your Linux machine
petru@ubuntu-dev:~$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 ens37
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens37
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens37
petru@ubuntu-dev:~$
Linux route command
Linux route command
petru@ubuntu-dev:~$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         vodafone.broadb 0.0.0.0         UG        0 0          0 ens37
link-local      0.0.0.0         255.255.0.0     U         0 0          0 ens37
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 ens37
petru@ubuntu-dev:~$ 
Check the Linux route table with the netstat command
Check the Linux route table with the netstat command

How to check if you have connectivity to the Internet

In order to confirm that you have connectivity to the Internet, you can use the ping command. Choose a host located in the Internet and check if it is reachable from your machine.

petru@ubuntu-dev:~$ ping -c 2 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=119 time=12.0 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=119 time=19.2 ms

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 11.992/15.611/19.231/3.619 ms
petru@ubuntu-dev:~$ ping -c 2 1.1.1.1
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=11.9 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=59 time=12.2 ms

--- 1.1.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 11.878/12.030/12.183/0.152 ms
petru@ubuntu-dev:~$ ping -c 2 208.67.222.222
PING 208.67.222.222 (208.67.222.222) 56(84) bytes of data.
64 bytes from 208.67.222.222: icmp_seq=1 ttl=60 time=10.3 ms
64 bytes from 208.67.222.222: icmp_seq=2 ttl=60 time=10.3 ms

--- 208.67.222.222 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 10.263/10.287/10.312/0.024 ms
petru@ubuntu-dev:~$ 
Check if you have Internet connectivity
Check if you have Internet connectivity

In my example, I checked if I am able to reach the following hosts:

8.8.8.8 – This is the IP address of the Google DNS server

1.1.1.1 – This is the IP address of the Cloudflare DNS server

208.67.222.222 – This is the IP address of the Cisco Umbrella DNS server

Check that your default gateway is up and running

In case you find out that you are not able to reach the Internet, you need to check if your default gateway is up and running. You can do this by running a ping command.

petru@ubuntu-dev:~$ ping -c 2 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
From 192.168.1.1 icmp_seq=1 Destination Net Unreachable
From 192.168.1.1 icmp_seq=2 Destination Net Unreachable

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1003ms

petru@ubuntu-dev:~$ ping -c 2 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.56 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.65 ms

--- 192.168.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 1.558/1.601/1.645/0.043 ms
petru@ubuntu-dev:~$
No Internet access. Default gateway up and running.
No Internet access. Default gateway up and running.

As you can see, I am able to ping the default gateway, but I am not able to reach the Internet. In this case, I need to check the default gateway. I need to confirm that the default route is present on the default gateway and the link between my router and the ISP is up.

How to check that the DNS resolution is working

If you try to browse the Internet and you discover that you are not able to open any URL, you need to check that the DNS resolution is working. You can do this by using the same ping command.

petru@ubuntu-dev:~$ ping gulian.uk
ping: gulian.uk: Temporary failure in name resolution
petru@ubuntu-dev:~$
DNS resolution failure
DNS resolution failure

As you can see I have some issues with DNS resolution on my Linux machine. Normally, you should be able to ping the name and see an IP address. In situations like this, you need to check your DNS settings.

Run a similar command on your Linux host.

petru@ubuntu-dev:~$ cat /etc/resolv.conf 
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0 trust-ad
Check the configured name server
Check the configured name server
petru@ubuntu-dev:~$ resolvectl status
Global
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub

Link 2 (ens33)
Current Scopes: none
     Protocols: -DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported

Link 3 (ens37)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 172.16.10.1
       DNS Servers: 172.16.10.1

Link 4 (docker0)
Current Scopes: none
     Protocols: -DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
petru@ubuntu-dev:~$ 
Check the DNS server
Check the DNS server

On my Linux host, the DNS resolution is failing because I have configured a wrong DNS server. As you can see, my machine is using the DNS server with the IP address 172.16.10.1. This server is unreachable.

petru@ubuntu-dev:~$ ping -c 2 172.16.10.1
PING 172.16.10.1 (172.16.10.1) 56(84) bytes of data.

--- 172.16.10.1 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1025ms

petru@ubuntu-dev:~$
Unreachable DNS server
Unreachable DNS server

After changing the configuration and adding the correct DNS server (192.168.1.1), I am able to perform DNS resolutions and I am able to surf on the Web.

petru@ubuntu-dev:~$ ping -c 2 gulian.uk
PING gulian.uk (192.0.78.228) 56(84) bytes of data.
64 bytes from 192.0.78.228 (192.0.78.228): icmp_seq=1 ttl=58 time=10.3 ms
64 bytes from 192.0.78.228 (192.0.78.228): icmp_seq=2 ttl=58 time=50.9 ms

--- gulian.uk ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 10.324/30.587/50.850/20.263 ms
petru@ubuntu-dev:~$ 
Successful DNS resolution
Successful DNS resolution
Surfing the web
Surfing the web

How to check the ARP table on your Ubuntu machine

Next thing that may help you in troubleshooting network issues is checking the ARP table.

ARP stands for Address Resolution Protocol. This protocol maps an IP address (layer 3 address) to a MAC address (layer 2 address). With the help of this protocol you can find the physical address of a host on a LAN associated with an IP address.

With the help of this protocol, your Linux machine finds the physical address (MAC address) of your default gateway.

In Linux, you need to run this command to display the arp table.

petru@ubuntu-dev:~$ arp 
Address                  HWtype  HWaddress           Flags Mask            Iface
vodafone.broadband       ether   d4:35:1d:0d:44:7a   C                     ens37
pgulian-ltm1.broadband   ether   3c:22:fb:87:85:97   C                     ens37
192.168.1.167            ether   4e:e8:d9:90:7e:ae   C                     ens37
192.168.1.117            ether   8e:a1:ff:7e:26:63   C                     ens37
192.168.1.93             ether   86:5e:64:b7:38:a6   C                     ens37
Redmi8-Redmi8-2.broadba  ether   90:78:b2:86:83:3f   C                     ens37
petru@ubuntu-dev:~$ 
Check the arp table
Check the arp table

That’s it! You have learned how to check the IP address of your Linux machine and explore the routing table. You discovered how to find the ARP table and how to troubleshoot the DNS resolution. These are valuable skills for network troubleshooting and configuration. If you found this blog post helpful, please like and subscribe for more Linux tutorials. Thank you for reading it!

Processing…
Success! You're on the list.

Leave a Reply