When working with network configuration in modern Linux distributions like CentOS, RHEL, or Ubuntu, you'll notice that legacy networking commands (ifconfig
, route
) are being replaced by the more powerful ip
command from the iproute2 package.
The main advantages of using ip route
include:
- More consistent syntax for all network operations
- Better integration with modern Linux networking features
- Availability in minimal installations where legacy tools might be missing
To set a default gateway using ip route
, here's the direct equivalent of the legacy route add default gw 192.168.1.254 eth0
command:
ip route add default via 192.168.1.254 dev eth0
After setting the default gateway, you can verify it with:
ip route show
# Or more specifically:
ip route show default
Example output might look like:
default via 192.168.1.254 dev eth0 proto static metric 100
Here are some common variations you might need:
1. Setting default gateway with metric:
ip route add default via 192.168.1.254 dev eth0 metric 100
2. Removing the default route:
ip route del default
3. Setting default gateway on a specific table:
ip route add default via 192.168.1.254 dev eth0 table 100
Remember that ip route
changes are temporary. For permanent configuration:
For RHEL/CentOS:
# Edit /etc/sysconfig/network-scripts/route-eth0
default via 192.168.1.254 dev eth0
For Ubuntu/Debian:
# Edit /etc/network/interfaces
post-up ip route add default via 192.168.1.254 dev eth0
If your default gateway isn't working:
- Verify interface is up:
ip link show eth0
- Check for conflicting routes:
ip route show table all
- Test connectivity to gateway:
ping 192.168.1.254
In modern Linux distributions like CentOS and RHEL, the ip
command from iproute2 package has largely replaced the legacy route
command. While both can configure network routing, ip
offers more features and is now the recommended tool.
The traditional route command for adding a default gateway:
route add default gw 192.168.1.254 eth0
The equivalent ip route command:
ip route add default via 192.168.1.254 dev eth0
The complete syntax breaks down as:
ip route add {NETWORK/MASK} via {GATEWAY_IP} dev {INTERFACE}
For default route (0.0.0.0/0), you can simplify to:
ip route add default via {GATEWAY_IP} dev {INTERFACE}
Setting default gateway on eth0:
ip route add default via 192.168.1.1 dev eth0
Adding a specific network route:
ip route add 10.0.0.0/24 via 192.168.1.254 dev eth1
To check active routes:
ip route show
Or more specifically for the default route:
ip route show default
For temporary changes (lost after reboot):
ip route add default via 192.168.1.254 dev eth0
For permanent changes on CentOS/RHEL:
# Edit /etc/sysconfig/network-scripts/route-eth0
default via 192.168.1.254 dev eth0
Or using nmcli (NetworkManager):
nmcli connection modify eth0 ipv4.gateway 192.168.1.254
Common issues and solutions:
# If you get "Network is unreachable"
ip link set eth0 up
ip addr add 192.168.1.100/24 dev eth0
ip route add default via 192.168.1.254
To delete a route:
ip route del default via 192.168.1.254