Home Learn Linux Mastering the ‘ip’ command in Linux: 10 real-world examples

Mastering the ‘ip’ command in Linux: 10 real-world examples

Dive into the functionalities of the 'ip' command in Linux with this guide, featuring 10 practical examples. It's designed to help you master network configuration and diagnostics, offering insights into managing interfaces, routes, and more, making you adept at handling various network tasks in Linux.

by Divya Kiran Kumar
ip command linux

In this comprehensive guide, we delve into the ip command, a cornerstone in the toolkit of every Linux user, from beginners to seasoned system administrators. Often confused with Windows’ ipconfig, the ip command in Linux is a part of the iproute2 package and is a modern replacement for the older ifconfig command.

This guide will not only introduce you to the syntax and various applications of the ip command but also provide real-world examples to demonstrate its practical usage. Additionally, we’ll cover installation procedures across different Linux distributions and answer frequently asked questions to ensure a well-rounded understanding of this essential tool.

Introduction to the ip command

Linux’s ipconfig equivalent is commonly misunderstood. In reality, Linux uses ifconfig and the more advanced ip command for network management. The ip command, part of the iproute2 package, offers comprehensive features for managing network interfaces and routes.

Is the ip command included by default?

In most modern Linux distributions, the ip command is included by default as part of the iproute2 package. This package is essential for network configuration and troubleshooting, making it a standard component in the base installation of many distributions. However, in some minimal or custom installations, it might not be present.

Installing iproute2 in various Linux distributions

If you find that the ip command is missing from your system, installing it is generally straightforward. Below, I’ll guide you through the installation process on several popular Linux distributions.

Debian, Ubuntu, and derivatives:

Debian and its derivatives, like Ubuntu, include iproute2 by default. However, if it’s missing, you can easily install it using the apt package manager:

sudo apt update
sudo apt install iproute2

Fedora, CentOS, and Red Hat:

On Fedora and other RPM-based distributions like CentOS and Red Hat, iproute2 is typically pre-installed. If you need to install it manually, use the dnf or yum package manager:

sudo dnf install iproute

Or on older distributions using yum:

sudo yum install iproute

Arch Linux and Manjaro:

Arch Linux and Manjaro, known for their rolling release model, also include iproute2 by default. To install it on these distributions, use the pacman package manager:

sudo pacman -Syu iproute2

OpenSUSE:

OpenSUSE users can rely on Zypper to install iproute2:

sudo zypper install iproute2

Checking the installation

After installation, you can verify the presence of the ip command by typing:

ip a

This command should display a list of your network interfaces, indicating that iproute2 is successfully installed.

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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 01:23:45:67:89:ab brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86398sec preferred_lft 86398sec
inet6 fe80::1234:5678:9abc:def1/64 scope link 
valid_lft forever preferred_lft forever

In this example:

  • lo is the loopback interface, a standard virtual network interface that your computer uses to communicate with itself. It has the IPv4 address 127.0.0.1 and the IPv6 address ::1.
  • eth0 is a physical network interface, representing an Ethernet connection in this case. It shows that the Ethernet interface has an IPv4 address assigned (192.168.1.100 with a 24-bit subnet mask), and an IPv6 link-local address. The state UP indicates that the interface is active and operational.

The output details can vary based on your network configuration, such as whether you’re using Wi-Fi (wlan0 or similar), have multiple network interfaces, or different IP addressing.

Syntax of the ip command

The basic syntax is:

ip [ OPTIONS ] OBJECT { COMMAND | help }

With OBJECT being link, addr, route, etc., each managing different network aspects.

Example 1: Viewing all network interfaces

Command:

ip link show

This command lists all network interfaces on your system, showing details like state, MTU size, and MAC address, which is crucial for identifying and managing network interfaces.

Output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 02:01:02:03:04:05 brd ff:ff:ff:ff:ff:ff

Example 2: Assigning an IP address

Command:

sudo ip addr add 192.168.1.10/24 dev eth0

This assigns the IP address 192.168.1.10 with a 24-bit subnet mask to the eth0 interface, a common task for setting up a network interface.

Example 3: Removing an IP address

Command:

sudo ip addr del 192.168.1.10/24 dev eth0

Use this to remove a specific IP address from an interface, a necessary step in reconfiguring or troubleshooting network settings.

Example 4: Enabling and disabling network interfaces

Command to enable:

sudo ip link set eth0 up

Command to disable:

sudo ip link set eth0 down

These commands are used to activate or deactivate a network interface, which is essential for managing network connections or troubleshooting.

Example 5: Viewing routing table

Command:

ip route show

This displays the routing table, showing how data packets travel through your network, a key aspect of network management.

Output:

default via 192.168.1.1 dev eth0 
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10

Example 6: Adding a route

Command:

sudo ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0

Adding a route is necessary for directing traffic to a specific network, a common requirement in complex network setups.

Example 7: Deleting a route

Command:

sudo ip route del 192.168.2.0/24

This removes a specified route, useful in network reconfiguration or when resolving conflicts.

Example 8: Monitoring network interfaces

Command:

ip -s link

Monitoring interfaces in real-time is crucial for troubleshooting and ensuring optimal network performance.

Example 9: Changing the MTU size

Command:

sudo ip link set dev eth0 mtu 1400

Adjusting the MTU size can optimize network performance or resolve connectivity issues, especially in mixed network environments.

Example 10: Flushing IP addresses

Command:

sudo ip addr flush dev eth0

This command removes all IP addresses from an interface, a useful step in network troubleshooting or reconfiguration.

Essential ip command reference for Linux networking

Command Description
ip link show Displays all network interfaces with details like state, MTU size, and MAC address.
ip addr show Lists all IP addresses assigned to the network interfaces.
ip addr add [IP_ADDRESS]/[MASK] dev [INTERFACE] Assigns a specific IP address with a subnet mask to a network interface.
ip addr del [IP_ADDRESS]/[MASK] dev [INTERFACE] Removes an IP address from a specific network interface.
ip link set [INTERFACE] up Activates a network interface, making it operational.
ip link set [INTERFACE] down Deactivates a network interface, making it non-operational.
ip route show Displays the current routing table, showing how data packets are routed within the network.
ip route add [DESTINATION] via [GATEWAY] Adds a new route to the routing table for directing traffic to a specific network.
ip route del [DESTINATION] Removes a specific route from the routing table.
ip -s link Provides detailed statistics for each network interface, useful for monitoring and troubleshooting.
ip link set dev [INTERFACE] mtu [SIZE] Sets the Maximum Transmission Unit (MTU) size for a network interface.
ip addr flush dev [INTERFACE] Removes all IP addresses assigned to a specific network interface.

Frequently Asked Questions (FAQs) on Linux’s ip command

Here’s a FAQ section that aims to address these queries, enhancing your understanding and application of this versatile networking tool.

What is the ip command in Linux?

Answer: The ip command in Linux is a versatile tool used for managing network interfaces, IP addresses, routing tables, and more. It is part of the iproute2 package and is used to configure and troubleshoot network settings.

How does the ip command differ from ifconfig?

Answer: ifconfig is an older networking command that has been largely replaced by the ip command. ip offers more extensive features, better presentation of network interface information, and is designed to better handle modern networking standards like IPv6.

Can I use ip command to configure IPv6 addresses?

Answer: Yes, the ip command is fully capable of handling IPv6 addresses. It can be used to add, delete, and show IPv6 addresses on network interfaces, as well as configure IPv6-specific routing rules.

How do I change the MTU size of a network interface using ip?

Answer: You can change the MTU (Maximum Transmission Unit) size with the command sudo ip link set dev [INTERFACE] mtu [SIZE], replacing [INTERFACE] with your network interface name and [SIZE] with the desired MTU value.

Is the ip command available on all Linux distributions?

Answer: The ip command is available on most modern Linux distributions by default as part of the iproute2 package. However, on some minimal installations, it might need to be installed manually.

How can I view my current routing table using ip?

Answer: You can view the current routing table by executing the command ip route show. This command displays all the routes, including the default gateway and specific network routes.

Can I use ip command to monitor network traffic?

Answer: While the ip command can provide network interface statistics, for detailed network traffic monitoring, tools like iftop, nethogs, or tcpdump are more appropriate.

Is the ip command suitable for scripting?

Answer: Absolutely. The ip command can be used in bash scripts for automating network configuration tasks. Its output can be parsed and used within scripts for various networking operations.

How do I install iproute2 on my Linux distribution if it’s not already installed?

Answer: The installation method depends on your Linux distribution. Generally, you can use the package manager like apt for Debian/Ubuntu (sudo apt install iproute2), dnf for Fedora (sudo dnf install iproute), or pacman for Arch Linux (sudo pacman -Syu iproute2).

Can I manage wireless connections using the ip command?

Answer: The ip command can be used to manage basic settings of wireless interfaces, but for detailed wireless management, tools like iwconfig or nmcli (NetworkManager Command Line Interface) are more specialized.

Conclusion

The exploration of the ip command in Linux has revealed its pivotal role in network management, offering a comprehensive suite of functionalities that surpasses its predecessor, ifconfig. Through the practical examples and detailed explanations provided, users can gain a clearer understanding of how to effectively manage and troubleshoot network interfaces and routes. The inclusion of installation guides across various distributions ensures that even beginners can equip themselves with this essential tool. With the additional FAQ section addressing common queries, this guide aims to empower users, from novices to seasoned professionals, to harness the full potential of the ip command in their daily Linux networking tasks.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.