The ip command is the modern, powerful utility for network configuration on Linux, superseding older tools like ifconfig. Mastering ip is critical for any system administrator or power user seeking precise control over network interfaces, routing tables, and arp caches. This guide will meticulously detail the fundamental operations of the ip command, enabling you to effectively display, add, remove, and modify network interface configurations. Understanding these core functions is indispensable for maintaining robust and reliable network connectivity.
Prerequisites
- Access to a Linux system (e.g., Ubuntu, CentOS, Debian).
- A terminal or command-line interface.
sudoprivileges to modify network configurations. Basic familiarity with command-line operations is assumed.
1. Display Network Interface Information
Begin by examining your current network configuration. The ip a (or ip addr) command provides a comprehensive overview of all network interfaces, including their state, IP addresses, and MAC addresses.
1.1. View All Network Interfaces
Execute the following to list all active and inactive interfaces with their assigned IP addresses.
ip a
Pro-Tip: The output will show interfaces like lo (loopback), eth0 or enp0s3 (Ethernet), and wlan0 (wireless). Pay attention to the interface names; they vary based on hardware and Linux distribution.
1.2. View Specific Interface Details
To narrow down the output to a single interface, specify its name:
ip a show eth0
Replace eth0 with your actual interface name. This is useful for focused troubleshooting.
2. Control Network Interface State
Managing the operational state of an interface is a fundamental task. You can bring interfaces up (activate) or down (deactivate) as needed, which is essential for applying configuration changes or conserving resources.
2.1. Bring an Interface Up
To activate a network interface, use the up subcommand. This makes the interface ready to send and receive traffic.
sudo ip link set eth0 up
Warning: Deactivating and reactivating the primary network interface on a remote server will temporarily disconnect you. Ensure you have alternative access or a script to bring it back up if something goes wrong.
2.2. Bring an Interface Down
To deactivate an interface, use the down subcommand. This stops all network traffic through that interface.
sudo ip link set eth0 down
Practical Tip: Bringing an interface down is often a prerequisite before making significant changes to its configuration, such as changing its MAC address or adding a new IP address range that might conflict.
3. Assign IP Addresses to Interfaces
Configuring IP addresses is the core function of network interface management. The ip addr add and ip addr del commands facilitate this with precision.
3.1. Add an IP Address
Assign a new IP address and subnet mask (CIDR notation) to an interface. This command adds the address without removing existing ones, allowing for multiple IP addresses on a single interface.
sudo ip addr add 192.168.1.100/24 dev eth0
Pro-Tip: The /24 denotes the subnet mask (255.255.255.0). Always ensure your IP address falls within the correct network segment and does not conflict with other devices.
3.2. Remove an IP Address
To remove a specific IP address from an interface, use the del subcommand. You must specify the exact IP address and subnet mask.
sudo ip addr del 192.168.1.100/24 dev eth0
Common Mistake: Forgetting the subnet mask (e.g., /24) when deleting an IP address can lead to errors, as the system needs to match the exact address entry.
3.3. Flush All IP Addresses from an Interface
To remove all IP addresses and associated configurations from an interface, use the flush subcommand. This is a drastic but sometimes necessary step for a clean slate.
sudo ip addr flush dev eth0
Warning: Use flush with extreme caution, especially on production systems, as it will immediately disrupt all network communication through that interface.
4. Configure Default Routes
The routing table dictates how network packets leave your system to reach their destinations. A default route (gateway) is crucial for accessing external networks, including the internet.
4.1. View Current Routing Table
Inspect the existing routing rules, including the default gateway.
ip r
Practical Tip: Look for a line starting with default via to identify your current gateway. The dev field indicates the interface used.
4.2. Add a Default Route
To establish a default gateway, specify the gateway’s IP address.
sudo ip r add default via 192.168.1.1 dev eth0
Pro-Tip: This command adds a temporary route. For persistent changes, you must configure your network manager (e.g., Netplan, NetworkManager, or traditional /etc/network/interfaces) or use a systemd-networkd configuration.
4.3. Delete a Default Route
Remove an existing default route. This is useful when reconfiguring network paths.
sudo ip r del default via 192.168.1.1 dev eth0
Common Mistake: Deleting the only default route on a server will isolate it from the internet and other networks, unless specific static routes are configured.
5. Manage ARP Cache Entries
The Address Resolution Protocol (ARP) maps IP addresses to MAC addresses. The ip neigh command (short for “neighbors”) manages the ARP cache.
5.1. View ARP Cache
Display the current ARP table, showing known IP-to-MAC mappings.
ip neigh show
Practical Tip: Entries marked REACHABLE are actively communicating, while STALE or DELAY indicate potential issues or aged entries.
5.2. Add a Static ARP Entry
Manually add a static ARP entry. This can be useful in specific security scenarios or for troubleshooting.
sudo ip neigh add 192.168.1.100 lladdr 00:11:22:33:44:55 dev eth0
Warning: Incorrect static ARP entries can lead to network communication failures or security vulnerabilities if used maliciously (ARP spoofing).
By consistently applying the ip command, you gain granular control over your Linux system’s networking. Remember that most changes made directly with ip are ephemeral; they will be lost upon reboot. For persistent configurations, integrate these commands into your distribution’s network configuration management system, such as Netplan on Ubuntu, NetworkManager, or systemd-networkd.
