Centos7新增靜態路由

2021-07-08 18:13:37 字數 2857 閱讀 1791

**

本文摘取自 centos7系統配置上的變化(二)網路管理基礎

一、ip route顯示和設定路由

1、顯示路由表

[root@centos7 ~]# ip route show

default via 192.168.150.254 dev enp0s3 proto static metric 1024

192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110

太難看了,格式化一下(顯示的是預設閘道器和區域網路由,兩行的內容沒有共通性):

[root@centos7 tmp]# ip route show|column -t

default via 192.168.150.254 dev enp0s3 proto static metric 1024

192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110

2、新增靜態路由

[root@centos7 ~]# ip route add 10.15.150.0/24 via 192.168.150.253dev enp0s3 [root@centos7 ~]# [root@centos7 ~]# ip route show|column -t default via 192.168.150.254 dev enp0s3 proto static metric 1024 10.15.150.0/24 via 192.168.150.253 dev enp0s3 proto static metric 1 192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110[root@centos7 ~]# [root@centos7 ~]# ping 10.15.150.1ping 10.15.150.1 (10.15.150.1) 56(84) bytes of data. 64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.77ms 64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.08ms 64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.57ms ^c

3、刪除靜態路由

只需要把 add 替換成 del,或者更簡單的只寫目標網路

[root@centos7 ~]# ip route del 10.15.150.0/24

二、設定永久的靜態路由

1、新增永久靜態路由

ip route 指令對路由的修改不能儲存,重啟就沒了。把 ip route 指令寫到 /etc/rc.local 也是徒勞的。

rhel7官網文件沒有提到 /etc/sysconfig/static-routes,經測試此檔案已經無效;

/etc/sysconfig/network 配置檔案僅僅可以提供全域性預設閘道器,語法同 centos6 一樣: gateway= ;

永久靜態路由需要寫到 /etc/sysconfig/network-scripts/route-inte***ce 檔案中,比如新增兩條靜態路由:

[root@centos7 ~]# vi /etc/sysconfig/network-scripts/route-enp0s3

10.15.150.0/24via 192.168.150.253 dev enp0s3

10.25.250.0/24via 192.168.150.253 dev enp0s3

重啟計算機,或者重新啟用裝置enp0s3才能生效。

[root@centos7 ~]# nmcli dev connect enp0s3

一般直接連線一次裝置即可,如果不成功就先斷開裝置再連線裝置,注意必須兩個指令一起執行,否則,,,,,,你曉得。

[root@centos7 ~]# nmcli dev disconnect enp0s3 && nmcli dev connect enp0s3
2、清除永久靜態路由

可以刪除 ifcfg-enp0s3檔案或者注釋掉檔案裡的相應靜態路由條目,重啟計算機。

實驗的過程中出現兩個奇怪的現象:

1)有時候路由生效了但是在 ip route show 卻沒有顯示,重啟計算機後是肯定顯示的,原因暫時不明。

2)存在多個網絡卡時,預設路由似乎是隨機經由某個網絡卡裝置。檢查了所有連線配置檔案後發現,第一網絡卡的預設連線配置檔案 ifcfg-eth0 設定了gateway0(此設定會覆蓋/etc/sysconfig/network 定義的全域性預設閘道器),第二網絡卡的連線配置檔案 ifcfg-eth1 使用的是dhcp,會在啟動時也分配預設閘道器,兩個預設閘道器讓計算機糊塗了。這是在測試系統裡經常發生的現象,生產系統一般不會讓網絡卡用dhcp,或者即使是用了也會仔細分配預設閘道器防止衝突。

其他需要注意的:

1)連線配置檔案 ifcfg-* 裡可以設定多個gateway,一般第乙個是 gateway0,然後gateway1, gateway2... ,尾號最大的有效;

2)如果必須在/etc/sysconfig/network 檔案定義全域性閘道器,連線配置檔案 ifcfg-* 就不要設定gateway了,dhcp的連線要注意dhcp伺服器不要定義預設閘道器。

3)ifcfg-enp0s3 檔案改名為 ifcfg-eth0 後,route-enp0s3 檔案也要改名為 route-eth0。

還可以參考下面的文件:

分類: 

linux

標籤: 

centos7, 

靜態路由

Centos7新增靜態路由

一 ip route show 路由顯示和設定 1.路由顯示 ip route show 2.新增靜態路由 ip route add10.0.0.0 8 via 10.1 dev ens160 3.刪除靜態路由 ip route del 10.0.0.0 8 二 設定永久靜態路由 1.ip rout...

Centos7新增靜態路由

一 ip route顯示和設定路由 1 顯示路由表 root centos7 ip route show default via 192.168.150.254 dev enp0s3 proto static metric 1024 192.168.150.0 24 dev enp0s3 proto...

CentOS7新增永久靜態路由

centos7永久靜態路由需要寫到 etc sysconfig network scripts route 檔案中,故在 etc sysconfig network scripts 下,新建檔名為route 的檔案,代表網絡卡名 比如新增兩條靜態路由 網絡卡的裝置名叫eth0 root centos...