How to use the command 'ip' (with examples)
- Linux
- December 25, 2023
The ‘ip’ command in Linux is a powerful tool for managing networking configurations. It allows you to show and manipulate routing, devices, policy routing, and tunnels. The command offers various subcommands, each with its own usage documentation. In this article, we will explore several common use cases of the ‘ip’ command and provide examples for each.
Use case 1: List interfaces with detailed info
Code:
ip address
Motivation:
Listing interfaces with detailed information can be useful when troubleshooting networking issues or configuring network settings. It provides an overview of the network configuration on the system, including IP addresses, network masks, and interface names.
Explanation:
The ‘address’ subcommand is used to display or manipulate the IP addresses and routing settings of network interfaces. When invoked without any additional options, it lists all interfaces with detailed information.
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
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
5: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff
inet 192.168.0.100/24 brd 192.168.0.255 scope global dynamic eth0
valid_lft 84611sec preferred_lft 84611sec
Use case 2: List interfaces with brief network layer info
Code:
ip -brief address
Motivation:
When you need a quick overview of the system’s network configuration, it can be helpful to obtain a concise list of interfaces with brief network layer information. This allows you to focus on essential details without overwhelming you with extensive output.
Explanation:
The ‘-brief’ option is used with the ‘ip address’ command to display a more compact output. Instead of providing detailed information about each interface, the command shows a shorter summary, including interface index, interface name, and IP addresses.
Example output:
1: lo <LOOPBACK,UP,LOWER_UP> 127.0.0.1/8
5: eth0 <BROADCAST,MULTICAST,UP,LOWER_UP> 192.168.0.100/24
Use case 3: List interfaces with brief link layer info
Code:
ip -brief link
Motivation:
In situations where you primarily want to obtain link layer details of the interfaces, using the ‘ip link’ command with the ‘-brief’ option can provide a concise output. This can be useful for quickly checking the connectivity status or inspecting the MAC address of each interface.
Explanation:
The ‘-brief’ option used with the ‘ip link’ command displays a summarized output of the link layer information for each network interface. It includes the interface index, name, and link flags.
Example output:
1: lo <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
5: eth0 <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
Use case 4: Display the routing table
Code:
ip route
Motivation:
Viewing the routing table can help you understand how network traffic is being routed on your system. It shows the available routes along with their destination networks and associated gateways.
Explanation:
The ‘route’ subcommand is used to display or modify the IP routing table. When invoked without any additional options, the ‘ip route’ command lists the system’s routing table.
Example output:
default via 192.168.0.1 dev eth0 proto dhcp src 192.168.0.100 metric 100
192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.100 metric 100
Use case 5: Show neighbors (ARP table)
Code:
ip neighbour
Motivation:
The ARP (Address Resolution Protocol) table maps IP addresses to MAC addresses and helps in establishing communication within the network. Displaying the neighbors (ARP table) can be valuable when troubleshooting network connectivity issues or verifying MAC address mappings.
Explanation:
The ’neighbour’ subcommand, invoked without any additional options, displays the ARP table (also known as the neighbors table).
Example output:
192.168.0.1 dev eth0 lladdr xx:yy:zz:aa:bb:cc REACHABLE
Use case 6: Make an interface up/down
Code:
ip link set interface up/down
Motivation:
Changing an interface’s operational state, such as bringing it up or down, can be necessary for various reasons. This command allows you to enable or disable network interfaces, which can be useful when managing network connections or troubleshooting network-related issues.
Explanation:
The ’link’ subcommand, with the ‘set’ sub-subcommand, can be used to modify the state of a network interface. The ‘up’ and ‘down’ arguments are used to bring the specified interface up or down, respectively.
Example:
To bring the ’eth0’ interface up:
ip link set eth0 up
Use case 7: Add/Delete an IP address to an interface
Code:
ip addr add/del ip/mask dev interface
Motivation:
Adding or deleting an IP address to/from a network interface is often required when configuring networking settings or troubleshooting connectivity problems. This is particularly useful when you need to assign multiple IP addresses or modify the existing IP configuration.
Explanation:
The ‘addr’ subcommand, with the ‘add’ or ‘del’ sub-subcommand, is used to add or delete an IP address to/from a network interface, respectively. The ‘ip/mask’ argument represents the IP address and subnet mask, while the ‘interface’ argument specifies the interface to be modified.
Example (adding an IP address to an interface):
ip addr add 192.168.0.200/24 dev eth0
Use case 8: Add a default route
Code:
ip route add default via ip dev interface
Motivation:
Adding a default route is necessary to enable network communication outside your local network. It allows the system to forward traffic to destinations not explicitly defined in the routing table.
Explanation:
The ‘route’ subcommand, with the ‘add’ sub-subcommand, is used to add a new route to the routing table. The ‘default’ argument represents the default route, while the ‘via ip’ and ‘dev interface’ specify the gateway IP address and the network interface, respectively.
Example:
ip route add default via 192.168.0.1 dev eth0
Conclusion:
The ‘ip’ command in Linux is a versatile tool for managing various aspects of networking configurations. We have covered several common use cases, including listing interfaces, displaying routing tables, modifying interface states, and configuring IP addresses. Understanding these examples will empower you to effectively manage and troubleshoot network-related tasks on your Linux system.