linux默认路由配置详细教程(Linux route指定静态路由配置)

route

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

说明:

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

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

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

选项and参数:

Linux route指定静态路由配置

 

查看路由表:

[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

本文来自作者:计市经济,不代表小新网立场!

转载请注明:https://www.xiaoxinys.cn/74525.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。