route linux 详解

简介: route linux 详解

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

目录
相关文章
|
7月前
|
Linux
如何在 Linux 上添加路由?
如何在 Linux 上添加路由?
488 1
|
14天前
|
存储 Linux 网络架构
如何在Linux上添加路由?
【4月更文挑战第8天】
29 2
如何在Linux上添加路由?
|
4月前
|
Linux 网络架构
Linux - route命令
Linux - route命令
31 0
|
5月前
|
Linux
linux查看IP
linux查看IP
|
Linux 网络架构
Linux Command route 路由
Linux Command route 路由
Linux Command route 路由
|
网络协议 Linux 网络架构
Linux Command ip
Linux Command ip
|
Linux 网络架构 网络协议
|
网络协议 Linux 网络虚拟化
|
Linux 网络架构 索引