Guide to Linux Networking with iproute2

Moraneus
7 min readMay 18, 2024

--

As a Linux administrator or power user, having a solid understanding of networking is crucial. While the task might seem intimidating initially, the right tools and knowledge can make it much more manageable. One of the most powerful and versatile toolkits for controlling and monitoring networking on Linux is iproute2. In this comprehensive guide, we’ll dive deep into the core utilities of iproute2, exploring how they can make your network administration tasks more efficient and effective across various Linux distributions.

What is iproute2?

iproute2 is a collection of user-space utilities designed for controlling and monitoring various aspects of networking in Linux. It’s a feature-rich suite that includes tools for managing routing, network interfaces, tunnels, traffic control, and more. iproute2 is the successor to the older net-tools package, which contained tools like ifconfig, route, and netstat.

In this article, we will explore the powerful capabilities of the ip and ss (socket statistics) commands, which are essential tools included in the iproute2 package for managing and monitoring networking on Linux systems.

Why iproute2 is Better

  1. Unified and Consistent: One of the key advantages of iproute2 is that it provides a cohesive and consistent framework. All tools within the suite follow a similar syntax and design philosophy, making it easier to learn and use them effectively across different Linux distributions.
  2. Feature-Rich: iproute2 supports a wide array of advanced networking features that the older tools lack. It can handle complex network configurations, multiple routing tables, network namespaces, and more.
  3. Actively Maintained: Unlike the net-tools package, which is no longer actively maintained, iproute2 is regularly updated to keep pace with the ever-evolving networking landscape in Linux.

Installing iproute2

The installation process for iproute2 varies slightly depending on your Linux distribution or if you’re using macOS. Here’s how you can install iproute2 on some of the most common platforms:

Ubuntu and Debian

On Ubuntu, Debian, and their derivatives, iproute2 is usually pre-installed. If it’s missing, you can install it using the apt package manager:

sudo apt update
sudo apt install iproute2

CentOS and Red Hat Enterprise Linux (RHEL)

On CentOS, RHEL, and other Red Hat-based distributions, iproute2 is also typically pre-installed. If it’s not available, you can install it using the yumpackage manager:

sudo yum install iproute

Fedora

On Fedora, you can install iproute2 using the dnf package manager:

sudo dnf install iproute

Arch Linux

On Arch Linux and its derivatives, you can install iproute2 using the pacman package manager:

sudo pacman -S iproute2

macOS

While iproute2 is primarily used on Linux, you can also install it on macOS using the Homebrew package manager. First, install Homebrew if you haven’t already:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then, install iproute2mac, which is a port of iproute2 for macOS:

brew install iproute2mac

The ip Command: Your Swiss Army Knife for Networking

The ip command is the centerpiece of the iproute2 suite. It's an extremely versatile tool that replaces a multitude of older commands. Its capabilities range from assigning IP addresses and configuring interfaces to managing routing and network namespaces.

The ip command follows a specific syntax format:

ip [OPTIONS] OBJECT {COMMAND | help}
  • OPTIONS: Optional flags that modify the behavior of the ip command.
  • OBJECT: The networking component or object you want to manipulate or query.
  • COMMAND: The specific action or operation to perform on the selected object.
  • help: Displays help information for the specified object.

Objects

The ip command supports various networking objects, each representing a specific aspect of networking. Here some of them:

  • link: Network interfaces and their properties.
  • address: Protocol (IPv4 or IPv6) addresses assigned to interfaces.
  • route: Entries in the routing table.
  • neigh: Neighbour (ARP or NDISC) entries.
  • tunnel: Tunnel over IP configuration.
  • stats: Display network interface statistics.

Showing IP Addresses

To get a detailed overview of your system’s IP configuration, you can use the following command:

ip address show

This will display all network interfaces on your system along with their associated IP addresses. It provides a quick and comprehensive snapshot of your network setup.

Managing IP Addresses

The ip command makes it easy to assign and remove IP addresses from interfaces. To assign an IP address to an interface, use the following syntax:

sudo ip address add 192.168.1.100/24 dev eth0

This command assigns the IP address 192.168.1.100 with a 24-bit subnet mask (255.255.255.0) to the eth0 interface.

If you need to remove an IP address from an interface, you can use the del subcommand:

sudo ip address del 192.168.1.100/24 dev eth0

This removes the specified IP address from the eth0 interface.

Bringing Interfaces Up and Down

To activate an interface and allow it to send and receive data, you can use the ip link set command with the up option:

sudo ip link set eth0 up

Similarly, to deactivate an interface, you can use the down option:

sudo ip link set eth0 down

Modifying Interface Properties

The ip command provides a way to modify various properties of network interfaces. For example, to change the Maximum Transmission Unit (MTU) of an interface:

sudo ip link set eth0 mtu 1500

This sets the MTU of the eth0 interface to 1500 bytes.

You can also enable promiscuous mode on an interface, which allows it to capture all traffic, even if the traffic is not addressed to that specific interface:

sudo ip link set eth0 promisc on

Managing Routes

iproute2 provides powerful tools for managing routing. To add a default route via a gateway, you can use the following command:

sudo ip route add default via 192.168.1.1

This command sets the default route to the gateway at IP address 192.168.1.1.

If you need to add a route to a specific network, you can specify the network and the gateway:

sudo ip route add 172.16.0.0/16 via 192.168.1.254

This adds a route to the 172.16.0.0/16 network via the gateway at IP address 192.168.1.254.

To add a route to a specific interface using the ip command, you can use the following syntax:

sudo ip route add <destination> via <gateway> dev <interface>

Here’s a breakdown of the components:

  • <destination>: The destination network or host for the route. It can be specified as an IP address with a subnet mask (e.g., 192.168.2.0/24) or as a single IP address (e.g., 192.168.2.100).
  • <gateway>: The IP address of the gateway or next hop through which the packets should be routed to reach the destination.
  • <interface>: The network interface to which the route should be associated.

Let’s look at an example to illustrate this:

sudo ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0

This command adds a route to the 192.168.2.0/24 network via the gateway at 192.168.1.1, and the route is associated with the eth0 interface. Any packets destined for the 192.168.2.0/24 network will be routed through the specified gateway using the eth0 interface.

You can verify the newly added route by running:

ip route show

To delete a route, simply replace add with del:

sudo ip route del 172.16.0.0/16 via 192.168.1.254

This removes the route to the 172.16.0.0/16 network via the specified gateway.

The ss Command: Socket Statistics

The ss command is a powerful tool for investigating sockets and displaying detailed information about network connections. It provides more features and better performance compared to the older netstat command. Here are few useful ss commands:

Showing All Sockets

To display a list of all sockets, both listening and non-listening, you can use the following command:

ss -a

This provides an overview of all network connections on your system.

Showing Established Sockets

By simply typing ss without any options, you'll see all sockets with established connections.

ss

Displaying Listening Sockets

If you only want to see the sockets that are currently listening for incoming connections, you can use the -l option:

ss -l

This is useful for identifying which services are running and on what ports they are listening.

Showing TCP Sockets

To display only TCP sockets, you can use the -t option:

ss -t

You can combine this with other options, such as -a to show all TCP sockets (listening and non-listening), or -l to show only listening TCP sockets.

Viewing UDP Sockets

ss shows only established connections. To view UDP socket statistics, which are connectionless, you need to use specific options. Combine -a (all) and -u (UDP) to display all UDP sockets:

ss -au

This command will list all UDP sockets, including those that are not actively connected.

Displaying Socket Statistics

If you need a quick overview of socket statistics, you can use the -s option:

ss -s

This provides a summary of the number of sockets in various states, such as established, closed, and listening.

Display Raw Values

If you want to see the raw numeric values without any translation by the ss tool, use the -n option:

ss -n

With the ss command, you can combine multiple options to customize its output. For instance, using -a will display all sockets (both listening and non-listening), -l will show only listening sockets, -n will present raw numeric values without translation, and -p will include the process using each socket. By mixing these flags, you can tailor the command to provide precisely the information you need.

Conclusion

iproute2 is an essential toolkit for managing and monitoring Linux networking. Its powerful and flexible commands, such as ip, ss, provide in-depth control and insight into your network configuration. By mastering these tools, you'll be well-equipped to administer Linux-based systems and ensure optimal network performance.

Remember, practice is key to becoming proficient with iproute2. The more you use these tools in real-world scenarios, the more comfortable and efficient you’ll become in managing your Linux network stack. Don’t hesitate to experiment, consult the man pages for detailed options, and seek out additional resources to deepen your understanding.

With iproute2 in your toolkit, you’ll be able to tackle complex networking tasks with confidence and precision.

Happy networking!

Your Support Means a Lot! 🙌

If you enjoyed this article and found it valuable, please consider giving it a clap to show your support. Feel free to explore my other articles, where I cover a wide range of topics related to Python programming and others. By following me, you’ll stay updated on my latest content and insights. I look forward to sharing more knowledge and connecting with you through future articles. Until then, keep coding, keep learning, and most importantly, enjoy the journey!

References

--

--