欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS8.3 Ubuntu20.04添加静态路由

程序员文章站 2022-07-14 19:58:19
...

添加临时静态网络路由(两种方法):

a. route add -net 172.16.0.0 netmask 255.255.0.0 gw 172.16.5.1
b. ip route add 172.16.0.0/16 via 172.16.5.1 dev ens192

删除临时静态路由(两种方法):

a. route del -net 172.16.0.0 netmask 255.255.0.0
b. ip route del 172.16.0.0/16

添加永久静态路由:

Ubuntu20.04:

  1. route -n 检查路由表
[email protected]:/etc/netplan# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.155.255.1    0.0.0.0         UG    100    0        0 ens160
0.0.0.0         172.18.105.1    0.0.0.0         UG    101    0        0 ens192
10.155.255.0    0.0.0.0         255.255.255.0   U     100    0        0 ens160
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens160
172.18.105.0    0.0.0.0         255.255.255.0   U     101    0        0 ens192
  1. 进入目录**/etc/netplan/**
[email protected]:~# cd /etc/netplan/
[email protected]:/etc/netplan# ls
00-installer-config.yaml
[email protected]:/etc/netplan#vi 00-installer-config.yaml
  1. 编辑yaml文件
    在需要的接口(例如ens192)上添加 routes信息,注意对齐
# This is the network config written by 'subiquity'
network:
 ethernets:
   ens160:
     dhcp4: false
     addresses: [10.155.255.108/24]
     optional: true
     gateway4: 10.155.255.1
     nameservers:
       addresses: [8.8.8.8]
   ens192:
     dhcp4: false
     addresses: [172.18.105.108/24]
     optional: true
     gateway4: 172.18.105.1
     nameservers:
       addresses: [8.8.8.8]
     routes:
             - to: 172.18.0.0/16
               via: 172.18.105.1
 version: 2
 renderer: NetworkManager
  1. 让netplan生效
[email protected]:/etc/netplan# netplan apply
[email protected]:/etc/netplan#              
  1. 重新查看路由表,查看172.8.0.0/16路由信息
[email protected]:/etc/netplan# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.155.255.1    0.0.0.0         UG    100    0        0 ens160
0.0.0.0         172.18.105.1    0.0.0.0         UG    101    0        0 ens192
10.155.255.0    0.0.0.0         255.255.255.0   U     100    0        0 ens160
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens160
172.18.0.0      172.18.105.1    255.255.0.0     UG    101    0        0 ens192
172.18.105.0    0.0.0.0         255.255.255.0   U     101    0        0 ens192
[email protected]:/etc/netplan#                                                     

CentOS8

  1. ip route show | column -t查看路由表
[[email protected] network-scripts]# ip route show | column -t
default           via  10.155.255.1  dev    ens192  proto  static  metric  102
default           via  172.18.105.1  dev    ens224  proto  static  metric  103
10.155.255.0/24   dev  ens192        proto  kernel  scope  link    src     10.155.255.109  metric    102
10.155.255.0/24   via  10.155.255.1  dev    ens192  proto  static  metric  102
172.18.105.0/24   dev  ens224        proto  kernel  scope  link    src     172.18.105.109  metric    103
192.168.122.0/24  dev  virbr0        proto  kernel  scope  link    src     192.168.122.1   linkdown
[[email protected] network-scripts]#  
  1. 进入**/etc/sysconfig/network-scripts/**目录
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# ls
ifcfg-ens192  ifcfg-ens224  route-ens192  route-ens224
[[email protected] network-scripts]# vi route-ens224
  1. 选择要添加路由的网卡路由文件(例如route-ens224)添加如下路由信息,然后保存退出
ADDRESS0=172.18.0.0
NETMASK0=255.255.0.0
GATEWAY0=172.18.105.1
  1. 断开及重联interface
[[email protected] network-scripts]# nmcli device disconnect ens224 && nmcli device connect ens224
Device 'ens224' successfully disconnected.
Device 'ens224' successfully activated with '864417a3-87de-4bf0-a731-fb7f9bfae086'.
[[email protected] network-scripts]# 
  1. 重新查看路由表,查看172.18.0.0/16路由信息
[[email protected] network-scripts]# ip route show | column -t
default           via  10.155.255.1  dev    ens192  proto  static  metric  102
default           via  172.18.105.1  dev    ens224  proto  static  metric  103
10.155.255.0/24   dev  ens192        proto  kernel  scope  link    src     10.155.255.109  metric    102
10.155.255.0/24   via  10.155.255.1  dev    ens192  proto  static  metric  102
172.18.0.0/16     via  172.18.105.1  dev    ens224  proto  static  metric  103
172.18.105.0/24   dev  ens224        proto  kernel  scope  link    src     172.18.105.109  metric    103
192.168.122.0/24  dev  virbr0        proto  kernel  scope  link    src     192.168.122.1   linkdown
[[email protected] network-scripts]#