ip(including-relevant-subcommands)

The ip command in Linux is a powerful and flexible tool for network configuration and management. It is used for a wide range of tasks related to networking, such as assigning IP addresses, routing, managing network interfaces, and controlling network devices. The ip command is part of the iproute2 package, which is the preferred set of utilities for network management on Linux.

Overview of the ip Command

The basic syntax of the ip command is:

ip [ OPTIONS ] OBJECT [ COMMAND ]

Where:

  • OPTIONS: Optional flags and parameters to modify the behavior of the command.

  • OBJECT: The type of object to operate on (e.g., link, addr, route).

  • COMMAND: The action to perform on the object (e.g., add, delete, show).

Common Subcommands and Use Cases

1. ip link - Manage Network Interfaces

The ip link command is used to manage network interfaces. This includes actions like bringing interfaces up or down, setting interface parameters, and viewing interface details.

  • Show all network interfaces:

    ip link show
  • Bring an interface up:

    ip link set dev eth0 up
  • Bring an interface down:

    ip link set dev eth0 down
  • Change interface MTU (Maximum Transmission Unit):

    ip link set dev eth0 mtu 1400
  • Rename a network interface:

    ip link set dev eth0 name newname

2. ip addr - Manage IP Addresses

The ip addr command is used to show and configure IP addresses assigned to network interfaces.

  • Show IP addresses for all interfaces:

    ip addr show
  • Assign an IP address to an interface:

    ip addr add 192.168.1.100/24 dev eth0
  • Delete an IP address from an interface:

    ip addr del 192.168.1.100/24 dev eth0
  • Assign an alias (secondary IP address):

    ip addr add 192.168.1.101/24 dev eth0 label eth0:0

3. ip route - Manage Routing Table

The ip route command is used to view and modify the system's routing table. This is useful for controlling how network traffic is directed.

  • Show the routing table:

    ip route show
  • Add a new route:

    ip route add 192.168.2.0/24 via 192.168.1.1
  • Delete a route:

    ip route del 192.168.2.0/24
  • Add a default route:

    ip route add default via 192.168.1.1

4. ip link set - Change Link Parameters

This command allows you to modify the configuration of a network interface.

  • Set the interface to promisc mode (for packet sniffing):

    ip link set dev eth0 promisc on
  • Disable promiscuous mode:

    ip link set dev eth0 promisc off

5. ip addr flush - Remove IP Addresses

This command is used to remove all IP addresses from a network interface.

  • Flush all IP addresses from an interface:

    ip addr flush dev eth0

6. ip link set - Change Interface MTU

You can adjust the Maximum Transmission Unit (MTU) size for network interfaces.

  • Set MTU size for an interface:

    ip link set dev eth0 mtu 1500

7. ip maddr - Manage Multicast Addresses

The ip maddr command is used to show and configure multicast addresses for network interfaces.

  • Show multicast addresses:

    ip maddr show
  • Add a multicast address to an interface:

    ip maddr add 224.0.0.1 dev eth0
  • Delete a multicast address from an interface:

    ip maddr del 224.0.0.1 dev eth0

8. ip tunnel - Manage Tunnels

Tunnels allow network packets to be encapsulated for transmission over a network.

  • Create a new GRE tunnel:

    ip tunnel add gre1 mode gre remote 192.168.1.1 local 192.168.1.2
  • Bring up a tunnel interface:

    ip link set gre1 up
  • Delete a tunnel:

    ip tunnel del gre1

9. ip netns - Manage Network Namespaces

Network namespaces allow the creation of isolated network environments.

  • Create a new network namespace:

    ip netns add ns1
  • Assign a network interface to a namespace:

    ip link set eth0 netns ns1
  • List all network namespaces:

    ip netns list

10. ip link set dev - Change Interface States

  • Set interface to up:

    ip link set dev eth0 up
  • Set interface to down:

    ip link set dev eth0 down

Advanced Examples

  • Show all IP addresses for a specific interface:

    ip addr show dev eth0
  • Set the default route to a new gateway:

    ip route add default via 192.168.1.1
  • Add a static route to a network:

    ip route add 10.10.10.0/24 via 192.168.1.1
  • Change the MTU of an interface:

    ip link set dev eth0 mtu 1400
  • Remove an interface's IP address:

    ip addr del 192.168.1.100/24 dev eth0

Conclusion

The ip command is an essential tool for network management on Linux systems. It offers a broad range of functionalities, including managing network interfaces, routing, and IP addresses. Mastering this tool is crucial for anyone managing Linux network configurations, whether for personal use or in large-scale enterprise environments.

Last updated