ip addr

Introduction to the ip addr Command

The ip addr command, often shortened as ip a, is part of the IProute2 suite and is a powerful tool for managing and displaying network interfaces in Linux-based systems. It replaces older commands like ifconfig and provides more detailed information and capabilities. This command lets users examine the state of network interfaces, assign or remove IP addresses, set up routing, and perform other networking tasks.

Purpose of ip addr

The primary purpose of ip addr is to show and manipulate network addresses and interface parameters. It is a fundamental command that system administrators use to diagnose, troubleshoot, and configure network interfaces on Linux systems.

Basic Structure of the Command

The command follows this basic syntax:

ip addr <subcommand> [options] <interface>

The ip command contains several subcommands related to different network configurations. The most common subcommand in the context of managing IP addresses is addr.

Understanding the Output of ip addr show

When you execute the command ip addr show (or its shorthand ip a), you receive a list of all active network interfaces and their properties. Here’s an example of the output:

$ ip addr 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
       valid_lft 86400sec preferred_lft 86400sec
    inet6 fe80::5054:ff:fe12:3456/64 scope link 
       valid_lft forever preferred_lft forever

Explanation of the Output

  1. Interface Number and Name: 1: lo, 2: eth0

    • The numbers (1, 2) represent the interface index, and the names (lo, eth0) indicate the interface names. Common names include lo for the loopback interface and ethX for Ethernet interfaces.

  2. Interface Flags: <LOOPBACK,UP,LOWER_UP> and <BROADCAST,MULTICAST,UP,LOWER_UP>

    • These flags describe the state and capabilities of the interface. Here’s a breakdown of common flags:

      • UP: The interface is active.

      • LOOPBACK: Indicates a loopback interface.

      • BROADCAST: The interface supports broadcasting (used for sending packets to all devices in the network).

      • MULTICAST: The interface supports multicast traffic (sending packets to multiple specified devices).

      • LOWER_UP: Indicates that the link is physically connected and working.

  3. MTU: This is the Maximum Transmission Unit, which specifies the largest packet size that can be transmitted. For example, mtu 1500.

  4. Queueing Discipline (qdisc): This specifies the queuing mechanism in use. In the example, it’s shown as qdisc noqueue for the loopback and qdisc mq (multi-queue) for the Ethernet interface.

  5. Link Layer Information:

    • For lo: link/loopback signifies a loopback interface.

    • For eth0: link/ether 52:54:00:12:34:56 indicates that it’s an Ethernet interface with the MAC address 52:54:00:12:34:56.

  6. IP Address and Subnet Mask:

    • inet 127.0.0.1/8 scope host lo: This shows the IPv4 address 127.0.0.1 with a subnet mask of /8 on the lo (loopback) interface.

    • inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0: This line indicates that the interface eth0 has been assigned the IPv4 address 192.168.1.100 with a subnet mask of /24. The brd 192.168.1.255 indicates the broadcast address, and the scope global specifies that it’s accessible beyond the local machine.

  7. Lifetime Information:

    • valid_lft 86400sec refers to the amount of time (in seconds) that the IP address is valid.

    • preferred_lft 86400sec indicates how long the address is preferred for new connections. After this time, the system might start using other addresses (if available).

  8. IPv6 Address:

    • inet6 fe80::5054:ff:fe12:3456/64 scope link is an example of an IPv6 address with a /64 prefix and scope link, indicating it’s a link-local address.

Using Subcommands with ip addr

1. Displaying All Addresses (ip addr show)

To show all IP addresses and details for all interfaces, you use:

This will display all active and inactive interfaces, along with their associated IP addresses, MAC addresses, and additional parameters.

2. Displaying a Specific Interface (ip addr show <interface>)

If you want to view details of a specific interface, use:

This displays only the information related to eth0.

3. Adding an IP Address (ip addr add)

To manually assign an IP address to an interface, use:

Here, 192.168.1.200/24 is the IP address and subnet mask you are assigning to the eth0 interface.

4. Deleting an IP Address (ip addr del)

To remove an IP address from an interface, use:

This deletes the IP address 192.168.1.200 from the eth0 interface.

Key Concepts in ip addr

  1. Link Layer (MAC Addresses): The link/ether part in the output denotes that the interface is Ethernet-based and displays its MAC address. MAC addresses are essential for uniquely identifying devices within a local network.

  2. IP Addresses and Subnets: IP addresses can be either IPv4 or IPv6. Subnets are represented using a CIDR (Classless Inter-Domain Routing) notation, like /24, which defines the network and host portions of an IP address.

  3. Scope: This defines the visibility and reach of an IP address. Some common scopes are:

    • global: Accessible from anywhere on the network.

    • link: Only accessible within the same link or subnet.

    • host: Only accessible from within the local machine.

  4. Broadcast Addresses: The broadcast address is used to communicate with all devices in a specific subnet. In ip addr show, it is displayed as brd.

  5. Lifetime (valid_lft and preferred_lft): These specify how long the address remains valid and preferred for new connections.

Basic Network Configuration with ip addr

  1. Assigning Multiple IP Addresses: You can assign more than one IP address to a single network interface using the ip addr add command. This is useful in cases where a server needs to be reachable through multiple IPs.

  2. Changing the MTU Size: Sometimes you need to change the MTU size for an interface for performance optimization or compatibility. This can be done using:

  3. Enabling or Disabling an Interface: You can bring an interface up or down using:

Using Additional Options

  • Verbose Mode: To display more detailed information about each interface, you can use the -d option for detailed output:

  • Listing IP Addresses in JSON Format: If you’re scripting or need structured output, you can use the -j option to get JSON-formatted output:

Comparison with Older Commands

The ip addr command replaces older commands like ifconfig. Compared to ifconfig, it provides more options, better control, and extended support for modern networking protocols like IPv6

. Here’s a quick comparison:

Feature
ifconfig
ip addr

IPv6 Support

Limited

Full

Multiple IPs on One Interface

Not Well Supported

Fully Supported

Display of Detailed Interface Flags

Limited

Comprehensive

When to Use ip addr?

  • Network Troubleshooting: When you’re diagnosing a network issue, the ip addr command provides detailed information about the state of your interfaces, IP addresses, and potential configuration issues.

  • Adding/Removing Addresses: You can use ip addr to manually assign or remove IP addresses for network testing, configuration, or recovery.

  • Checking Interface Status: It helps you understand the current state of your network interfaces and their configuration.

Summary

The ip addr command is a crucial tool in a Linux system administrator’s toolkit. It provides comprehensive control over network interfaces, IP addresses, and link layer parameters. With its detailed output and flexible options, it is an essential command for configuring, monitoring, and troubleshooting network interfaces. By understanding key concepts like link layers, scopes, and lifetime parameters, you’ll be equipped to manage and diagnose your system’s network configuration effectively.

Last updated