静态路由实例

Windows、Linux、Cisco和VMware等添加静态路由的方法

Posted by sysin on 2020-03-20
Estimated Reading Time 9 Minutes
Words 1.6k In Total
Viewed Times

作者:gc(at)sysin.org,主页:www.sysin.org

Windows

1
2
3
4
5
route add -p 10.0.0.0 mask 255.0.0.0 10.10.16.1

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 IOS、IOS-XE

1
2
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

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

Linux(通用,临时)

1
route add -net 10.10.11.0/20 gw 10.10.12.1

CentOS(永久)

推荐方式:

1
2
3
4
5
6
7
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

1
2
3
4
5
6
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

验证

1
ip route

Ubuntu(永久)

16.04

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#添加
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

1
2
3
4
5
6
7
8
9
10
11
12
13
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//注释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的值越小,优先级越高。

完整配置示例(>覆盖)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
  • 多网关
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
1
netplan apply

补充:yaml 基础

1
2
3
4
大小写敏感
使用缩进表示层级关系
缩进时不允许使用Tab键,只允许使用空格
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可

缩进建议使用2个空格,使用短横线“-”表示列表时,”- “后面的条目需要对齐,如果使用超过2个空格缩进,格式将有误。

VMware ESXi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#查看路由
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
1
2
3
4
5
6
7
8
9
10
11
12
13
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账号登录)

1
2
3
4
5
6
7
8
9
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账号登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#添加
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 #可选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

重启网络

1
systemctl restart systemd-networkd

验证

1
ip route

VMware Cloud Director Availability 4.0

注意:手动修改网络配置后,WebUI中显示错误无法直接配置。

基于 Photon OS,但是网卡名称不一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

重启网络

1
systemctl restart systemd-networkd



如果文章中使用的内容和图片侵犯了您的版权,请联系作者删除。如果您喜欢这篇文章或者觉得它对您有用,欢迎您发表评论,也欢迎您分享这个网站,或者赞赏一下作者,谢谢!


支付宝打赏 微信打赏

赞赏一下