Linux ip Command: A Comprehensive Guide with 6 Practical Examples

Try this guide with our instant dedicated server for as low as 40 Euros

Managing and modifying the network tasks is an important aspect of the job of a sysadmin. 

An important aspect of these tasks is to manage network configurations. For this, the ip command plays an important role in managing network operations.

Short for Internet Protocol, the ip command is a powerful utility for network configuration and management. It allows network administrators to configure network interfaces, assign IP addresses, and set up routing. 

The ip command is a relatively recent networking command-line utility for assigning IP addresses and modifying network variables on Linux systems. 

Before the introduction of ip, Linux relied on the ifconfig command for similar functionalities. However, ifconfig lacked the comprehensiveness and advanced features of ip.

In this tutorial, we will go into the details of the ip command. We will also present five scenarios where you can use the utility to manage or modify critical network settings. 

Table Of Contents

  1. A Short Introduction to the ip Command
    1. Display and Manage Network Interfaces
  2. Five Scenarios Where the ip Command Simplifies Network Management
    1. Example #1: View Network Interface Information
    2. Example #2: Modify the Status of Network Interface
    3. Example #3: Manage and Monitor IP Addresses
    4. Example #4: How to Add IP Address in Linux
    5. Example #5: Display and Manage IP Routing Table
    6. Example #6: Display and Modify IP Neighbor Entries
  3. Conclusion
  4. FAQs

A Short Introduction to the ip Command

The basic syntax of ip command is as follows:

# ip [OPTION] OBJECT {COMMAND | help}

Here, 

OPTION (Optional): Specifies additional settings for the command.

COMMAND: The specific action you want to perform (show, add, delete, etc.).

help: Displays help information for the command.

OBJECT: Define the network node/component you want to manage (link, address, route).

The ip command offers several functionalities through different OBJECTS. The OBJECTS (or subcommands) that are used most often include:

  • link (or l): Manage network connections.
  • address (or addr or a): View and modify IP or IPv6 addresses assigned.
  • route (or r): Manage the routing table, determining how data packets reach their destinations.
  • neighbor (or n): Manage devices connected to your network.

The functionalities are not just limited to these entities. You can view the complete list of functionalities by running this command:

# ip help

ip help

Considerations

  • You can execute commands with shortened versions. For instance, the ip link is the same as the ip l.
  • Running ip commands requires root or sudo privileges to make changes to your network configuration.

Warning: Changes made with ip commands typically won’t persist after a server restart. You can sidestep this restriction by applying one of the following:

  • Add the commands to a startup script.
  • Edit the relevant configuration files in your Linux installation.

Display and Manage Network Interfaces

The ip command provides powerful tools for managing network interfaces in Linux.

You can get a detailed list of all link command options by running this command in the terminal:

# ip link help

ip link help

Five Scenarios Where the ip Command Simplifies Network Management

Now that you have a clear idea of how the ip command works, let’s explore the following scenarios where we demonstrate the use of the command to view or modify specific network interfaces.

Example #1: View Network Interface Information

Viewing network interface information is one of the core capabilities of the ip command. Here are a few examples of using the command to view specific network information.

View All Interfaces

To get a detailed overview of all available network interfaces, including names and status, run this command:

# ipl link show

View Specific Interface Details

You can view information on a particular device, such as eth0 or wlan0, by running the following command:

# ip link show dev [device]

Replace [device] with the actual interface name.

Check Interface Statistics

You can use the ip command to view the status for all network interfaces (like transferred or dropped packets, and errors). We recommend the following command in this context:

# ip -s link

Detailed Statistics for a Specific Interface

A common use case of the command is to get in-depth statistics for a specific interface. In this context, we recommend the following command syntax:

# ip -s link ls [interface]

Adding another -s to the command provides even more detailed output.

# ip -s -s link ls [interface]

Filter Active Interfaces

Finally, we recommend the following command to view a list of currently active interfaces:

# ip link ls up

Example #2: Modify the Status of Network Interface 

You can use the ip command to control the state and behavior of network interfaces. Here are three scenarios where you can benefit from using the command.

Activate Network Interfaces

Activating a network interface is the process of bringing it online. Use the following command to initiate the process:

# ip link set [interface] up

Deactivate Network Interfaces

Alternatively, you can run the following command to deactivate an interface and take it offline:

# ip link set [interface] down

Replace [interface] with the actual interface name

Advanced Interface Configuration

The ip link command allows you to control how fast interfaces send data by adjusting the transmit queue. This can be useful for fine-tuning network performance based on your hardware and network conditions.

The following command configures the interface:

# ip link set txqueuelen [number] dev [interface]

Replace [number] with the desired queue length and [interface] with the interface name.

Set Maximum Transmission Unit (MTU)

The MTU defines the largest packet size that can be transmitted on a network. Optimizing the MTU can improve network performance and help you avoid network congestion.

We recommend the following command to adjust the MTU to enhance how well the network performs:

# ip link set mtu [number] dev [interface]

Replace [number] with the desired MTU value and [interface] with the interface name.

Note: These advanced configuration options require a deeper understanding of network operations. Proceed with caution and consult network documentation if required.

Example #3: Manage and Monitor IP Addresses

You can also use the ip command to manage and monitor IP addresses on your network interfaces. The starting point is the following command that helps you view all available options:

# ip addr help

ip addr help

List All Devices

To get a basic overview of all network devices on your system, run the following command in the terminal:

# ip addr

Detailed Interface Information

The following command helps you view a list of all network interfaces and their corresponding IP addresses:

# ip addr show

Get the Details on a Specific Interface

You can narrow down and get information about a particular interface by running the following command:

# ip addr show dev [interface]

Filter by IP Version

To view only the IPv4 addresses, run:

# ip -4 addr

Alternatively, you can run the following command to view only the IPv6 addresses:

# ip -6 addr

Example #4: How to Add IP Address in Linux

You can use the ip command to assign an IP address to an interface. For this, run the following command in the terminal:

# ip addr add [ip_address] dev [interface]

Replace [IP_address] with the desired IP address and [interface] with the interface name.

Note: If the interface you mention doesn’t exist, you will see an error message.

Add a Broadcast Address

Use this command to add a broadcast address for an interface: 

# ip addr add brd [ip_address] dev [interface]

Delete an IP Address

You can use the following command to delete an IP address from an interface:

# ip addr del [ip_address] dev [interface]

Note: These commands typically require root or sudo privileges.

Example #5: Display and Manage IP Routing Table

In a networked environment, the routing table determines how data packets travel across your network. You can use the following command to see a complete list of ip route commands:

# ip route help

ip route help

List All Routes

Run one of the following commands to display all entries in the kernel’s routing table:

# ip route 

Or

# ip route list

You can narrow down the search by adding a SELECTOR object:

# ip route list SELECTOR

Note: SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ] [ table TABLE_ID ] [ proto RTPROTO ] [ type TYPE ] [ scope SCOPE ]

Filter Routes 

You can see routing for a specific network that can be accessed directly through a particular interface by running this command in the terminal:

# ip route list [ip_address]

Replace [ip_address] with the desired network.

Add a Route to a Device

You can specify a route for a network that can be accessed directly through a particular interface. For this, use the following command:

# ip route add [ip_address] dev [interface]

Replace [ip_address] with the network you want to reach and [interface] with the interface connected to that network.

Add a Route via a Gateway

Alternatively, you can add a new route through a gateway by executing the following command:

# ip route add [ip_address] via [gatewayIP]

Replace [ip_address] with the network you want to reach and [gateway IP] with the IP address of the router that acts as the gateway.

Add a Default Gateway

The default gateway is used for all destinations not explicitly defined in the routing table. You can add a default gateway in the routing table with one of the following commands:

# ip route add default [ip_address] dev [device]

# ip route add default [network/mask] via [gatewayIP]

Remove a Specific Route

To remove a route entry from the routing table, run this command in the terminal:

# ip route del [ip_address]

Remove the Default Gateway

Alternatively, you can remove a default gateway entry with the following command:

# ip route del default

Remove a Route via Device

You can easily remove a route associated with a specific interface by running the following command:

# ip route del [ip_address] dev [interface]

Example #6: Display and Modify IP Neighbor Entries

IP neighbor entries map IP addresses (protocol addresses) to hardware (link-layer) addresses (like MAC addresses) on your network.

Neighbor entries connect the protocol and the link-layer address. In IPv4 tables, they’re known as ARP (Address Resolution Protocol) tables.

Run the following command to get a complete list of all neighbor command options:

# ip neigh help

ip neigh help

Display IP Neighbor Entries

Start with the following command to display neighbor tables:

# ip neigh show

The output shows the IP addresses of neighboring devices along with their corresponding MAC addresses and state. The state can have the following statuses:

  • REACHABLE: Indicates a valid, reachable entry until the timeout ends.
  • PERMANENT: Represents a static entry that only an administrator can delete.
  • STALE: Shows a previously valid that’s currently unreachable; the kernel verifies it when data is sent.
  • DELAY: Indicates the kernel is still waiting to confirm the validity of a potentially stale entry.

Modify IP Neighbor Entries

Use the following command to create a new entry in the table:

# ip neigh add [ip_address] dev [interface]

Note: The ip neigh command is used to create a new entry in the neighbor table, although this is typically not necessary as the ARP protocol automatically creates entries. As a result, we recommend using this command with caution. 

Delete a Neighbor Entry

Alternatively, you can delete an existing ARP entry with this command:

# ip neigh del [ip_address] dev [interface]

Conclusion

The Linux ip command is a powerful tool for managing network configurations in Linux systems. The ip command provides a wide range of functionalities, from viewing network interfaces to configuring IP addresses and managing routing tables. 

By understanding and utilizing the various options and syntax of the ip command, users can effectively manage and troubleshoot their network setups in Linux environments. Whether setting up new connections, monitoring network traffic, or troubleshooting connectivity issues, the ip command is an indispensable tool for system administrators and network engineers. With its flexibility and versatility, the ip command empowers users to configure and maintain their network infrastructure in Linux efficiently.

FAQs

Q. What is the ifconfig command?

The ifconfig command is used in Linux to view and configure network interfaces, such as Ethernet interfaces.

Q. How do I view network devices in Linux?

You can view network devices in Linux by using commands like ifconfig or ip, which display information about network cards and their configurations.

Q. Where can I find network configuration files in Linux?

Network configuration files in Linux are typically located in directories like /etc/network/ or /etc/sysconfig/network-scripts/.

Q. What is the iproute2 package used for?

The iproute2 package is a set of Linux utilities used for network configuration and monitoring, including managing default gateways and routes.

Q. What is a default gateway in Linux?

In Linux, a default gateway is the route through which network traffic not destined for the local network is forwarded.

Q. How do I set a default route in Linux?

You can set a default route in Linux using commands like ip route add default via [gateway].

Q. What are physical interfaces in Linux networking?

Physical interfaces in Linux networking refer to the actual network cards or adapters, such as Ethernet interfaces.

Q. What is the neigh command used for?

The neigh command in Linux is used to manage neighbor objects, such as ARP (Address Resolution Protocol) entries, which map IP addresses to MAC addresses.

Q. How does the ip utility differ from ifconfig in Linux?

The ip utility, part of the iproute2 package, provides more advanced and comprehensive features for network configuration compared to ifconfig.

Q. How do I configure a static IP address for the eth0 interface?

To configure a static IP address for the eth0 interface in Linux, you can edit the network configuration files (like /etc/network/interfaces) and specify the desired IP address, subnet mask, gateway, and DNS servers.

Try this guide with our instant dedicated server for as low as 40 Euros