Debian 双网卡如何只保留一个默认路由呢? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
nifury
V2EX    Linux

Debian 双网卡如何只保留一个默认路由呢?

  nifury 2016-09-20 11:09:14 +08:00 9661 次点击
这是一个创建于 3312 天前的主题,其中的信息可能已经有所发展或是发生改变。

我正在使用 debian 8 ,有 eth0 和 wlan0 , eth0 连接内网, wlan0 连接外网,/e/n/interfaces 如下:

allow-hotplug eth0 iface eth0 inet dhcp allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf 

默认的路由表如下:

Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.17.128.2 0.0.0.0 UG 202 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 303 0 0 wlan0 172.17.128.0 0.0.0.0 255.255.128.0 U 202 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 303 0 0 wlan0 

现在的问题是我无法通过 WIFI 来 SSH 到我的主机,因为系统默认回复到 eth0 导致 TCP 连接无法建立

以下是我尝试过的方法:

unset new_routers

post-up route del default dev eth0

把 wlan0 设置 metric 0

都没用

而且我手动删掉 eth0 的默认路由之后,过一会儿自己又回来了……

有没有什么办法可以在系统启动的时候只保留 wlan0 的默认路由呢?

第 1 条附言    2016-09-20 13:57:08 +08:00
我通过增加
post-up sleep 6; ip route del default dev eth0
解决了,但是这种延迟的办法心中不爽啊,有没有更好的做法呢?
第 2 条附言    2016-09-20 19:07:28 +08:00
真正的解决了,在 /etc/dhcpcd.conf 中加入
interface eth0
nogateway

多谢大家的帮助啦~
32 条回复    2016-09-20 18:32:50 +08:00
saar
    1
saar  
   2016-09-20 11:15:55 +08:00
不用那条 metric 调高就好了
nifury
    2
nifury  
OP
   2016-09-20 11:18:34 +08:00
@saar 试过了
allow-hotplug eth0
iface eth0 inet dhcp
metric 1000

重启看 metric 并没有变化
zsj950618
    3
zsj950618  
   2016-09-20 11:21:00 +08:00
你需要的是策略路由,从那个网卡进来的从哪个网卡出去。。。
nifury
    4
nifury  
OP
   2016-09-20 11:24:32 +08:00
@zsj950618 嗯是可以这样
然而我还是想只保留一个默认路由
毕竟以后要更新系统,或者跑自己的小程序的时候,如果默认连到内网的话很不方便呢
sylecn
    5
sylecn  
   2016-09-20 11:27:59 +08:00
eth0 只连了内网,不需要这个默认路由啊。删掉这个试试:

ip r del default via 172.17.128.2 dev eth0
sylecn
    6
sylecn  
   2016-09-20 11:29:03 +08:00
如果确认删除了会自动回来, eth0 设置 static ,手动配置 IP 和 netmask 吧,不写 gateway 就行了。
nifury
    7
nifury  
OP
   2016-09-20 11:31:06 +08:00
@sylecn 谢谢~
然而实际情况是, eth0 连学校内网,是 DHCP 分配 IP 的,我没法手动配置 IP 的呀
sylecn
    9
sylecn  
   2016-09-20 11:40:43 +08:00
另外一个参考
http://superuser.com/questions/725935/default-route-in-debian-with-two-interfaces

/etc/dhcp/dhclient.conf 添加

interface eth0 {
supersede routers ""
}

表示忽略来自 eth0 的 DHCP 里面的 gateway 信息。
nifury
    10
nifury  
OP
   2016-09-20 11:43:09 +08:00
@jasontse 谢谢~
我装过 ifmetric 然而并没有修改 metric
修改过 dhclient.conf 然而并没有效果
我 /etc/dhcp3/下并没有 dhclient.conf ,所以改的是 /etc/dhcp/下的
不过在 dhcp3 下创建该 conf 也无效
nifury
    11
nifury  
OP
   2016-09-20 11:44:24 +08:00
@sylecn 谢谢~
这个我也看到过
不过并没有效果
cmlz
    12
cmlz  
   2016-09-20 11:44:35 +08:00
你两个网卡都是 DHCP 自动获取 IP ?
vi /etc/network/interfaces 贴上你的配置看看。
给你个例子参考下,双网卡配置:
auto eth0
allow-hotplug eth0
iface eth0 inet static
address 120.90.60.131
netmask 255.255.255.240
network 120.90.60.128
broadcast 120.90.60.143
gateway 120.90.60.129
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 8.8.4.4
auto eth0:10
iface eth0:10 inet static
address 192.168.200.118
netmask 255.0.0.0
nifury
    13
nifury  
OP
   2016-09-20 11:46:46 +08:00
@cmlz 谢谢~
是的,都是 DHCP

auto lo
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
cmlz
    14
cmlz  
   2016-09-20 11:51:49 +08:00
eth0 取消 DHCP ,把你 eth0 获取到的 IP 直接写到配置文件好了。
pagxir
    15
pagxir  
   2016-09-20 11:57:02 +08:00 via Android
关闭本机的反向路由过滤 or 双线策略路由。
xiaooloong
    16
xiaooloong  
   2016-09-20 12:05:44 +08:00
默认路由 == 网关地址

只在一个网卡上配置网关就行了
cmlz
    17
cmlz  
   2016-09-20 12:15:36 +08:00
不关闭 eth0 的 DHCP 的话,编辑 /etc/network/interfaces 文件,把下面两行命令加到最后面,应该也行。
down route del -net 0.0.0.0 netmask 0.0.0.0 gw 172.17.128.2 dev eth0
up route add -net 172.0.0.0 netmask 255.0.0.0 gw 172.17.128.2 dev eth0
sylecn
    18
sylecn  
   2016-09-20 13:16:43 +08:00
@nifury 修改后有没有 kill 掉已经启动的 dhclient 进程?不 kill 应该不生效的。
nifury
    19
nifury  
OP
   2016-09-20 13:23:39 +08:00
@sylecn 我修改后都是重启电脑的
nifury
    20
nifury  
OP
   2016-09-20 13:26:44 +08:00
@cmlz 唔,我需要 DHCP 的,因为两个校区分配的 IP 段不一样
而增加那两行命令也没有效
cmlz
    21
cmlz  
   2016-09-20 13:30:00 +08:00
@nifury
难道你的 eth0 每次获取到的网关都不一样?
nifury
    22
nifury  
OP
   2016-09-20 13:35:28 +08:00
@cmlz 在同一个校区是一样的,但另一个校区的网关不一样
nifury
    23
nifury  
OP
   2016-09-20 13:38:28 +08:00
@pagxir 谢谢。本机的反向路由过滤是关闭的
其实我只是想删掉 eth0 的默认路由而已,只是不知道怎么做
cmlz
    24
cmlz  
   2016-09-20 13:55:56 +08:00
@nifury
/etc/network/interfaces 后面只添加下面一行试试:

down route del -net 0.0.0.0 dev eth0
cmlz
    25
cmlz  
   2016-09-20 14:04:05 +08:00
把你的 /etc/network/interfaces 配置这样改:
auto lo
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet dhcp
down route del -net 0.0.0.0 dev eth0
#eth0 激活后删除默认路由

allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
nifury
    26
nifury  
OP
   2016-09-20 17:02:53 +08:00
@cmlz 谢谢,这个我试过,并没有用
另外为何是 down ,不应该是 up 么?
我试过 post-up 打印路由表,发现是空的,所以删除是无效的
猜测启动的时候 DHCP 还没回应,所以并没有删除
cmlz
    27
cmlz  
   2016-09-20 17:40:08 +08:00
@nifury
是 up 才对,如果你试过无效的话,看来只能延时等 DHCP 起效再操作了。
JamesRuan
    28
JamesRuan  
   2016-09-20 18:23:36 +08:00
“因为系统默认回复到 eth0 导致 TCP 连接无法建立”怎么可能吗?

是因为 sshd 开在 wlan up 之前吗?
如果不是的话,无论如何,如果按 0.0.0.0:22 进行 listern 的话,自然可以连接上啊?
catror
    29
catror  
   2016-09-20 18:23:45 +08:00 via Android
把内网的网关删除,手动加一条内网的路由,只路由内网地址
allen2000
    30
allen2000  
   2016-09-20 18:27:50 +08:00
D.2.7. Altering existing routes with ip route change

Occasionally, you'll want to remove a route and replace it with another one. Fortunately, this can be done atomically with ip route change.

Let's change the default route on tristan with this command.

Example D.24. Altering existing routes with ip route change

[root@tristan]# ip route change default via 192.168.99.113 dev eth0
[root@tristan]# ip route show
192.168.99.0/24 dev eth0 scope link
127.0.0.0/8 dev lo scope link
default via 192.168.99.113 dev eth0

from: http://linux-ip.net/html/tools-ip-route.html
catror
    31
catror  
   2016-09-20 18:32:38 +08:00 via Android
噢,是 DHCP 的啊…我有一台设备也是同时接内外网,不过地址都是静态的,内网不配置网关,然后加一条指向内网的网关的路由给内网地址使用就行
allen2000
    32
allen2000  
   2016-09-20 18:32:50 +08:00
可能还需要运行一下, ip route flush cache
If you do use the ip route change command, you should be aware that it does not communicate a routing table state change to the routing cache, so here is another good place to get in the habit of using ip route flush cache.
关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3172 人在线   最高记录 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 37ms UTC 10:58 PVG 18:58 LAX 03:58 JFK 06:58
Do have faith in what you're doing.
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86