Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
nifury
V2EX  ›  Linux

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

  •  
  •   nifury · Sep 20, 2016 · 10689 views
    This topic created in 3700 days ago, the information mentioned may be changed or developed.

    我正在使用 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 的默认路由呢?

    Supplement 1  ·  Sep 20, 2016
    我通过增加
    post-up sleep 6; ip route del default dev eth0
    解决了,但是这种延迟的办法心中不爽啊,有没有更好的做法呢?
    Supplement 2  ·  Sep 20, 2016
    真正的解决了,在 /etc/dhcpcd.conf 中加入
    interface eth0
    nogateway

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

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

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

    /etc/dhcp/dhclient.conf 添加

    interface eth0 {
    supersede routers ""
    }

    表示忽略来自 eth0 的 DHCP 里面的 gateway 信息。
    nifury
    nifury
    OP
    Sep 20, 2016
    10
    @jasontse 谢谢~
    我装过 ifmetric 然而并没有修改 metric
    修改过 dhclient.conf 然而并没有效果
    我 /etc/dhcp3/下并没有 dhclient.conf ,所以改的是 /etc/dhcp/下的
    不过在 dhcp3 下创建该 conf 也无效
    nifury
    nifury
    OP
    Sep 20, 2016
    11
    @sylecn 谢谢~
    这个我也看到过
    不过并没有效果
    cmlz
    cmlz
    Sep 20, 2016
    12
    你两个网卡都是 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
    nifury
    OP
    Sep 20, 2016
    13
    @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
    cmlz
    Sep 20, 2016
    14
    eth0 取消 DHCP ,把你 eth0 获取到的 IP 直接写到配置文件好了。
    pagxir
    pagxir
    Sep 20, 2016 via Android
    15
    关闭本机的反向路由过滤 or 双线策略路由。
    xiaooloong
    xiaooloong
    Sep 20, 2016
    16
    默认路由 == 网关地址

    只在一个网卡上配置网关就行了
    cmlz
    cmlz
    Sep 20, 2016
    17
    不关闭 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
    sylecn
    Sep 20, 2016
    18
    @nifury 修改后有没有 kill 掉已经启动的 dhclient 进程?不 kill 应该不生效的。
    nifury
    nifury
    OP
    Sep 20, 2016
    19
    @sylecn 我修改后都是重启电脑的
    nifury
    nifury
    OP
    Sep 20, 2016
    20
    @cmlz 唔,我需要 DHCP 的,因为两个校区分配的 IP 段不一样
    而增加那两行命令也没有效
    cmlz
    cmlz
    Sep 20, 2016
    21
    @nifury
    难道你的 eth0 每次获取到的网关都不一样?
    nifury
    nifury
    OP
    Sep 20, 2016
    22
    @cmlz 在同一个校区是一样的,但另一个校区的网关不一样
    nifury
    nifury
    OP
    Sep 20, 2016
    23
    @pagxir 谢谢。本机的反向路由过滤是关闭的
    其实我只是想删掉 eth0 的默认路由而已,只是不知道怎么做
    cmlz
    cmlz
    Sep 20, 2016
    24
    @nifury
    /etc/network/interfaces 后面只添加下面一行试试:

    down route del -net 0.0.0.0 dev eth0
    cmlz
    cmlz
    Sep 20, 2016
    25
    把你的 /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
    nifury
    OP
    Sep 20, 2016
    26
    @cmlz 谢谢,这个我试过,并没有用
    另外为何是 down ,不应该是 up 么?
    我试过 post-up 打印路由表,发现是空的,所以删除是无效的
    猜测启动的时候 DHCP 还没回应,所以并没有删除
    cmlz
    cmlz
    Sep 20, 2016
    27
    @nifury
    是 up 才对,如果你试过无效的话,看来只能延时等 DHCP 起效再操作了。
    JamesRuan
    JamesRuan
    Sep 20, 2016
    28
    “因为系统默认回复到 eth0 导致 TCP 连接无法建立”怎么可能吗?

    是因为 sshd 开在 wlan up 之前吗?
    如果不是的话,无论如何,如果按 0.0.0.0:22 进行 listern 的话,自然可以连接上啊?
    catror
    catror
    Sep 20, 2016 via Android
    29
    把内网的网关删除,手动加一条内网的路由,只路由内网地址
    allen2000
    allen2000
    Sep 20, 2016
    30
    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
    catror
    Sep 20, 2016 via Android
    31
    噢,是 DHCP 的啊…我有一台设备也是同时接内外网,不过地址都是静态的,内网不配置网关,然后加一条指向内网的网关的路由给内网地址使用就行
    allen2000
    allen2000
    Sep 20, 2016
    32
    可能还需要运行一下, 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.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   747 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 358ms · UTC 22:59 · PVG 06:59 · LAX 14:59 · JFK 17:59
    ♥ Do have faith in what you're doing.