Linux is a powerful and versatile operating system that can be used for various tasks, such as web development, data analysis, system administration, and more. However, to make the most out of Linux, you need to know how to work with networks and network commands.
In this article, you will learn to perform basic and advanced network operations on Linux with ease and confidence. Let’s get started!
Configure Network Interfaces
A network interface is a device or software component that enables communication between a computer and a network.
In Linux, network interfaces are usually named with a prefix (such as eth, wlan, or lo) followed by a number (such as 0, 1, or 2). For example, eth0 is the first Ethernet interface, wlan0 is the first wireless interface, and lo is the loopback interface.
To view the information and status of all network interfaces on your system, you can use the
ip command with the
To configure an IP address for an interface, you can use the
ip command with the
address option followed by
del. For example, you can change the IP address of eth0 to 192.168.1.32/24 using:
ip address add 192.168.1.32/24 dev eth0
You can delete the IP address of eth0 using:
ip address del 192.168.1.32/24 dev eth0
To enable or disable an interface, you can use the
ip command with the
link option followed by
down. For example, you can bring up
ip link set eth0 up
You can bring down
ip link set eth0 down
Checking and Troubleshooting Network Connectivity
One of the most common tasks for any network administrator is to check and troubleshoot network connectivity issues.
ping command is used to test the reachability of a host by sending packets and measuring the round-trip time and packet loss.
For example, you can ping four times with a one-second interval between packets and a packet size of 64 bytes using:
ping -c 4 -i 1 -s 64 google.com
As you can see, we have successfully pinged
google.com four times, with an average round-trip time of
681.982 ms and no packet loss. This means, we have a good network connectivity.
traceroute command is used to trace the route taken by packets to reach a destination host. It shows the IP address and hostname of each hop along the way, as well as the round-trip time and number of packets sent and received for each hop.
For example, you can trace the route to google.com with a maximum of 10 hops and three packets per hop, using:
traceroute -m 10 -q 3 google.com
As you can see, We have traced the route to google.com in 10 hops. The first hop is our default gateway (192.168.32.2), and the last hop is
188.8.131.52). The other hops are intermediate routers that forward the packets between my system and
tracepath command is similar to traceroute, but it also does not require root or sudo privileges.
host command is used for querying DNS servers and getting information about domain names and IP addresses. It is similar to dig, but with a simpler syntax and output format.
For example, You can query for MX records for google.com using the Google public DNS server (184.108.40.206) using:
host -t MX google.com 220.127.116.11
As you can see, We have queried for MX records for google.com using the Google public DNS server.
dig command is another tool to query DNS (Domain Name System) servers and get information about domain names, IP addresses, name servers, mail servers, and other records.
For example, you can query for A records for google.com and show only the answer section using:
dig +short google.com A
As you can see, We have queried for A records for google.com and got one IP address:
Analyze Network Traffic
Another important task for any network administrator is to monitor and analyze network traffic on a system or a network.
netstat command is used to display various information about network connections, routing tables, interface statistics, masquerade connections, and multicast memberships on a system.
For example, to show all TCP sockets with numeric values, you can use:
netstat -an -t
As you can see, We have displayed all TCP sockets with numeric values. The output of netstat shows the following information for each socket:
- The protocol (TCP or UDP)
- The local address and port
- The foreign address and port
- The state of the socket (such as ESTABLISHED, LISTENING, TIME_WAIT, etc.)
ss command is another tool for displaying information about network sockets on a system. It is similar to netstat, but with more features and options.
For example, to show all TCP sockets that are in the
ESTABLISHED state and have a destination port of 80 or 443, you can use:
ss -an -t state established '( dport 80 or dport 443 )'
As you can see, We have displayed all TCP sockets that are in the
ESTABLISHED state and have a destination port of 80 or 443.
This concludes our article on Linux networking and commands for troubleshooting. We hope you have learned some useful concepts and commands that can help you work with networks in Linux.