When the gateway you want to set is not in the same network segment as the host ip address, using route add default gw xx.xxx.xx.xx is unsuccessful and will
return: SIOCADDRT: Network is unreachable
Solution:
route add -host 192.168.18.1 dev eth0
route add default gw 192.168.18.1 dev eth0
route -n
# 现在默认网关就已经配置好了
The disadvantage of this method is that it becomes invalid after restarting.
The permanent fixing method is as follows:
1. First open the directory
cd /etc/rc.d/init.d/
2. Find the network file in the directory
cat network | grep static-routes
Can you find the following content in the file? If not, add it.
# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done
fi
3. Open the directory /etc/sysconfig/ and check whether the static-routes file can be found in the directory. If not, create it and add the
following content.
any host 192.168.18.1 dev eth0
any net deault gw 192.168.18.1 dev eth0
It will take effect permanently after that
(the default gateway configured temporarily or permanently above needs to be replaced with your own)