Centos7配置临时路由和永久路由

本文详细介绍如何在Linux系统中查看和配置网络设备,包括使用route-n和ipadd命令检查路由表和活动网卡,创建并应用静态路由配置,以及添加临时路由。通过具体步骤,读者可以掌握网络管理的基本技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先使用route -n查看路由表

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         1.1.1.1  0.0.0.0         UG    100    0        0 enp5s0f0
10.2.0.0        172.20.3.254    255.255.0.0     UG    0      0        0 enp5s0f1

然后查看工作的网卡:

ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: enp5s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether f4:ce:46:b0:3e:82 brd ff:ff:ff:ff:ff:ff
    inet 1.1.1.1/27 brd 139.220.242.63 scope global enp5s0f0
       valid_lft forever preferred_lft forever
    inet 1.1.1.31/32 scope global enp5s0f0
       valid_lft forever preferred_lft forever
3: enp5s0f1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether f4:ce:46:b0:3e:83 brd ff:ff:ff:ff:ff:ff
    inet 172.20.3.64/24 brd 172.20.3.255 scope global enp5s0f1
       valid_lft forever preferred_lft forever
    inet 172.20.3.66/32 scope global enp5s0f1
       valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:74:59:1c:3e brd ff:ff:ff:ff:ff:ff
    inet 10.110.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever

从上面发现处于活动的网卡名称为enp5s0f0,enp5s0f1;我们为其创建对应的路由配置文件。route-enp5s0f1,因为enp5s0f0规划是默认路由出去,故不创建

vim /etc/sysconfig/network-scripts/route-enp5s0f1
10.5.0.0/16 via 172.20.3.254  dev  enp5s0f1

保存退出

systemctl restart network

再次使用route -n查看

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         139.220.242.33  0.0.0.0         UG    100    0        0 enp5s0f0
10.2.0.0        172.20.3.254    255.255.0.0     UG    0      0        0 enp5s0f1
10.5.0.0        172.20.3.254    255.255.0.0     UG    0      0        0 enp5s0f1

路由表已经在里面了

添加临时路由
注意:临时路由重启网卡后路由会消失

添加一条道10.10.0.0/16的临时路由

route add -net 10.10.0.0/16 gw  172.20.3.254

route -n查看路由表:

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         139.220.242.33  0.0.0.0         UG    100    0        0 enp5s0f0
10.2.0.0        172.20.3.254    255.255.0.0     UG    0      0        0 enp5s0f1
10.5.0.0        172.20.3.254    255.255.0.0     UG    0      0        0 enp5s0f1
10.10.0.0       172.20.3.254    255.255.0.0     UG    100    0        0 enp5s0f1

路由表已经在里面了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值