route linux 详解

本文详细介绍如何使用route命令在Linux系统中设置和查看静态路由表,包括添加、删除路由及设置默认网关的方法,并讲解如何使静态路由永久生效。

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

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 table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0

[root@zsf ~]# route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0

0.0.0.0 12.1.1.2 0.0.0.0 UG 0 0 0 eth0

[root@zsf ~]# route -e

Kernel IP routing table

Destination Gateway Genmask Flags MSS Window irtt Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 0 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

13.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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

SIOCADDRT: No route to host,因为设置的网关地址不可达所以报错

[root@zsf ~]# route add default gw 12.1.1.13 #设置一个默认网关

[root@zsf ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

13.1.1.0 - 255.255.255.0 ! 0 - 0 -

link-local * 255.255.0.0 U 1002 0 0 eth0

default 12.1.1.13 0.0.0.0 UG 0 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 12.1.1.10 0.0.0.0 UG 0 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

13.1.1.0 - 255.255.255.0 ! 0 - 0 -

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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.254

route 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.10

route add default gw 12.1.1.13route

显示并设置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 table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0

[root@zsf ~]# route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0

0.0.0.0 12.1.1.2 0.0.0.0 UG 0 0 0 eth0

[root@zsf ~]# route -e

Kernel IP routing table

Destination Gateway Genmask Flags MSS Window irtt Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 0 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

13.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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

SIOCADDRT: No route to host,因为设置的网关地址不可达所以报错

[root@zsf ~]# route add default gw 12.1.1.13 #设置一个默认网关

[root@zsf ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

13.1.1.0 - 255.255.255.0 ! 0 - 0 -

link-local * 255.255.0.0 U 1002 0 0 eth0

default 12.1.1.13 0.0.0.0 UG 0 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 12.1.1.10 0.0.0.0 UG 0 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

13.1.1.0 - 255.255.255.0 ! 0 - 0 -

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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 ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

12.1.1.0 * 255.255.255.0 U 0 0 0 eth0

link-local * 255.255.0.0 U 1002 0 0 eth0

default 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.254

route 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.10

route add default gw 12.1.1.13

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值