IP and Routing
The ip command is the core of the iproute2 toolkit and the standard tool for managing network interfaces, IP addresses, and routes on modern Linux systems. This article covers how to use the ip command for network configuration on CentOS / AlmaLinux / Rocky Linux.
ip addr — Managing IP Addresses
Section titled “ip addr — Managing IP Addresses”Viewing IP Addresses for All Interfaces
Section titled “Viewing IP Addresses for All Interfaces”ip addr showShort form:
ip aViewing the IP Address of a Specific Interface
Section titled “Viewing the IP Address of a Specific Interface”ip addr show dev ens33Showing Only IPv4 Addresses
Section titled “Showing Only IPv4 Addresses”ip -4 addr showShowing Only IPv6 Addresses
Section titled “Showing Only IPv6 Addresses”ip -6 addr showTemporarily Adding an IP Address
Section titled “Temporarily Adding an IP Address”sudo ip addr add 192.168.1.200/24 dev ens33An interface can have multiple IP addresses bound to it:
sudo ip addr add 192.168.1.201/24 dev ens33Removing an IP Address
Section titled “Removing an IP Address”sudo ip addr del 192.168.1.200/24 dev ens33ip link — Managing Network Interfaces
Section titled “ip link — Managing Network Interfaces”Viewing the Status of All Interfaces
Section titled “Viewing the Status of All Interfaces”ip link showShort form:
ip lEnabling/Disabling an Interface
Section titled “Enabling/Disabling an Interface”sudo ip link set ens33 upsudo ip link set ens33 downChanging the MTU Value
Section titled “Changing the MTU Value”sudo ip link set ens33 mtu 9000Viewing Interface Statistics
Section titled “Viewing Interface Statistics”ip -s link show ens33ip route — Route Management
Section titled “ip route — Route Management”Viewing the Routing Table
Section titled “Viewing the Routing Table”ip route showShort form:
ip rExample output:
default via 192.168.1.1 dev ens33 proto dhcp metric 100192.168.1.0/24 dev ens33 proto kernel scope link src 192.168.1.100 metric 100default via 192.168.1.1— Default gateway192.168.1.0/24 dev ens33— Directly connected network
Viewing the Route to a Specific Destination
Section titled “Viewing the Route to a Specific Destination”ip route get 8.8.8.8Configuring the Default Gateway
Section titled “Configuring the Default Gateway”Temporarily Changing the Default Gateway
Section titled “Temporarily Changing the Default Gateway”sudo ip route del defaultsudo ip route add default via 192.168.1.254 dev ens33Permanently Setting the Default Gateway with nmcli
Section titled “Permanently Setting the Default Gateway with nmcli”sudo nmcli connection modify ens33 ipv4.gateway 192.168.1.1sudo nmcli connection up ens33Adding Static Routes
Section titled “Adding Static Routes”Temporarily Adding a Static Route
Section titled “Temporarily Adding a Static Route”sudo ip route add 10.10.0.0/16 via 192.168.1.254 dev ens33sudo ip route add 172.16.5.10/32 via 192.168.1.254Removing a Static Route
Section titled “Removing a Static Route”sudo ip route del 10.10.0.0/16Permanently Adding Static Routes with nmcli
Section titled “Permanently Adding Static Routes with nmcli”Temporary routes are lost after a reboot. Use nmcli to make them persistent:
sudo nmcli connection modify ens33 +ipv4.routes "10.10.0.0/16 192.168.1.254"Adding multiple routes:
sudo nmcli connection modify ens33 \ +ipv4.routes "10.10.0.0/16 192.168.1.254" \ +ipv4.routes "172.16.0.0/12 192.168.1.253"Apply the configuration:
sudo nmcli connection up ens33Viewing Permanently Configured Routes
Section titled “Viewing Permanently Configured Routes”nmcli connection show ens33 | grep ipv4.routesRemoving a Permanent Static Route
Section titled “Removing a Permanent Static Route”sudo nmcli connection modify ens33 -ipv4.routes "10.10.0.0/16 192.168.1.254"sudo nmcli connection up ens33ip Command vs. ifconfig Comparison
Section titled “ip Command vs. ifconfig Comparison”ifconfig belongs to the net-tools package and is no longer installed by default on newer EL systems. The ip command is more powerful and is the recommended replacement.
| Purpose | ifconfig (legacy) | ip (modern) |
|---|---|---|
| View all interfaces | ifconfig | ip addr show |
| View a specific interface | ifconfig ens33 | ip addr show dev ens33 |
| Add an IP address | ifconfig ens33 192.168.1.100 netmask 255.255.255.0 | ip addr add 192.168.1.100/24 dev ens33 |
| Bring up an interface | ifconfig ens33 up | ip link set ens33 up |
| Bring down an interface | ifconfig ens33 down | ip link set ens33 down |
| View the routing table | route -n | ip route show |
| Add a default gateway | route add default gw 192.168.1.1 | ip route add default via 192.168.1.1 |
| View interface statistics | ifconfig ens33 | ip -s link show ens33 |
If you still need ifconfig, you can install net-tools:
sudo dnf install net-toolsPractical Tips
Section titled “Practical Tips”Output in JSON Format (convenient for script parsing)
Section titled “Output in JSON Format (convenient for script parsing)”ip -j addr show ens33Combined with jq for formatted output:
ip -j addr show ens33 | jq .Brief Output Format
Section titled “Brief Output Format”ip -br addr showip -br link showMonitoring Network Changes
Section titled “Monitoring Network Changes”ip monitor routeip monitor allViewing the ARP Neighbor Table
Section titled “Viewing the ARP Neighbor Table”ip neighbour show