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

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM