前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux route文件,Linux route 命令使用详解

linux route文件,Linux route 命令使用详解

作者头像
全栈程序员站长
发布2022-09-09 11:27:45
3K0
发布2022-09-09 11:27:45
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

一:使用 route 命令添加

使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:

//添加到主机的路由# route add –host 192.168.1.11 dev eth0

# route add –host 192.168.1.12 gw 192.168.1.1

//添加到网络的路由# route add –net 192.168.1.11 netmask 255.255.255.0 eth0

# route add –net 192.168.1.11 netmask 255.255.255.0 gw 192.168.1.1

# route add –net 192.168.1.0/24 eth1

//添加默认网关# route add default gw 192.168.2.1

//删除路由# route del –host 192.168.1.11 dev eth0

二:在linux下设置永久路由的方法:

1.在/etc/rc.local里添加方法:

route add -net 192.168.3.0/24 dev eth0

2.在/etc/sysconfig/network里添加到末尾方法:

GATEWAY=gw-ip 或者 GATEWAY=gw-dev

3./etc/sysconfig/static-routes : (没有static-routes的话就手动建立一个这样的文件) any net 192.168.3.0/24 gw 192.168.3.254

any net 10.250.228.128 netmask 255.255.255.192 gw 10.250.228.129

如果在rc.local中添加路由会造成NFS无法自动挂载问题,所以使用static-routes的方法是最好的。无论重启系统和service network restart 都会生效。

按照linux启动的顺序,rc.local里面的内容是在linux所有服务都启动完毕,最后才被执行的,也就是说,这里面的内容是在netfs之后才被执行的,那也就是说在netfs启动的时候,服务器上的静态路由是没有被添加的,所以netfs挂载不能成功。static-routes文件又是什么呢,这个是network脚本执行时调用的一个文件,这个文件的放在/etc/sysconfig目录下,在network脚本中的位置是:

# Add non interface-specific static-routes.

if [ -f /etc/sysconfig/static-routes ]; then

grep “^any” /etc/sysconfig/static-routes | while read ignore args ; do

/sbin/route add -$args

done

fi

从这段脚本可以看到,这个就是添加静态路由的方法,static-routes的写法是:

any net 192.168.1.0/16 gw 网关ip

这样的话,在启动network脚本的时候路由就自动添加上了,又因为network是在netfs前面启动的,自然在挂载nfs的时候就正常了。这样看来,如果需要添加静态路由,使用static-routes文件要比使用rc.local好,而且当改变了网络配置,需要重启network脚本的时候,相应的静态路由是可以自动添加上的,但这时如果使用rc.local的话,在重启network服务的时候,原本添加好的静态路由就消失了。

4.开启 IP 转发: # echo “1” >/proc/sys/net/ipv4/ip_forward (临时)

# vi /etc/sysctl.conf –> net.ipv4.ip_forward=1 (永久开启)

三、route命令解释

用于显示和操作IP路由表。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是 为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为 Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。

格式:route

格式:/sbin/route

用于打印路由表(display the current routing table)。

在非root用户使用时需要使用完整路径执行route命令。

route命令输出的路由表字段含义如下: Destination 目标

The destination network or destination host. 目标网络或目标主机。

Gateway 网关

The gateway address or ‘*’ if none set. 网关地址,如果没有就显示星号。

Genmask 网络掩码

The netmask for the destination net; ‘255.255.255.255’ for a

host destination and ‘0.0.0.0’ for the default route.

Flags Possible flags include 标志,常用的是U和G。

U (route is up) 路由启用

H (target is a host) 目标是主机

G (use gateway) 使用网关

R (reinstate route for dynamic routing)

D (dynamically installed by daemon or redirect)

M (modified from routing daemon or redirect)

A (installed by addrconf)

C (cache entry)

! (reject route)

Metric 距离、跳数。暂无用。

The ‘distance’ to the target (usually counted in hops). It is

not used by recent kernels, but may be needed by routing dae-

mons.

Ref 不用管,恒为0。

Number of references to this route. (Not used in the Linux ker-nel.)

Use 该路由被使用的次数,可以粗略估计通向指定网络地址的网络流量。

Count of lookups for the route. Depending on the use of -F and

-C this will be either route cache misses (-F) or hits (-C).

Iface 接口,即eth0,eth0等网络接口名

Interface to which packets for this route will be sent.

格式:route -n

格式:/sbin/route -n

用于打印路由表,加上-n参数就是在输出的信息中不打印主机名而直接打印ip地址。

格式:route add default gw {IP-ADDRESS} {INTERFACE-NAME}用于设置默认路由,其中,

参数{IP-ADDRESS): 用于指定路由器(网关)的IP地址;

参数{INTERFACE-NAME}: 用于指定接口名称,如eth0。使用/sbin/ifconfig -a可以显示所有接口信息。

例:route add default gw mango

格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}添加到指定网络的路由规则,其中

参数{NETWORK-ADDRESS}: 用于指定网络地址

参数{NETMASK}: 用于指定子网掩码

参数{INTERFACE-NAME}: 用于指定接口名称,如eth0。

例1:route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0

例2:route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} reject设置到指定网络为不可达,避免在连接到这个网络的地址时程序过长时间的等待,直接就知道该网络不可达。

例:route add -net 10.0.0.0 netmask 255.0.0.0 reject

格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}

格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} reject

用于删除路由设置。参数指定的方式与route add相似。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161145.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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