Hello, everyone. I'm your friend Acquainta again.
Linux添加临时路由及永久路由的方法
Linux's method of adding temporary and permanent routes
一:使用 route 命令添加 使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法: //添加到主机的路由
One: add route to the route to the route to the route to the route to be added using the route to the route to the route to the route to be added using the route to the route to the route to be added using the route to the route to the route to be rebooted by the machine or re-opening of the netcard. Method: / / add route to the host
//添加到网络的路由
/ / Add to network route
//添加默认网关
// Add default gateway
//删除路由
// Delete route
删除默认路由
Delete default route
#route del default gw 192.168.2.1
二:在linux下设置永久路由的方法: 1.在/etc/rc.local里添加 方法: route add -net 192.168.3.0/24 dev eth0 route add -net 192.168.2.0/24 gw 192.168.2.254
II: Method of setting the permanent route under linux: 1. Add method: route add -net 192.168.03.0/24 dev eth0 route add -net 192.168.2.0/24 gw 192/168.2.254
2.在/etc/sysconfig/network里添加到末尾 方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev
Add the end to/etc/sysconfig/network Method: GATEWAY=gw-ip or GATEWAY=gw-dev
3https://cloud.tencent.com/developer/article/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
3https://cloud.tencent.com/development/article/etc/sysconfig/static-rootes: any net 192/168.0/24 gw192/168.3.254 any net 10.250.228.128 netmask 255.255.192 gw 10.250.228.129
4.开启 IP 转发:
4. Open IP forwarding:
这里并没有错误的地方,只不过之前没有接触对这些参数没什么想法,这次正好研究了下,大概了解了下没个参数的意思。 首先从最简单的是route add方法: route add这个是添加路由的命令,这个没什么好说的,然后是后面的-host和net参数,根据紧跟参数后面的信息可以发现host指的是添加一个主机到路由中,net是添加一个网段到路由中。在后面的参数是dev和gw,字面理解加上后面紧跟的值判断,dev表示这个host或者这段net走的是那个网口,gw是gateway,自然是通过哪个网关了。对这里为何指定网口或者指定网关都行呢,我的认识是这样的,网口上配置有网关的信息,指定的网口其实最终还是去读取这个网口对应的网关信息,等效于指定gw参数。不过指定网口一般是由于有多个网络环境的情况,如果网口没有指定网关,会去读取默认的网关,所以这里一般要求把网口的网关信息配置上去,在linux下很多时候网关信息并不是配置在网口的配置文件,比如:/etc/sysconfig/network-scripts/ifcfg-eth0 文件里面,而是在/etc/sysconfig/network这里面,所以在用dev参数的时候需要小心这点,尽可能通过指定gw的方式。 为什么会介绍到route add方法,因为后面会有用到。现在回到主题,关于static-routes的设置。网上一般的都是写着: any net 192.168.3.0/24 gw 192.168.3.254 设置的都是整个网段的,但如果是单个主机怎么设置呢?同时从厂家那边给了一个脚本是: eth0 x.x.x.x netmask 255.255.255.192 gw x.x.x.x 看上去也很对的样子,但是执行发现netstat -an里面看不到添加的信息,完全不行。 然后我们在看下static-routes这个文件的作用,根据网上的说法,static-routes其实在被network这个脚本调用的,打开这个脚本: /etc/rc.d/init.d/network 里面有写到:
Here's nothing wrong, but there's nothing wrong with these parameters before we get in touch with them. Here's just what we're looking at. First from the simplest: root anadd method: this is an order to add route, and then the next -- host and net parameters, based on the information behind the parameters, can be found that host refers to adding a host to the path, and the net adds a section to the path. The following parameter is dev and grow, and the literal version of the net adds a value to the bottom.
if [ -f /etc/sysconfig/static-routes ]; then grep “^any” /etc/sysconfig/static-routes | while read ignore args ; do /sbin/route add – a r g s d o n e f i n e t w o r k 在执行的时候到 s t a t i c ? r o u t e s 里面寻找 a n y 开头的行,然后执行 / s b i n / r o u t e a d d ? args done fi network在执行的时候到static-routes里面寻找any开头的行,然后执行 /sbin/route add – argsdonefinetwork在执行的时候到static?routes里面寻找any开头的行,然后执行/sbin/routeadd?args 相当于手动执行了route add脚本,那后面事情就简单了,回到了手动执行的方式,根据前面的说明,正确的脚本就是: any host 10.0.0.10 gw 192.168.1.1 network执行的时候把any去掉,获取后面的参数:host 10.0.0.10 gw 192.168.1.1 然后执行route add -host 10.0.0.10 gw 192.168.1.1 注意这里host前面的-是network自带的,不需要配置到static-routes文件中。 ############################### echo “route add -net 172.16.100.0/24 gw 192.168.6.249” >>/etc/rc.local 使得在系统重启之后,加入这条命令。 ################################################################ Debian/Ubuntu RedHat/CentOS/Fedora如何永久添加静态路由
f /etc/sysconfig/static-routes]; n grep “any” /etc/sysconfig/static-routes
目前Linux系统网络配置基本都差不多,但是不同的发行版又有些不同,常见主要区分两大类型: (1)RedHat(RHEL)/CentOS/Fedora Linux是放在/etc/sysconfig/network-scripts目录下面的一大堆文件里面。 (2)Debian/Ubuntu Linux网络配置则是存在/etc/network/目录下面的几个文件里面。
Currently, the Linux system network configurations are largely similar, but different distribution versions are somewhat different, with two main types often distinguished: (1) RedHat (RHEL)/CentOS/Fedora Linux is placed in a large pile of files under the /etc/sysconfig/network-scripts directory. (2) Debian/Ubuntu Linux network configuration is in several files under the /etc/network/ directory.
比如我们要给linux系统添加静态路由,就要区分不同的平台,RedHat(RHEL)/CentOS/Fedora Linux环境永久添加静态路由通常是写在诸如/etc/sysconfig/network-scripts/route-eth0这些文件里。而Debian/Ununtu下有所不同,添加路由的脚本放到/etc/network/interfaces里执行。
For example, if we want to add static routes to the Linux system, we have to distinguish between different platforms, and the ReedHat (RHEL)/CentOS/Fedora Linux environment permanently adds static paths that are usually written in documents such as /etc/sysconfig/network-scripts/route-eth0.
一、Debian平台实例: Debian/Ununtu添加路由的脚本放到/etc/network/interfaces里执行: auto eth0 iface eth0 inet static address 192.168.1.88 netmask 255.255.255.0 gateway 192.168.1.1 up route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1 down route del -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1
I. Example of Debian/Ununtu platform: A script added by Debian/Ununtu is placed in /etc/network/interfaces: auto eth0 eth0 inet state address 192.1668.1.88 netmask 255.255.255.0 Gateway 192.168.1.1 up route add -net 192.168.2.0 netmask 255.255.0 gw 192.168.2.1 down route del -net 192.168.2.0
二、RedHat、CentOS、Fedora平台实例: 使用配置文件/etc/sysconfig/network-scripts/route-interface,interface可以根据实际的网卡换成网卡名,比如我要在eth0上加静态路由,那就写route-eth0 配置文件的格式如下:
ii. Examples of RedHat, Centos, Fedora platforms: using configuration files/etc/sysconfig/network-scripts/route-interface, interface can change to net card names based on actual web cards, e.g., if I want to add static route on eth0, the format of the route-eth0 profile will be as follows:
ADDRESS0=X.X.X.X NETMASK0=x.x.x.x GATEWAY0=X.X.X.x
如果有多条静态路由要建立,那么继续在文件里追加,ADDRESS、NETMASK、GATEWAY后面的数字递增即可。如有2条记录:
If there are a number of static routes to be built, then continue to be added to the document by increasing numbers after ADDRRESS, NETMASK, GATEWAY.
ADDRESS0=X.X.X.X NETMASK0=x.x.x.x GATEWAY0=X.X.X.x
ADDRESS1=X.X.X.X NETMASK1=x.x.x.x GATEWAY1=X.X.X.x
采用规范的方法的好处如下: 1.系统启动的时候自动加上这些静态路由,无需写rc.local 2.如果修改了静态路由,只需要service network restart即可,无需手动使用route命令增删静态路由
The benefits of a regulated approach are as follows: 1. These static routes are automatically added to the system when it is activated and need not be written rc.local 2. If the static route is modified, only the service network is required to add or delete the static route without the manual use of the roote command.
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
Copyright statement: The content of the text is contributed by Internet users, and this view only represents the author. The site provides only information storage space services, does not have ownership rights and does not incur legal liability.
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185921.html原文链接:https://javaforall.cn
Published by: Chief of the Whole House Programmer, referenced in https://javaforall.cn/185921.html link to original language: https://javaforall.cn
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论