Fork me on GitHub

【树莓派】双网卡添加多路由静态路由持久化的问题处理

在实际现场遇到这样一个问题,双网卡添加多路由静态路由持久化的问题,后来经过尝试,最终使用一种比较挫的办法解决了,但我认为只是一个临时解决方案,这里暂时补录于下,下次再出现问题用于回朔。

一.问题:

客户现场的网络有一定限制,外网网段为:192.168.0.*网段,内网为 192.168.12.*网段,但内网需要和另外两台机器(192.168.104.198、192.168.254.111)互通,配置成 static ip_address=192.168.12.22/16 或者 static ip_address=192.168.12.22/8,结果并不生效;子网wlan限制比较麻烦;

而在树莓派上面配置的网络情况如下:

interface eth1
static ip_address=192.168.12.22/24
static routers=192.168.12.1

interface eth2
static ip_address=192.168.0.113/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

网络重启之后,可见结果如下:

eth1      Link encap:Ethernet  HWaddr 00:0e:c6:cc:2f:74  
          inet addr:192.168.12.22  Bcast:192.168.12.255  Mask:255.255.255.0
          inet6 addr: fe80::7c6e:1e5d:52f0:7ec/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:635257 errors:0 dropped:0 overruns:0 frame:0
          TX packets:692 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:53507871 (51.0 MiB)  TX bytes:106496 (104.0 KiB)

eth2      Link encap:Ethernet  HWaddr 00:9a:9b:96:6a:a5  
          inet addr:192.168.0.113  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::3145:4033:dc2e:3588/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:296807 errors:0 dropped:3761 overruns:0 frame:0
          TX packets:80909 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17482980 (16.6 MiB)  TX bytes:40364469 (38.4 MiB)

二.处理办法

尝试了如下解决办法:

1.直接修改/etc/dhcpcd.conf的配置信息:static ip_address=192.168.12.22/16 或者 static ip_address=192.168.12.22/8;

结果:不生效;

2.手动添加路由:

sudo route add -net 192.168.104.0/24 gw 192.168.12.1 eth1
sudo route add -net 192.168.254.0/24 gw 192.168.12.1 eth1
sudo route add default gw 192.168.0.1

添加之后,可见网络连通;

将这些内容写在 /etc/rc.local中,可见并无效果。

手动强制source:

sudo -s source /etc/rc.local

结果OK!

 

3.或者直接添加192.168.0.0网段,并编写配置开机启动脚本:

sudo route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.12.1 dev eth1

但是我们还需要能够自动添加,开机启动就增加,而不是需要每次人工去启动,于是增加在 /etc/rc.local中。

/sbin/route add -net 192.168.104.0/24 gw 192.168.12.1 eth1
/sbin/route add -net 192.168.254.0/24 gw 192.168.12.1 eth1
/sbin/route add default gw 192.168.0.1

但是重启之后,却并没有生效,持久化有问题,得继续处理。

开机启动脚本:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          rc.local
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO


PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
        if [ -x /etc/rc.local ]; then
                [ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local)"
                sudo -s source /etc/rc.local
                ES=$?
                [ "$VERBOSE" != no ] && log_end_msg $ES
                return $ES
        fi
}

case "$1" in
    start)
        do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop|status)
        # No-op
        exit 0
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

然后加入chkconfig中

sudo chkconfig --add netmu

重启之后,还是存在问题。

经过查询,问题在于树莓派Jessie版本有一定问题导致 /etc/rc.local不生效(可以参见 https://www.jeffgeerling.com/blog/2016/setting-static-ip-address-raspbian-jessie-lite-on-raspberry-pi

在这篇文章(https://raspberrypi.stackexchange.com/questions/39785/dhcpcd-vs-etc-network-interfaces)中提到:

Be aware that the latest version of Raspbian Jessie (12/30/2016) contains a flaw in the second line of the interfaces file. The second line is missing the leading # symbol to designate the line as a comment. This problem causes the interfaces file to be unreadable by the system. I spent many hours trying to get my wifi to work before I found this error.

To fix it, simply add a # symbol to the start of the line.

这篇文章中(https://nebulousthinking.wordpress.com/2016/02/25/setting-a-static-ip-for-raspbian-jessie-in-2016/)提到了:

需要配置dns之类信息,但其实我们之前早已做了配置。这些解释对有需要的人可以在读一读:

  • interface = the name of the network connection; this should be either eth0 or wlan0 for 99% of us home users
  • static ip_address = the address you want to force your Pi to use. It is unknown to me why we put the /24 at the end there. Maybe some kind soul can ELI5 in the comments here.
  • static routers = your gateway / router on the LAN the Pi will connect to.
  • static domain_name_servers = the DNS server you want the Pi to connect to when it can’t resolve a network address. 8.8.8.8 is the free Google DNS server but use whatever you prefer there.

看来和系统有一定关系,于是尝试 /etc/init.d/rc.local等文件修改。但是还没生效。

 

4.解决办法:

树莓派的配置信息在 /etc/network/interfaces 下:

/sbin/route add -net 192.168.104.0/24 gw 192.168.12.1 eth1
/sbin/route add -net 192.168.254.0/24 gw 192.168.12.1 eth1

但是还需要开机自动执行。

最终采用crontab去实现,结果OK;

 

sudo vi /etc/network/netmu.sh

内容为:

#!/bin/bash
#ifconfig eth1 down
#ifconfig eth1 up
/sbin/route add -net 192.168.104.0/24 gw 192.168.12.1 eth1
/sbin/route add -net 192.168.254.0/24 gw 192.168.12.1 eth1

添加到crontab中:

0/2 * * * *     root   /etc/network/netmu.sh

保存退出。

 

三.其他补充:

redhat 或者fedora系列类,永久静态路由是添加在/etc/sysconfig/network-scripts目录下对应设备文件中。

关于添加路由的一些信息,可以参考:http://wiki.ubuntu.org.cn/Quick_HOWTO_:_Ch03_:_Linux_Networking/zh,部分参考如下:

添加临时的静态路由

route add命令可以用来添加一条新路由到你的服务器,直到下一次重启。这个方法的优势是在所有版本的Linux中都通用,而且在man里面都有详细的说明。在我们的例子里,10.0.0.0网络前面有-net参数,子网掩码和网关前面也分别有netmask和gw参数。

[root@bigboy tmp]# route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 wlan0

如果你想为一个单独的服务器添加路由,那么应该使用-host参数,而且不设子网掩码值(route命令会自动知道子网掩码是255.255.255.255)。这里是为机器10.0.0.1添加路由的例子。

[root@bigboy tmp]# route add -host 10.0.0.1 gw 192.168.1.254 wlan0

如果想要这个变化在重启后也能持续,一个通用的方法是把route add命令添加到/etc/rc.d/rc.local文件中,它会始终在启动过程结束前运行。

添加永久静态路由

在Fedora Linux中,永久静态路由是添加在/etc/sysconfig/network-scripts目录下对应设备文件中的。文件名的格式是route-网络设备名称,所以wlan0设备对应的文件名是route-wlan0

该文件的格式很直观,第一行是目标网络加上单词via和网关的IP地址。在我们的例子中,要设置一个到通过网关192.168.1.254 到网络10.0.0.0的路由,子网掩码是255.0.0.0(一个头8位都是1的掩码),我们像这样设置/etc/sysconfig/network-scripts/route-wlan0文件:

#
# File /etc/sysconfig/network-scripts/route-wlan0
# 
10.0.0.0/8 via 192.168.1.254

注意: 文件名/etc/sysconfig/network-scripts/route-*非常重要。在route-后面添加了错误的设备后缀会导致下次启动后路由不能被正确添加。而且不会在屏幕上报告错误或者在/var/log/目录下生成任何日志文件。

你可以通过运行/etc/sysconfig/network-scripts/ifup-routes命令加设备名作为参数测试新建的文件。在下面例子里,我们先检查路由表查看没有到网络10.0.0.0的路由,然后执行ifup-routes命名把路由加上。

[root@bigboy tmp]# netstat -nr 
Kernel IP routing table 
Destination Gateway	   Genmask 	  Flags MSS Window irtt Iface
192.168.1.0 0.0.0.0       255.255.255.0  U     0   0      0    wlan0
169.254.0.0 0.0.0.0       255.255.0.0    U     0   0      0    wlan0
0.0.0.0     192.168.1.1   0.0.0.0        UG    0   0      0    wlan0
[root@bigboy tmp]# ./ifup-routes wlan0
[root@bigboy tmp]# netstat -nr
Kernel IP routing table
Destination Gateway 	   Genmask 	 Flags MSS Window irtt Iface
192.168.1.0 0.0.0.0       255.255.255.0 U     0   0      0    wlan0
169.254.0.0 0.0.0.0       255.255.0.0   U     0   0      0    wlan0
10.0.0.0    192.168.1.254 255.0.0.0     UG    0   0      0    wlan0
0.0.0.0     192.168.1.1   0.0.0.0       UG    0   0      0    wlan0
[root@bigboy tmp]#

注意: 在基于Debian的系统中,永久静态路由是配置在文件/etc/network/interfaces中的。请参阅后面章节"Debian / Ubuntu网络配置"以了解更多详情。

怎样删除路由

这里介绍了怎样删除之前章节添加的路由。

[root@bigboy tmp]# route del -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 wlan0

文件 /etc/sysconfig/network-scripts/route-wlan0也需要被更新,这样你重启服务器後路由才不会被重新加回来。删除如下的行:

10.0.0.0/8 via 192.168.1.254

其余更多,在使用中再继续摸索吧。

 

posted @ 2017-06-23 17:41  念槐聚  阅读(2315)  评论(0编辑  收藏  举报

IT技术&应用开发&研究 - 创建于 2008年05月12日

这是一位IT工程师的个人站,内容主要是网站开发方面的技术文章,大部分来自学习或工作,部分来源于网络,希望对大家有所帮助。

致力于软件学习&研究工作,涉及Linux与软件开发出、测试、产品、行业相关知识,关注互联网前沿技术与与创业趋势等。


博客园 | Github | W3C

返回顶部