Ubuntu双网卡不双待攻略
特别不喜欢基于Windows的工作环境,这对一个Unix/Linux程序员来说,可以说是一种信仰。要不是电子邮件客户端被牢牢绑定在可恶的Outlook 2013上(因为加密邮件要求, 想使用Thunderbird都不行),我想我坚决不会在工作中使用Windows10。 幸运的是,还有一个台式机(代号: ISX)可以用,安装的操作系统是Ubuntu14.04LTS。
1. 需求
在Ubuntu14.04LTS上既能访问外网,又能访问内网。外网用来冲浪,内网用来干活。
2. 困境
台式机ISX只有一个有线网卡,没有无线网卡。
root@idorax:~# lspci | egrep Ethernet 02:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 0c)
硬件条件不允许双网卡,只能自己想办法。
3. 办法
3.1 加硬件
自己花钱买一个USB网卡转换器就是了,看起来是这样子滴,
3.2 改路由
新加的USB网卡转换器连接外网,原来的有线网卡连接内网,都采用DHCP方式上网。 连接完毕后查看网络连接信息是这样的,
o eth1 <-- USB网卡转换器, 冲浪
o eth0 <-- 有线网卡, 干活
o 系统启动后的初始路由
root@idorax:~# route | sed '1d' Destination Gateway Genmask Flags Metric Ref Use Iface default 10.123.255.254 0.0.0.0 UG 0 0 0 eth1 10.123.0.0 10.123.255.254 255.255.0.0 UG 0 0 0 eth0 10.123.0.0 * 255.255.0.0 U 1 0 0 eth1 10.123.255.254 * 255.255.255.255 UH 0 0 0 eth0 100.9.1.0 * 255.255.255.0 U 1 0 0 eth0
其中, eth0(连接内网)的路由设置会影响外网(eth1)的访问。
o 修改路由
root@idorax:~# route add -net 100.0.0.0/8 gw 100.9.1.254 metric 20 dev eth0 root@idorax:~# root@idorax:~# route del -net 10.123.0.0 netmask 255.255.0.0 gw 10.123.255.254 dev eth0 root@idorax:~# route del -net 10.123.255.254 netmask 255.255.255.255 dev eth0 root@idorax:~# route del -net 100.9.1.0 netmask 255.255.255.0 dev eth0
o 修改后的路由
root@idorax:~# route | sed '1d'
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.123.255.254 0.0.0.0 UG 0 0 0 eth1
10.123.0.0 * 255.255.0.0 U 1 0 0 eth1
100.0.0.0 100.9.1.254 255.0.0.0 UG 20 0 0 eth0
到此为止, 基于Ubuntu14.04LTS的双网卡不双待的工作环境就设置好了。
- eth0用来访问内网 (注意其Metric被设置为20), eth1用来访问外网。
- eth0不再干扰eth1, 满足需求。
上面的手工添加/删除路由的过程可以用bash脚本实现,吼吼,再次表示不喜欢Windows, 虽然Windows也支持脚本编程。
o vroute.sh
#!/bin/bash TMPDIR=/tmp NAME=$(basename $0) function show_route { route | tr -s ' ' ':' | sed 's/*/0.0.0.0/g' | sed '1d' } function add_route_100_ug { typeset net=$1 typeset gw=$2 typeset metric=$3 typeset dev=$4 typeset f_out=$TMPDIR/$NAME.out.$$ show_route | egrep "UG" | egrep ":$dev$" | \ awk -F':' '{print $1}' > $f_out while read line; do typeset s=$(echo $line) [[ $s == ${net%/*} ]] && return 0 done < $f_out sudo route add -net $net gw $gw metric $metric dev $dev return $? } function del_route_10_ug { typeset net=$1 typeset dev=$2 typeset f_out=$TMPDIR/$NAME.out.$$ show_route | egrep "UG" | egrep ":$dev$" > $f_out typeset s="" typeset s_net="" typeset found=1 while read line; do s=$(echo $line) s_net=$(echo $line | awk -F':' '{print $1}') [[ $s_net == $net ]] && found=0 && break done < $f_out (( found != 0 )) && return 0 typeset netmask=$(echo $s | awk -F':' '{print $3}') typeset gw=$(echo $s | awk -F':' '{print $2}') sudo route del -net $net netmask $netmask gw $gw dev $dev return $? } function del_route_10_uh { typeset dev=$1 typeset s=$(show_route | egrep ":UH:" | egrep ":$dev$") [[ -z "$s" ]] && return 0 typeset net=$(echo $s | awk -F':' '{print $1}') typeset netmask=$(echo $s | awk -F':' '{print $3}') sudo route del -net $net netmask $netmask dev $dev return $? } function del_route_10_u { typeset dev=$1 typeset s=$(show_route | egrep ":U:" | egrep ":$dev$") [[ -z "$s" ]] && return 0 typeset net=$(echo $s | awk -F':' '{print $1}') typeset netmask=$(echo $s | awk -F':' '{print $3}') sudo route del -net $net netmask $netmask dev $dev return $? } trap "rm -f $TMPDIR/$NAME.*.$$" EXIT eth="eth0" add_route_100_ug 100.0.0.0/8 100.9.1.254 20 $eth || exit 1 del_route_10_ug 10.123.0.0 $eth || exit 2 del_route_10_uh $eth || exit 3 del_route_10_u $eth || exit 4 exit 0
结束语: 毛主席说过,“广阔天地,大有作为”, 你懂的 :-) 环境太艰苦,只能自己想办法,"艰难困苦,玉汝于成"。 如果你想了解“Win10双网卡不双待攻略”, 请狠戳这里。看完之后,你就很能理解我为什么很不喜欢Windows了。
扩展阅读: