Linux ip command


The ip command in Linux is a versatile and powerful tool used for managing and configuring network interfaces, routing, and IP addresses. It is considered the successor to older networking tools like ifconfig, route, and netstat, offering a more modern and unified approach to network management.

Basic Syntax of the ip Command

ip [options] OBJECT { COMMAND | help }
  • OBJECT: The type of object you want to manage (e.g., link, addr, route, etc.).
  • COMMAND: The action you want to perform on the object (e.g., show, add, del).
  • help: Displays help for the ip command.

Common Objects and Commands

  • link: Manage network interfaces (similar to ifconfig).
  • addr: Manage IP addresses (IPv4 and IPv6).
  • route: Manage routing tables.
  • neigh: Manage ARP (Address Resolution Protocol) cache.
  • maddr: Manage multicast addresses.
  • tunnel: Manage IP tunnels.

Examples of Using the ip Command

1. Display Network Interfaces

  • To display all network interfaces and their configurations:
    ip link show
  • Example Output:
    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 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:4e:9a:16 brd ff:ff:ff:ff:ff:ff
  • Explanation:
    • lo: The loopback interface (for internal communication).
    • eth0: The first Ethernet interface.
    • UP: Indicates that the interface is up and active.
    • mtu: Maximum transmission unit size for the interface.

2. Display IP Addresses

  • To display the IP addresses of all interfaces:
    ip addr show
  • Example Output:
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0 valid_lft 86032sec preferred_lft 86032sec
  • Explanation:
    • inet: Displays the IPv4 address assigned to the interface.
    • inet6: Displays the IPv6 address assigned to the interface (not shown in the above output, but would be listed if assigned).
    • /24: The subnet mask in CIDR notation (255.255.255.0 in this case).

3. Bring an Interface Up or Down

  • To bring an interface up (activate it):
    sudo ip link set eth0 up
  • To bring an interface down (deactivate it):
    sudo ip link set eth0 down

4. Assign an IP Address to an Interface

  • To assign a static IP address to an interface:
    sudo ip addr add 192.168.1.100/24 dev eth0
  • Explanation:
    • This command assigns the IP address 192.168.1.100 with a subnet mask of 255.255.255.0 (CIDR notation /24) to the interface eth0.

5. Delete an IP Address from an Interface

  • To delete an IP address from an interface:
    sudo ip addr del 192.168.1.100/24 dev eth0

6. Show Routing Table

  • To display the routing table:
    ip route show
  • Example Output:
    default via 192.168.1.1 dev eth0 proto static 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
  • Explanation:
    • default: The default route for traffic that doesn’t match any other route.
    • via 192.168.1.1: Traffic should be forwarded through the gateway 192.168.1.1.
    • 192.168.1.0/24: This network is directly reachable via eth0.

7. Add a Route

  • To add a static route:
    sudo ip route add 10.0.0.0/24 via 192.168.1.1 dev eth0
  • Explanation:
    • This adds a route to the network 10.0.0.0/24 via the gateway 192.168.1.1 using the interface eth0.

8. Delete a Route

  • To delete a route:
    sudo ip route del 10.0.0.0/24 via 192.168.1.1 dev eth0

9. View ARP Cache

  • To display the ARP cache (which maps IP addresses to MAC addresses):
    ip neigh show
  • Example Output:
    192.168.1.1 dev eth0 lladdr 00:11:22:33:44:55 REACHABLE 192.168.1.10 dev eth0 lladdr 08:00:27:4e:9a:16 STALE

Summary of Common ip Command Options

CommandDescription
ip link showDisplay network interfaces
ip addr showShow IP addresses for all interfaces
ip addr add [IP] dev [interface]Assign an IP address to an interface
ip addr del [IP] dev [interface]Remove an IP address from an interface
ip link set [interface] upBring an interface up
ip link set [interface] downBring an interface down
ip route showDisplay the routing table
ip route add [network] via [gateway]Add a new route
ip route del [network] via [gateway]Delete a route
ip neigh showDisplay ARP cache

ip vs ifconfig

  • ip command is more powerful and modern compared to ifconfig. It integrates multiple network functions (address management, route management, link management) into one tool, while ifconfig is mainly for displaying and configuring interfaces.
  • ip uses more flexible and intuitive syntax, supporting both IPv4 and IPv6, while ifconfig was limited to IPv4.

The ip command is widely used in modern Linux systems for its robustness and flexibility.