一、centos7/ubuntu16.04添加临时静态路由:
第1种方法:
ip route add 192.168.1.0/24 via 192.168.2.1 dev eno1
第2种方法:
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.2.1 dev eno2
二、centos7添加永久静态路由:
1、切换到网卡目录
#cd /etc/sysconfig/network-scripts/
2、新建路由文件(文件名根据要添加路由的网卡为准,此处为eno2网卡)
#vim route-eno2
3、文件中添加路由条目
#192.168.1.0/24 via 192.168.2.1 dev eno2
4、退出保存文件并重启网络服务
#systemctl restart network
5、查看新增路由是否生效
#route -n
三、ubuntu22.04添加永久路由(从ubuntu17开始使用netplan管理网络)
1 找到网络配置文件
#cd /etc/netplan
2 编辑配置文件
#vim 00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
enp96s0f0:
addresses:
- 192.168.3.204/23
gateway4: 192.168.2.12 #默认路由
nameservers:
addresses: [8.8.8.8]
routes: #静态路由
- to: 10.10.0.0/16
via: 192.168.2.1
version: 2
3 使配置生效
#sudo netlpan apply
4 验证配置是否生效
#route -n 或ip route show
root@nvidia:/etc/netplan# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.2.12 0.0.0.0 UG 0 0 0 enp96s0f0
10.10.0.0 192.168.2.1 255.255.0.0 UG 0 0 0 enp96s0f0
四、windows添加静态路由
1、以管理员身份打开cmd窗口
2、route add -p 192.168.1.0 mask 255.255.255.0 192.168.2.1
3、route print查看