ip - Linux


Overview

The ip command in Linux is a versatile tool used for managing network interfaces, routes, and policies. It is part of the iproute2 package and is used to configure and display the state of network interfaces, routing tables, and various network parameters. It replaces older tools such as ifconfig and route, offering a more powerful and consistent interface for network configuration and diagnostics, particularly useful for managing modern networking setups including VLANs, tunnels, and IPsec.

Syntax

The basic syntax for the ip command is:

ip [ OPTIONS ] OBJECT { COMMAND | help }
  • OBJECT: can be link, addr, route, neigh, etc., specifying the type of object to manage.
  • COMMAND: specifies the action to be taken, such as add, del, show, etc.

Common Objects and Commands:

  • link: refers to network devices.
  • addr: addresses assigned to devices.
  • route: routing table entries.
  • neigh: ARP or NDISC cache entries.

Options/Flags

Here are some commonly used options and flags with the ip command:

  • -V, --version: Show the version of the ip utility.
  • -s, --stats: Display more detailed statistics.
  • -4: Only use IPv4.
  • -6: Only use IPv6.
  • -c, --color: Colorize the output.

These options modify the output or the particular details the command will operate upon.

Examples

1. Display all network interfaces:

ip link show

2. Display the IP addresses assigned to all interfaces:

ip addr show

3. Add a new IP address to an interface:

ip addr add 192.168.1.2/24 dev eth0

4. Delete an IP address from an interface:

ip addr del 192.168.1.2/24 dev eth0

5. Change the state of a network interface (up/down):

ip link set dev eth0 up
ip link set dev eth0 down

Common Issues

  • Permissions: Most ip commands require root permissions; running without sufficient privileges will result in Operation not permitted errors.
  • Typographical Errors: Incorrect interface names or wrong IP address formats can lead to commands not taking effect.
  • Interface Down: Attempting to configure IP addresses on interfaces that are down will fail; ensure interfaces are up before configuring them.

Integration

The ip command can be combined with other commands for scripting or complex network setups:

Example Script to configure an interface:

#!/bin/bash
ip link set dev eth0 up
ip addr add 192.168.1.100/24 dev eth0
ip route add default via 192.168.1.1

This script activates an interface, assigns it an IP, and sets a default gateway.

  • ifconfig: Older command similar to ip link and ip addr.
  • netstat: Tool for displaying network connections, routing tables, interface statistics, masquerade connections, multicast memberships.
  • ss: Utility to investigate sockets.
  • traceroute: Print the route packets take to network host.

Further Reading: