主流系统和产品添加静态路由的方法


本文描述主流系统和产品添加静态路由的方法,一些具备 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