Linux ip Command Examples

The 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.

Note: The 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).
Warning: Many ip commands require root privileges. Use sudo where necessary, and exercise caution when modifying network settings to avoid connectivity issues.

Example 1: Display All Network Interfaces

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.

Example 2: Display IP Addresses for All Interfaces

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.

Example 3: Show Specific Interface Details

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.

Example 4: Enable a Network 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.

Example 5: Disable a Network Interface

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.

Warning: Disabling an active interface may disrupt network connectivity.

Example 6: Assign a Static IP Address

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).

Example 7: Remove an IP Address

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.

Warning: Removing an active IP address may disrupt connectivity.

Example 8: Display Routing Table

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.

Example 9: Add a Static Route

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.

Example 10: Display Network Statistics

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:

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.