DEV Community

Cover image for Networking in LINUX
SAHIL
SAHIL

Posted on

Networking in LINUX

Introduction
Linux is a powerful operating system, and a big part of that power comes from its robust networking capabilities. Whether you're a system administrator, a developer, or just a curious user, understanding how to manage and troubleshoot networks in Linux is a valuable skill. This post will cover some of the most common and essential networking commands, how to view network information, and a basic guide to setting up a network interface.

Essential Networking Commands
There are two main sets of tools you'll encounter for networking in Linux: the older net-tools and the newer iproute2 suite. While net-tools commands like ifconfig are still widely used, the iproute2 commands are the modern standard and offer more functionality.

ip (The iproute2 suite)
The ip command is the Swiss Army knife of Linux networking. It can do almost everything you need.

ip address (or ip a): Displays the IP addresses and network interfaces on your system.

Example: ip a will show you interfaces like eth0 (a wired connection) and wlan0 (a wireless connection), along with their IP addresses, subnet masks, and MAC addresses.

ip route (or ip r): Shows the routing table, which determines how network traffic is forwarded.

Example: ip r will show your default gateway, which is the path to the internet.

ip link: Displays the status and properties of network interfaces.

Example: ip link set eth0 up brings the eth0 interface up.

ip neighbor: Manages the Address Resolution Protocol (ARP) cache, which maps IP addresses to MAC addresses.

ifconfig (from net-tools)
The ifconfig command is the classic way to view and configure network interfaces. It's still common in many older tutorials and systems.

Example: ifconfig shows network interfaces, their IP addresses, and traffic statistics.

ping
The ping command is used to test connectivity to a host. It sends a series of Internet Control Message Protocol (ICMP) echo requests.

Example: ping google.com sends packets to Google's server to check if it's reachable.

netstat (from net-tools)
The netstat command shows network connections, routing tables, and interface statistics.

Example: netstat -tuln lists all listening TCP and UDP ports and their corresponding programs.

Setting up a Network Interface
While graphical tools and network managers handle this for you on most desktop distributions, it's good to know how to do it manually.

1. Assigning a Static IP Address
To assign a static IP address to an interface (e.g., eth0), you can use the ip command.

sudo ip address add 192.168.1.100/24 dev eth0
Enter fullscreen mode Exit fullscreen mode

This command assigns the IP address 192.168.1.100 with a subnet mask of 255.255.255.0 (represented by /24) to the eth0 interface

2. Bringing the Interface Up

Once the IP is assigned, you need to bring the interface up.

sudo ip link set eth0 up
Enter fullscreen mode Exit fullscreen mode

3. Adding a Default Gateway

To route traffic to the internet, you need to add a default gateway. This is usually the IP address of your router.

sudo ip route add default via 192.168.1.1
Enter fullscreen mode Exit fullscreen mode

Configuration Files
For persistent network settings that survive a reboot, you need to edit configuration files. The location and format of these files can vary between Linux distributions.

Debian/Ubuntu: /etc/network/interfaces

CentOS/RHEL: /etc/sysconfig/network-scripts/ifcfg-eth0

Here's an example of a static IP configuration for a Debian/Ubuntu system in /etc/network/interfaces:

auto eth0
iface eth0 inet static
  address 192.168.1.100
  netmask 255.255.255.0
  gateway 192.168.1.1

Enter fullscreen mode Exit fullscreen mode

After editing, you need to restart the networking service.

  • On systemd-based systems (most modern distros): sudo systemctl restart networking
  • On older systems: sudo service networking restart

Troubleshooting

  • ping: If you can't reach a website, first ping its IP address. If that works, but pinging the domain name doesn't, it's likely a DNS issue.
  • traceroute (or tracert on Windows): Shows the path that packets take to a destination. This can help identify where connectivity is failing.
  • nmap: A powerful network scanner for discovering hosts and services on a network.
  • ss: A modern replacement for netstat. It shows socket statistics.

Conclusion

Mastering these commands and understanding the configuration files will give you a solid foundation in Linux networking. From checking a simple connection with ping to manually configuring an interface with ip, you're now equipped with the essential tools to navigate the network world on your Linux machine. Keep exploring and happy networking! 🌐

Top comments (0)