rhel7添加静态路由

1.使用route 命令添加

//添加到主机的路由

[root@localhost ~]# route add -host 8.8.8.8 dev ens3
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
8.8.8.8         0.0.0.0         255.255.255.255 UH    0      0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@localhost ~]# route add -host 8.8.8.8 gw 8.8.8.8
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
8.8.8.8         8.8.8.8         255.255.255.255 UGH   0      0        0 ens3
8.8.8.8         0.0.0.0         255.255.255.255 UH    0      0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

//添加到网络的路由 (why)

route add –net 11.11.1.11 netmask 255.255.255.0 dev eth0
route add –net 11.11.1.11 netmask 255.255.255.0 gw 11.11.1.1
route add –net 11.11.1.0/24 dev eth1

//添加默认网关

[root@localhost ~]# route add default gw 172.25.8.250
[root@localhost ~]# route add default gw 172.25.8.251
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
8.8.8.8         8.8.8.8         255.255.255.255 UGH   0      0        0 ens3
8.8.8.8         0.0.0.0         255.255.255.255 UH    0      0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

//删除路由

[root@localhost ~]# route del -host 8.8.8.8 dev ens3
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

//机器重启或者网卡重启后路由就失效了

[root@localhost ~]# systemctl restart network
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

2.使用ip命令来添加、删除路由

[root@localhost ~]# ip route add default via 172.25.8.251 dev ens3
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

[root@localhost ~]# ip route add 172.16.1.0/24 via 172.25.8.251 dev ens3
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
172.16.1.0      172.25.8.251    255.255.255.0   UG    0      0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

3.在linux下设置永久路由的方法

1)在/etc/sysconfig/network里添加到末尾,是全局生效的。(有效的方法)

GATEWAY=gw-ip或者GATEWAY=gw-dev

2)在/etc/sysconfig/network-script/route-interface下添加路由(每个接口一个文件,如果没有就创建一个,只能添加针对该接口的路由)
例如给eth0添加一个默认网关:

vim /etc/sysconfig/network-scripts/ifcfg-eth0

#添加如下内容(可以省略dev eth0)
GATEWAY=X.X.X.X

#保存退出后
service network restart或systemctl restart network。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值