显示并设置 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 |
来源: http://www.bubuko.com/infodetail-2532602.html