Open In App

ip Command in Linux with Examples

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The ip command in Linux is a powerful utility for network configuration and management. It allows users to interact with various networking components such as network interfaces, routing tables, addresses, and more. In this guide, we will delve into the ip command, covering each aspect with examples, code, and detailed explanations.

Introduction to the IP Command in Linux

The ip command is part of the iproute2 package and serves as a versatile replacement for older networking tools like `ifconfig` and `route`. It provides a unified interface for configuring and managing network settings in modern Linux distributions.

Basic Usage and Syntax of IP Command

The basic syntax of the ip command is as follows:

ip [OPTIONS] OBJECT {COMMAND | help}

Where:

  • OPTIONS: Additional options that modify the behavior of the command.
  • OBJECT: The networking component you want to interact with (e.g., link, address, route).
  • COMMAND: The action you want to perform on the specified object.
  • help: Displays help information about the specified object or command.

Displaying Network Interfaces and IP Addresses Using IP Command

To view information about network interfaces and their associated IP addresses, use the following command:

ip addr show

displaying network interface

displaying network interface

This command displays details such as interface names (`eth0`, `wlan0`), MAC addresses, IPv4 and IPv6 addresses, subnet masks, and more.

Configuring Network Interfaces Using IP Command

To configure a network interface, you can use the `ip link` command followed by the action (e.g., `set`, `add`, `delete`).

For example: to set the IP address of an interface:

sudo ip addr add 192.168.1.100/24 dev eth0

This command assigns the IP address `192.168.1.100` with a subnet mask of `24` (equivalent to 255.255.255.0) to the eth0 interface.

Managing Routing Tables Using IP Command

Example 1: Deleting a Route

To delete an existing route from the routing table, you can use the `ip route delete` command. For example:

sudo ip route delete 10.0.0.0/24 via 192.168.1.1 dev eth0

This command removes the route to the `10.0.0.0/24` network via the gateway `192.168.1.1` through the `eth0` interface.

Example 2: Changing the Default Gateway

To change the default gateway for outgoing traffic, you can modify the default route using the `ip route` command. For instance:

sudo ip route add default via 192.168.1.254 dev eth0

This command sets `192.168.1.254` as the new default gateway through the `eth0` interface.

Changing Interface State Using IP Command

Example 1: Bringing an Interface Up

To bring an interface up (activate it), you can use the `ip link` command with the `set` action. For example:

sudo ip link set eth0 up

This command brings the `eth0` interface up, enabling it to send and receive network traffic.

Example 2: Changing MTU (Maximum Transmission Unit)

To change the MTU (maximum transmission unit) of a network interface, you can use the `ip link` command with the `set` action and the `mtu` parameter. For instance:

sudo ip link set eth0 mtu 1500

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

Displaying Detailed Interface Statistics Using IP Command

Example 1: Monitoring Interface Traffic

To monitor real-time network traffic on a specific interface, you can use the `ip -s link` command in combination with tools like `watch` or `grep` to filter the output. For example:

watch -n 1 "ip -s link show eth0 | grep 'RX bytes'"

This command continuously monitors the receive (RX) traffic on the `eth0` interface, updating every second.

Example 2: Displaying Interface Errors

To identify potential issues with a network interface, you can use the ip -s link command to display detailed statistics, including error counts. For instance:

ip -s link show eth0 | grep -E 'errors|dropped'

This command shows statistics related to packet errors and dropped packets on the eth0 interface.

Options available in the IP command in Linux

Options

Description

Example Usage

address

Show all IP addresses associated with all network devices.

ip address

Show information related to a specific interface.

ip address show (interface)

link

Display link layer information, including characteristics of link layer devices currently available.

ip link

Show statistics of various network interfaces.

ip -s link

Show statistics of a specific network interface.

ip -s link show (interface)

route

Display routing table, showing the route packets your network will take.

ip route

add

Assign an IP address to an interface.

ip a add (ip_address) dev (interface)

del

Delete an assigned IP address from an interface.

ip a del (ip_address) dev (interface)

up

Enable a network interface.

ip link set (interface) up

down

Disable a network interface.

ip link set (interface) down

monitor

Monitor and display the state of devices, addresses, and routes continuously.

ip monitor

help

Display help information about the `ip` command.

ip help

neighbour

View MAC address of devices connected to the system.

ip neighbour

Delete an ARP entry.

ip neighbour del (ip_address) dev (interface)

Add an ARP entry.

ip neighbour add (ip_address) dev (interface)

1. address

 This option is used to show all IP addresses associated with all network devices.

ip address

ip address

ip address

This will show the information related to all interfaces available on our system.

2. link

It is used to display link layer information; it will fetch characteristics of the link layer devices currently available. Any networking device which has a driver loaded can be classified as an available device.

ip link

ip link

ip link

 This link option when used with -s option is used to show the statistics of the various network interfaces.

ip -s link

ip -s link

ip -s link

 And, to get information about a particular network interface, add an option show followed by the name of the particular network interface.

ip -s link show (interface)

For Example:

ip -s link show enp3s0

ip -s link show enp3s0

ip -s link show enp3s0

3. monitor: 

This command can monitor and display the state of devices, addresses and routes continuously.

ip monitor

ip monitor

ip monitor

4. neighbour:

This command is used to view the MAC address of the devices connected to your system.

ip neighbour

ip neighbour

ip neighbour

  • STABLE: This means that the neighbor is valid, but is probably already unreachable, so the kernel will try to check it at the first transmission.
  • REACHABLE: This means that the neighbor is valid and reachable.
  • DELAY: This means that a packet has been sent to the stable neighbor and the kernel is waiting for confirmation.
  • Delete an ARP entry:
ip neighbour del (ip_address) dev interface

For Example:

ip neighbour del 192.168.0.200 dev enp3s0

Add an ARP entry:

ip neighbour add (ip_address) dev interface

For Example:

ip neighbour add 192.168.0.200 dev enp3s0

Conclusion

The `ip` command in Linux is a powerful tool that is used for performing administrative tasks. We can say that this command is the improved version of the deprecated `ifconfig` command. As we discussed, this command can be used to manipulate devices, routing and tunnels. This `ip` command can also monitor the state of devices, routes and addresses. Overall, we can say that this command is a versatile tool that can be helpful network administrators manage their networks more efficiently.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads