频道栏目
首页 > 资讯 > 路由器 > 正文

rhel7添加静态路由方法讲解

18-02-05        来源:[db:作者]  
收藏   我要投稿

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。
相关TAG标签
上一篇:Linux安装JDK、Tomcat、MySQL基础软件的方法
下一篇:索尼开源旗下新机内核源码开发者订制
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站