首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux route指定静态路由配置

Linux route指定静态路由配置

作者头像
张琳兮
发布2018-09-10 11:38:18
7K0
发布2018-09-10 11:38:18
举报
文章被收录于专栏:首富手记首富手记

route

显示并设置Linux中静态路由表

说明:

         route命令用来显示并设置Linux内核中的网络路由表,route命令设置的路由主要是静态路由。实现两个不同子网之间的通信,需要一台连接两个网络的路由器,或者同事位于两个网络的网关来实现。

在Linux系统中设置路由通常是为解决一下问题:

1) 该Linux系统在一个局域网中,局域网有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为Linux机器的默认路由。需要注意的是,直接在命令行下执行route命令来添加路由,只是临时生效,当网卡或者机器重启之后,该路由条目就失效了。只有刚添加在/etc/rc.local中添加route命令来保证该路由设置永久有效。

选项and参数:

选项

解释英文

解释中文

-A

-c

operate on the kernel’s routing cache.

打印将Linux核心的路由缓存

-n

不执行DNS反向查找,直接显示数字形式的IP地址

-e

以netstat格式显示路由表

-net

the target is a network

到一个网络的路由表

-host

the target is a host.

到一个主机的路由表

参数

解释英文

解释中文

add

add a   new route.

增加指定的路由记录

del

delete   a route.

删除指定的路由记录

Target

母的网络或目的主机

gw

设置网关,必须可达

dev

路由记录所表示的网络接口

reject

关闭的路由

查看路由表:

[root@zsf ~]# route Kernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface12.1.1.0        *               255.255.255.0   U       0      0        0 eth0link-local      *               255.255.0.0     U       1002   0        0 eth0default         12.1.1.2        0.0.0.0         UG      0      0        0 eth0[root@zsf ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface12.1.1.0        0.0.0.0         255.255.255.0   U       0      0        0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U       1002   0        0 eth00.0.0.0         12.1.1.2        0.0.0.0         UG      0      0        0 eth0[root@zsf ~]# route -e Kernel IP routing tableDestination     Gateway         Genmask         Flags   MSS Window    irtt Iface12.1.1.0         *              255.255.255.0     U       0   0          0 eth0link-local        *              255.255.0.0     U           0 0          0 eth0default         12.1.1.2         0.0.0.0         UG        0 0          0 eth0

说明:

其中Flags为路由标志,编辑当前网络节点的状态

·U   up代表路由当前为启动状态

·H   host表示此网关为一个主机

·G   gateway此网关为一个路由器

·R   reinstate route使用动态路由重新初始化的路由

·D    dynamically,此路由是动态写入的

·M   modified是有路由守护程序或导向器修改

·! 此路由当前为关闭状态

插入一条到13.1.1.0/24这个网段的路由从eth0出去[root@zsf ~]# route add -net 13.1.1.0   netmask 255.255.255.0 dev eth0[root@zsf ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface12.1.1.0        *               255.255.255.0   U       0      0        0 eth013.1.1.0        *               255.255.255.0   U       0      0        0 eth0link-local      *               255.255.0.0     U       1002   0        0 eth0default         12.1.1.2        0.0.0.0         UG      0      0        0 eth0删除一条到13.1.1.0/24这个网段的路由从eth0出去的路由[root@zsf ~]# route del -net 13.1.1.0   netmask 255.255.255.0 dev eth0[root@zsf ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface12.1.1.0        *               255.255.255.0   U       0      0        0 eth0link-local      *                 255.255.0.0     U       1002   0        0 eth0default         12.1.1.2        0.0.0.0         UG      0      0        0 eth0[root@zsf ~]# route add -net 13.1.1.0 netmask   255.255.255.0 dev eth0SIOCADDRT: No route to host,因为设置的网关地址不可达所以报错[root@zsf ~]# route add default gw 12.1.1.13 #设置一个默认网关[root@zsf ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface12.1.1.0        *               255.255.255.0   U       0      0        0 eth013.1.1.0        -               255.255.255.0   !       0      -          0 -link-local      *               255.255.0.0     U       1002   0        0 eth0default         12.1.1.13       0.0.0.0         UG      0      0        0 eth0default         12.1.1.2        0.0.0.0         UG      0      0        0 eth0[root@zsf ~]# route add -net 0.0.0.0   netmask 0.0.0.0 gw 12.1.1.10 设置一个默认网关(删除的时候必须把add换成del删除)[root@zsf ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface12.1.1.0        *               255.255.255.0   U       0      0        0 eth0link-local      *               255.255.0.0     U       1002   0        0 eth0default         12.1.1.10       0.0.0.0         UG      0      0        0 eth0default         12.1.1.2        0.0.0.0         UG      0      0        0 eth0[root@zsf ~]# route del default gw   12.1.1.13 #删除一个默认网关[root@zsf ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface12.1.1.0        *               255.255.255.0   U       0      0        0 eth0link-local      *               255.255.0.0     U       1002   0        0 eth0default         12.1.1.2        0.0.0.0         UG      0      0        0 eth0#添加一条屏蔽的路由[root@zsf ~]# route add -net 13.1.1.0 netmask 255.255.255.0 dev eth0   reject [root@zsf ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface12.1.1.0        *               255.255.255.0   U       0      0        0 eth013.1.1.0        -               255.255.255.0   !       0      -        0 -link-local      *               255.255.0.0     U       1002   0        0 eth0default         12.1.1.2        0.0.0.0         UG      0      0        0 eth0#删除一条屏蔽的路由[root@zsf ~]# route del -net 13.1.1.0 netmask 255.255.255.0 dev eth0   reject [root@zsf ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface12.1.1.0        *               255.255.255.0   U       0      0        0 eth0link-local      *               255.255.0.0     U       1002   0        0 eth0default         12.1.1.2        0.0.0.0         UG      0      0        0 eth0

以上方法都是临时生效,想让静态路由永久生效我们把它写入到/etc/rc.local开机的时候会自动执行这个文件内的指令。

vim /etc/rc.local#增加一条到13.1.1.0/24这个网段下一跳为eth0接口route add -net 13.1.1.0 netmask   255.255.255.0 dev eth0#增加一条到13.1.1.0/24这个网段下一跳为13.1.1.254route add -net 13.1.1.0 netmask   255.255.255.0 gw 13.1.1.254##增加一条到13.1.1.0/24这个网段下一跳为eth0的屏蔽路由route add -net 13.1.1.0 netmask   255.255.255.0 dev eth0 reject#增加一个默认网关route add -net 0.0.0.0 netmask 0.0.0.0 gw   12.1.1.10route add default gw 12.1.1.13

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • route
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档