Linux系统添加永久静态路由的方法

一、使用route命令添加

使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:
A、添加到主机的路由

# route add –host 192.168.1.10 dev eth0
# route add –host 192.168.1.10 gw 192.168.1.1
# route add -host 192.168.1.3 gw 172.16.0.1 dev eth0  # 其中dev eth0可以省略

B、添加到网络的路由

# route add –net 192.168.1.0/24 gw 192.168.1.1
# route add –net 192.168.1.0/24 dev eth1
# route  del  -net 192.168.32.7/32  gw 172.18.0.200  # 删除路由

C、添加默认路由

# route add default gw 192.168.1.1

D、删除路由

# route del –host 192.168.1.10 dev eth0 
# route del default gw 192.168.1.1
# route  del  -net 192.168.32.7/32  gw 172.18.0.200  # 删除路由

E、查看添加的所有路由信息

[root@centos-7 network-scripts]# route -n  # 第一种查看路由表方法
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.7.2     0.0.0.0         UG    0      0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.0.0.1        192.168.7.2     255.255.255.255 UGH   0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.7.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0


[root@centos-7 network-scripts]# ip route  # 第二种查看路由表方法
default via 192.168.7.2 dev eth0 
10.0.0.0/24 dev eth0 scope link 
10.0.0.1 via 192.168.7.2 dev eth0 
169.254.0.0/16 dev eth0 scope link metric 1002 
192.168.7.0/24 dev eth0 proto kernel scope link src 192.168.7.100 

二、使用ip route 添加路由

A、添加路由

# ip route add 2.2.2.0/24  via  1.1.1.1  # 前面是路由,后面是网关地址
# ip  a  a  1.1.1.1/24 dev eth1    # 添加一个IP地址
# ip route add 192.168.0.0/24 via 172.16.0.1  # 添加路由

B、查看路由信息

[root@centos-7 network-scripts]# ip route   # 此方法查看到的路由信息方便存在配置文件中,或者用命令也可以直接复制粘贴,方便你我他。
default via 192.168.7.2 dev eth0  
10.0.0.0/24 dev eth0 scope link 
10.0.0.1 via 192.168.7.2 dev eth0 
169.254.0.0/16 dev eth0 scope link metric 1002 
192.168.7.0/24 dev eth0 proto kernel scope link src 192.168.7.100 

三、将路由永久写到配置文件中,重启网卡永久生效

A、在/etc/sysconfig/network-scripts目录下创建一个route-ethx的网卡名称,添加以下对应要添加的内容即可

[root@centos-7 network-scripts]# cat route-eth0 
10.0.0.1 via 192.168.7.2
10.0.0.0/24 dev eth0

B、查看此时配置的路由信息

[root@centos-7 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.7.2     0.0.0.0         UG    0      0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.0.0.1        192.168.7.2     255.255.255.255 UGH   0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.7.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

四、网络排错思路

网络排错检查思路:

1、首先检查配置文件是否正确,正确的话

2、检查路由,路由没有问题,

3、就ping网关,网关通,说明本机到网关是没有问题

4、然后你在ping一个外网地址,比如114.114.114.114,如果也是通的,

5、那么你再ping域名,如www.baidu.com 如果ping外网ip通,域名不通,说明DNS没有解析到域名,这样可以定位到DNS问题。

 

 

 

 

多个路由器之间的互通实验,详见此链接:https://www.cnblogs.com/struggle-1216/p/12730939.html

  

  

 

posted @ 2020-07-11 18:32  一叶知秋~~  阅读(23805)  评论(0编辑  收藏  举报