CentOS7添加明细路由

例如添加一条到192.168.3.0网段的路由,从网卡eth0出去,网关是192.168.2.12

注意:

在CentOS中通过直接修改网卡配置文件/etc/sysconfig/network-scripts/ifcfg-eth0的方法不生效

命令行直接添加路由

sudo ip route add 192.168.3.0/24 via 192.168.2.12 dev eth0

开机自动生效

打开网络配置文件 rc.local

sudo vi /etc/rc.d/rc.local
  1. 在文件中添加你的路由命令:
# 在文件末尾添加以下行
sudo ip route add 192.168.3.0/24 via 192.168.2.12 dev eth0
  1. 保存文件并退出编辑器。

  2. 授予 rc.local 可执行权限:

sudo chmod +x /etc/rc.d/rc.local
  1. 确保 rc.local 已经启用。执行以下命令:
sudo systemctl enable rc-local
  1. 最后,重启系统,这样在开机时 rc.local 中的路由命令就会自动执行:
这样,你的路由指令就会在开机时自动添加到路由表中,从而实现开机生效的效果。请注意,`rc.local`
文件中的命令将在每次系统启动时执行,所以请确保该命令没有副作用且需要在每次启动时都运行。

通过nmcli命令添加路由

sudo nmcli connection modify eth0 +ipv4.routes "192.168.3.0/24 192.168.2.12"

删除路由

sudo nmcli connection modify eth0 -ipv4.routes "192.168.3.0/24 192.168.2.12"

注意:

通过nmcli修改路由需重启操作系统,重启网络服务不生效

在使用sudo nmcli connection modify eth0 +ipv4.routes "192.168.3.0/24 192.168.2.12"这条命令时,并没有直接对应的配置文件。NetworkManager使用d-bus机制来管理网络配置,因此网络配置信息会保存在d-bus系统中,而不是像传统的配置文件那样存储在磁盘上。

当你运行上述命令时,它会直接更新NetworkManager中eth0这个连接的路由设置,并且这些设置在系统重启后会保留,因为它们被保存在NetworkManager的配置数据库中。

要查看eth0连接的详细配置,你可以使用以下命令:

sudo nmcli connection show eth0

这会显示eth0连接的详细信息,包括路由配置。

示例

[root@node01 ~]# nmcli con show eth0
connection.id:                          eth0
connection.uuid:                        406ef76f-d50d-4205-9f87-4046d0da9161
connection.stable-id:                   --
connection.interface-name:              eth0
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.timestamp:                   1691027313
connection.read-only:                   no
connection.permissions:                 
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.autoconnect-slaves:          -1 (default)
connection.secondaries:                 
connection.gateway-ping-timeout:        0
connection.metered:                     unknown
connection.lldp:                        -1 (default)
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          yes
802-3-ethernet.mac-address:             --
802-3-ethernet.cloned-mac-address:      --
802-3-ethernet.generate-mac-address-mask:--
802-3-ethernet.mac-address-blacklist:   
802-3-ethernet.mtu:                     auto
802-3-ethernet.s390-subchannels:        
802-3-ethernet.s390-nettype:            --
802-3-ethernet.s390-options:            
802-3-ethernet.wake-on-lan:             1 (default)
802-3-ethernet.wake-on-lan-password:    --
ipv4.method:                            manual
ipv4.dns:                               
ipv4.dns-search:                        
ipv4.dns-options:                       (default)
ipv4.dns-priority:                      0
ipv4.addresses:                         192.168.2.11/24
ipv4.gateway:                           192.168.2.12
ipv4.routes:                            
ipv4.route-metric:                      -1
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-timeout:                      0
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.dhcp-fqdn:                         --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
ipv4.dad-timeout:                       -1 (default)
ipv6.method:                            auto
ipv6.dns:                               
ipv6.dns-search:                        
ipv6.dns-options:                       (default)
ipv6.dns-priority:                      0
ipv6.addresses:                         
ipv6.gateway:                           --
ipv6.routes:                            
ipv6.route-metric:                      -1
ipv6.ignore-auto-routes:                no
ipv6.ignore-auto-dns:                   no
ipv6.never-default:                     no
ipv6.may-fail:                          yes
ipv6.ip6-privacy:                       -1 (unknown)
ipv6.addr-gen-mode:                     stable-privacy
ipv6.dhcp-send-hostname:                yes
ipv6.dhcp-hostname:                     --
ipv6.token:                             --
GENERAL.NAME:                           eth0
GENERAL.UUID:                           406ef76f-d50d-4205-9f87-4046d0da9161
GENERAL.DEVICES:                        eth0
GENERAL.STATE:                          activated
GENERAL.DEFAULT:                        yes
GENERAL.DEFAULT6:                       no
GENERAL.VPN:                            no
GENERAL.ZONE:                           --
GENERAL.DBUS-PATH:                      /org/freedesktop/NetworkManager/ActiveConnection/0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/Settings/3
GENERAL.SPEC-OBJECT:                    /
GENERAL.MASTER-PATH:                    --
IP4.ADDRESS[1]:                         192.168.2.11/24
IP4.GATEWAY:                            192.168.2.12
IP4.ROUTE[1]:                           dst = 192.168.3.0/24, nh = 192.168.2.12, mt = 100
IP6.ADDRESS[1]:                         fe80::54fc:ae6:27ee:f265/64
IP6.GATEWAY:                            
[root@node01 ~]# 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值