ip Command ExamplesThe ip command in Linux, part of the iproute2 suite, is a modern and powerful tool for managing and configuring network interfaces, routes, and other network settings. It has largely replaced the deprecated ifconfig command in most Linux distributions. Below are 10 examples demonstrating various uses of the ip command with detailed explanations.
ip command is typically pre-installed on modern Linux distributions. Ensure you have the iproute2 package installed (e.g., sudo apt install iproute2 on Debian/Ubuntu or sudo yum install iproute on CentOS/RHEL).ip commands require root privileges. Use sudo where necessary, and exercise caution when modifying network settings to avoid connectivity issues.ip link show
Lists all network interfaces, their state (UP/DOWN), and basic configuration details like MAC addresses.
Sample Output:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 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 mode DEFAULT group default qlen 1000
link/ether 00:1a:2b:3c:4d:5e brd ff:ff:ff:ff:ff:ff
Explanation: Shows the loopback (lo) and Ethernet (eth0) interfaces, including their status and MAC addresses.
ip addr show
Displays IP addresses and detailed configuration for all network interfaces.
Sample 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:1a:2b:3c:4d:5e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
Explanation: Shows IP addresses (e.g., 192.168.1.100/24 for eth0) and other details like netmask and broadcast address.
ip addr show eth0
Displays configuration details, including IP addresses, for the specified interface (eth0).
Sample Output:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:1a:2b:3c:4d:5e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
Explanation: Limits output to eth0, useful for focusing on a single interface.
sudo ip link set eth0 up
Activates the eth0 interface, enabling network connectivity.
Sample Output: No output unless an error occurs. Verify with ip link show eth0, which shows state UP.
Explanation: Brings up a previously disabled interface, often used after configuration changes.
sudo ip link set eth0 down
Disables the eth0 interface, stopping network activity.
Sample Output: No output unless an error occurs. Verify with ip link show eth0, which shows state DOWN.
Explanation: Useful for troubleshooting or temporarily disabling an interface.
sudo ip addr add 192.168.1.150/24 dev eth0
Assigns the IP address 192.168.1.150 with a /24 subnet mask to the eth0 interface.
Sample Output: No output unless an error occurs. Verify with ip addr show eth0.
Explanation: Sets a static IP temporarily. For persistent changes, edit network configuration files (e.g., /etc/network/interfaces or use NetworkManager).
sudo ip addr del 192.168.1.150/24 dev eth0
Removes the specified IP address from the eth0 interface.
Sample Output: No output unless an error occurs. Verify with ip addr show eth0.
Explanation: Useful for correcting or reconfiguring IP assignments.
ip route show
Displays the system’s routing table, showing how network traffic is routed.
Sample Output:
default via 192.168.1.1 dev eth0 proto dhcp metric 100
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100 metric 100
Explanation: Shows the default gateway (192.168.1.1) and local network routes for eth0.
sudo ip route add 10.0.0.0/24 via 192.168.1.1 dev eth0
Adds a static route to the 10.0.0.0/24 network via the gateway 192.168.1.1 through eth0.
Sample Output: No output unless an error occurs. Verify with ip route show.
Explanation: Directs traffic for the specified network to the gateway, useful for custom routing setups.
ip -s link show eth0
Shows detailed statistics for the eth0 interface, including RX/TX packets and errors.
Sample Output:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:1a:2b:3c:4d:5e brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
9876543 12345 0 0 0 0
TX: bytes packets errors dropped carrier collsns
1234567 6789 0 0 0 0
Explanation: Provides packet and byte counts, useful for monitoring network activity or diagnosing issues.
Additional Tips:
ip a for ip addr show or ip l for ip link show to save typing./etc/network/interfaces (Debian/Ubuntu) or /etc/sysconfig/network-scripts/ (CentOS/RHEL).ip with ping, traceroute, or tcpdump to diagnose network issues.ip subcommands like ip neighbor for ARP tables or ip maddr for multicast addresses.Note: Interface names (e.g., eth0, enp0s3, wlan0) vary by system. Check the man page (man ip) or ip help for additional subcommands and options specific to your system.