主流系統和產品添加靜態路由的方法


本文描述主流系統和產品添加靜態路由的方法,一些具備 WEB 管理界面的產品不在討論范圍,比如防火牆、路由器等多數產品具備直觀的操作界面。

macOS

1、添加路由命令(臨時)

與 Linux 類似,但是網關沒有 gw 參數(同 FreeBSD)

# 查看當前路由表
netstat -rn

# 獲取默認路由
route get 0.0.0.0

# 刪除默認路由
sudo route -n delete default 10.2.0.1

# 添加默認路由
sudo route add -net 0.0.0.0 10.2.0.1

# 添加靜態路由
sudo route add -net 10.16.0.0 10.18.18.10
sudo route add -net 10.16.0.0/16 10.18.18.10
sudo route -n add -net 192.168.2.0 -netmask 255.255.255.0 192.168.5.254

 

 

2、使用 networksetup 命令設置永久靜態路由

可以適用於 macOS Big Sur。

macOS 提供了一個名為 networksetup 的命令行界面,它允許您進行各種網絡配置。

可以通過 networksetup –help 查看具體的幫助。其實它就是 “系統偏好設置” 中網絡設置工具的命令行版本,但是功能更為強大一些。

使用 networksetup 命令添加永久靜態路由,如下:

# 語法
networksetup -getadditionalroutes <networkservice>
networksetup -setadditionalroutes <networkservice> [ <dest> <mask> <gateway> ]

# 查看 <networkservice>
networksetup -listallnetworkservices
# 這里顯示如下
Wi-Fi
iPhone USB
Bluetooth PAN
Thunderbolt Bridge

# 添加靜態路由
networksetup -setadditionalroutes "Wi-Fi" 10.18.1.0 255.255.255.0 192.168.1.1
networksetup -setadditionalroutes "Wi-Fi" 10.16.0.0 255.255.0.0 192.168.1.1

 

 

 

解釋:

  • “Wi-Fi” 指定路由走哪個設備(使用命令 networksetup -listallnetworkservices 查看當前的設備)
  • 10.18.1.0/24 和 10.16.0.0 都指向 192.169.1.1

驗證:

使用 netstat -nr 查看路由表。

清空路由:

networksetup -setadditionalroutes Wi-Fi


再次用 netstat -rn 查看路由可以看到添加的路由沒有了。

FreeBSD

臨時:

route add -net 10.10.1.0/24 10.10.1.1

 

與 Linux 類似,但是網關沒有 gw 參數(同 macOS)

永久:

vi /etc/rc.conf

 

Set default router IP to 60.1.2.3:

defaultrouter="60.1.2.3"

 

Create static routing for lan network 192.168.1.0/24, append following two lines:

static_routes="lan"
route_lan="-net 192.168.1.0/24 192.168.1.254"

 

 

How do I add multiple static routes?

      network                             router IP
lan (192.168.1.0/24)                192.168.1.254
mumoffice (10.0.0.0/8)        10.30.110.5
foo 169.254.1.1                        via loopback (lo0)

 

Add following to /etc/rc.conf

static_routes="lan mumoffice foo"
route_lan="-net 192.168.1.0/24 192.168.1.254"
route_mumoffice="-net 10.0.0.0/8 10.30.110.5"
route_foo="-host 169.254.1.1 -iface lo0"

 

Windows

route add -p 10.0.0.0 mask 255.0.0.0 10.10.16.1
// -p 參數永久添加,不用 -p 為臨時生效
route add -p 10.10.11.0 mask 255.255.255.0 10.10.12.1
route add -p 10.10.13.0 mask 255.255.255.0 10.10.12.1
route add -p 10.10.14.0 mask 255.255.255.0 10.10.12.1

Cisco

Cisco IOS、IOS-XE

ip route 10.0.0.0 255.0.0.0 10.10.200.2254
ip route 10.10.11.0 255.255.255.0 10.10.12.1

 

Cisco NX-OS

N7K(config)#vrf context management
N7K(config-vrf)# ip route 0.0.0.0/0 <下一跳 IP>

 

Linux

Linux(通用,臨時)

route add -net 10.10.11.0/24 gw 10.10.1.1

 

CentOS(永久)

推薦方式:

echo '
10.10.12.0/24 via 10.10.15.1
10.10.13.0/24 via 10.10.15.1
10.10.14.0/24 via 10.10.15.1
10.10.15.0/24 via 10.10.15.1
10.10.16.0/24 via 10.10.15.1
' > /etc/sysconfig/network-scripts/route-eth0

 

另外一種方法:使用 network.service(CentOS7 默認,CentOS8 需要 yum install network-scripts

 
echo '
any net 10.10.13.0/24 gw 10.10.15.1
any net 10.10.14.0/24 gw 10.10.15.1
any net 10.10.15.0/24 gw 10.10.15.1
any net 10.10.16.0/24 gw 10.10.15.1
' > /etc/sysconfig/static-routes

 

驗證

ip route

 

Debian(永久)

Debian 11:

#添加
cat >> /etc/network/interfaces <<EOF
# static routes
up ip route add 10.10.12.0/24 via 10.10.1.1 dev eth0
up ip route add 10.10.13.0/24 via 10.10.1.1 dev eth0
up ip route add 10.10.14.0/24 via 10.10.1.1 dev eth0
up ip route add 10.10.15.0/24 via 10.10.1.1 dev eth0
up ip route add 10.10.16.0/24 via 10.10.1.1 dev eth0
EOF

#重啟網絡
systemctl restart networking

#驗證
ip route

 

Ubuntu 16.04(永久)

16.04

#添加
cat >> /etc/network/interfaces <<EOF
# static routes
up route add -net 10.10.12.0/24 gw 10.10.15.1 dev eth0
up route add -net 10.10.13.0/24 gw 10.10.15.1 dev eth0
up route add -net 10.10.14.0/24 gw 10.10.15.1 dev eth0
up route add -net 10.10.15.0/24 gw 10.10.15.1 dev eth0
up route add -net 10.10.16.0/24 gw 10.10.15.1 dev eth0
EOF

#重啟網絡
service networking restart

#驗證
ip route

 

NetPlan(永久)

Ubuntu 18.04、20.04 及以上

18.04: /etc/netplan/50-cloud-init.yaml

20.04:/etc/netplan/00-installer-config.yaml

network:
  version: 2
  ethernets:
    eth0:
      addresses:
      - 10.10.15.5/24
      gateway4: 10.10.15.1
      nameservers:
        addresses:
        - 10.10.15.11
        - 10.10.15.12
        search:
        - sysin.org

 

 
# 注釋 gateway4
sed -i 's/gateway4.*/#&/' /etc/netplan/00-installer-config.yaml
# 追加靜態路由
cat >> /etc/netplan/00-installer-config.yaml <<EOF
      routes:
        - to: 0.0.0.0/0
          via: 10.10.15.254
          metric: 100
        - to: 10.10.12.0/24
          via: 10.10.15.1
          metric: 100
        - to: 10.10.13.0/24
          via: 10.10.15.1
          metric: 100
        - to: 10.10.14.0/24
          via: 10.10.15.1
          metric: 100
        - to: 10.10.15.0/24
          via: 10.10.15.1
          metric: 100
        - to: 10.10.16.0/24
          via: 10.10.15.1
          metric: 100
EOF

 

metric:為路由指定所需躍點數的整數值(范圍是 1 ~ 9999),Metric 的值越小,優先級越高。

完整配置示例(> 覆蓋)

cat > /etc/netplan/00-installer-config.yaml <<EOF
network:
  version: 2
  ethernets:
    eth0:
      addresses:
        - 10.10.15.57/24
      #gateway4: 10.10.15.254
      nameservers:
        addresses:
          - 10.10.15.11
          - 10.10.15.12
        search:
          - sysin.org
      routes:
        - to: 0.0.0.0/0
          via: 10.10.15.254
          metric: 100
        - to: 10.10.12.0/24
          via: 10.10.15.1
          metric: 100
        - to: 10.10.13.0/24
          via: 10.10.15.1
          metric: 100
        - to: 10.10.14.0/24
          via: 10.10.15.1
          metric: 100
        - to: 10.10.15.0/24
          via: 10.10.15.1
          metric: 100
        - to: 10.10.16.0/24
          via: 10.10.15.1
          metric: 100
EOF

 

  • 多網關
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 9.0.0.9/24
        - 10.0.0.10/24
        - 11.0.0.11/24
      #gateway4:  # unset, since we configure routes below
      routes:
        - to: 0.0.0.0/0
          via: 9.0.0.1
          metric: 100
        - to: 0.0.0.0/0
          via: 10.0.0.1
          metric: 100
        - to: 0.0.0.0/0
          via: 11.0.0.1
          metric: 100

 

 
netplan apply

 

補充:yaml 基礎

大小寫敏感
使用縮進表示層級關系
縮進時不允許使用 Tab 鍵,只允許使用空格
縮進的空格數目不重要,只要相同層級的元素左側對齊即可

 

縮進建議使用 2 個空格,使用短橫線 “-” 表示列表時,”- “后面的條目需要對齊,如果使用超過 2 個空格縮進,格式將有誤。

VMware

VMware ESXi

 
#查看路由
esxcfg-route -l
#添加
esxcli network ip route ipv4 add --gateway 10.10.15.1 --network 10.10.12.0/24
esxcli network ip route ipv4 add --gateway 10.10.15.1 --network 10.10.13.0/24
esxcli network ip route ipv4 add --gateway 10.10.15.1 --network 10.10.14.0/24
esxcli network ip route ipv4 add --gateway 10.10.15.1 --network 10.10.15.0/24
esxcli network ip route ipv4 add --gateway 10.10.15.1 --network 10.10.16.0/24
#刪除默認路由
esxcli network ip route ipv4 remove -n 0.0.0.0/0 -g 10.10.15.1
#恢復默認路由
esxcli network ip route ipv4 add --gateway 10.10.15.254 --network 0.0.0.0/0
#查看路由
esxcfg-route -l

 

esxcfg-route -l

esxcli network ip route ipv4 add --gateway 10.10.14.1 --network 10.10.12.0/24
esxcli network ip route ipv4 add --gateway 10.10.14.1 --network 10.10.13.0/24
esxcli network ip route ipv4 add --gateway 10.10.14.1 --network 10.10.14.0/24
esxcli network ip route ipv4 add --gateway 10.10.14.1 --network 10.10.15.0/24
esxcli network ip route ipv4 add --gateway 10.10.14.1 --network 10.10.16.0/24

esxcli network ip route ipv4 remove -n 0.0.0.0/0 -g 10.10.14.1

esxcli network ip route ipv4 add --gateway 10.10.14.254 --network 0.0.0.0/0

esxcfg-route -l

 

NSX-T

基於 Ubuntu,但不可手動編輯,以下為 nsxcli (使用 admin 賬號登錄)

set route prefix 0.0.0.0/0 gateway 10.10.15.254 interface eth0

set route prefix 10.10.12.0/24 gateway 10.10.15.1 interface eth0
set route prefix 10.10.13.0/24 gateway 10.10.15.1 interface eth0
set route prefix 10.10.14.0/24 gateway 10.10.15.1 interface eth0
set route prefix 10.10.15.0/24 gateway 10.10.15.1 interface eth0
set route prefix 10.10.16.0/24 gateway 10.10.15.1 interface eth0

get route

 

VMware vRealize Network Insight

基於 Ubunt 16.04,與 Ubuntu 相同

使用 support 賬號登錄

#添加
cat >> /etc/network/interfaces <<EOF
# static routes
up route add -net 10.10.12.0/24 gw 10.10.15.1 dev eth0
up route add -net 10.10.13.0/24 gw 10.10.15.1 dev eth0
up route add -net 10.10.14.0/24 gw 10.10.15.1 dev eth0
up route add -net 10.10.15.0/24 gw 10.10.15.1 dev eth0
up route add -net 10.10.16.0/24 gw 10.10.15.1 dev eth0
EOF

#重啟網絡
service networking restart

#驗證
ip route

 

VMware Photon OS

vCenter Server 6.x/7.0

用於 vRealize 8.x 系列產品(3.0),vSphere_Replication(2.0),SRM(2.0)

編輯 /etc/systemd/network/10-eth0.network

添加如下:

[Route]
Destination=10.1.0.0/16
Gateway=10.5.0.1
GatewayOnlink=true #可選

 

cat >> /etc/systemd/network/10-eth0.network <<EOF

[Route]
Destination=10.10.12.0/24
Gateway=10.10.15.1
GatewayOnlink=true

[Route]
Destination=10.10.13.0/24
Gateway=10.10.15.1
GatewayOnlink=true

[Route]
Destination=10.10.14.0/24
Gateway=10.10.15.1
GatewayOnlink=true

[Route]
Destination=10.10.15.0/24
Gateway=10.10.15.1
GatewayOnlink=true

[Route]
Destination=10.10.16.0/24
Gateway=10.10.15.1
GatewayOnlink=true
EOF

 

重啟網絡

systemctl restart systemd-networkd


驗證

ip route

 

VMware Cloud Director Availability 4.0

注意:手動修改網絡配置后,WebUI 中顯示錯誤無法直接配置。

基於 Photon OS,但是網卡名稱不一樣。

cat >> /etc/systemd/network/ens160.network <<EOF

[Route]
Destination=10.10.12.0/24
Gateway=10.10.15.1
GatewayOnlink=true

[Route]
Destination=10.10.13.0/24
Gateway=10.10.15.1
GatewayOnlink=true

[Route]
Destination=10.10.14.0/24
Gateway=10.10.15.1
GatewayOnlink=true

[Route]
Destination=10.10.15.0/24
Gateway=10.10.15.1
GatewayOnlink=true

[Route]
Destination=10.10.16.0/24
Gateway=10.10.15.1
GatewayOnlink=true
EOF

 

重啟網絡

systemctl restart systemd-networkd

 




免責聲明!

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



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