V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
cathedrel
V2EX  ›  Linux

桌面系统,两块网卡正常都开启的情况下,流量总是默认走 enp4s0f0,绝对不走 f1,如何设置某些程序走 f1?不会 iptables 命令

  •  
  •   cathedrel · 2021-01-15 13:33:12 +08:00 · 2306 次点击
    这是一个创建于 1187 天前的主题,其中的信息可能已经有所发展或是发生改变。

    manjaro,日常工作电脑,主板上的两个网口各接一个路由器,除非关闭 f0,否则所有流量默认走 f0,绝对不走 f1 (开 iptraf-ng 观测的结果),请问如何在 f0 不关闭的情况下能指定某个程序的流量走 f1 ?两个路由器的内网网段都设置的 192.168.10.x,

    第 1 条附言  ·  2021-01-16 16:53:12 +08:00
    把两个路由器的网段设置成不同的网段后事情好办多了
    16 条回复    2021-01-16 12:33:32 +08:00
    julyclyde
        1
    julyclyde  
       2021-01-15 14:22:10 +08:00
    首先你这网络设置就不对
    no1xsyzy
        2
    no1xsyzy  
       2021-01-15 15:00:26 +08:00   ❤️ 2
    不会 iptables 命令,那就去学 iptables 命令
    但是内网网段一致的话,比较有挑战,你得时刻记着自己写的这是哪个的……
    Cat73
        3
    Cat73  
       2021-01-15 15:14:24 +08:00
    不是某些程序,是某些地址吧,根据路由表
    LiYanHong
        4
    LiYanHong  
       2021-01-15 15:18:32 +08:00
    可以设置不同用户走不同网卡,然后用指定用户运行需要的程序
    cathedrel
        5
    cathedrel  
    OP
       2021-01-15 16:47:04 +08:00
    @julyclyde 请问该怎么设置呢?
    cathedrel
        6
    cathedrel  
    OP
       2021-01-15 16:48:11 +08:00
    @no1xsyzy 会一点 firewalld 命令,能用起来吗?如有必要,内网网段可以设不一样,那么该怎么设置呢?求教
    cathedrel
        7
    cathedrel  
    OP
       2021-01-15 16:49:37 +08:00
    @Cat73 如果能设置某些地址走哪张网卡可以,怎么设置呢?
    cathedrel
        8
    cathedrel  
    OP
       2021-01-15 16:50:42 +08:00
    @LiYanHong 这样子不是最理想状况,不过也想了解一下怎么设置不同用户走不同网卡,请指点一下
    Cat73
        9
    Cat73  
       2021-01-15 16:55:11 +08:00   ❤️ 1
    @cathedrel route 系列命令,配置路由表
    teawithlife
        10
    teawithlife  
       2021-01-15 17:07:34 +08:00   ❤️ 1
    先把两个网卡的地址改为非同一网段,不然很可能出现各种奇怪的问题

    如果要所有流量都走 enp4s0f1,运行以下命令
    sudo ip route del default
    sudo ip route add default via <enp4s0f1 本地 IP> dev enp4s0f1

    如果只是部分目标 IP 的流量走 enp4s0f1,比如 123.0.0.0/8 的流量,那么执行
    sudo ip route add 123.0.0.0/8 via <enp4s0f1 本地 IP> dev enp4s0f1

    要按程序来指定的话,估计只能通过 iptables 了
    guochao
        11
    guochao  
       2021-01-15 17:47:44 +08:00   ❤️ 2
    建立 net_cls cgroup,对 cgroup 进行 mark,在 route 中使用 mark 建立路由

    如果需要的话可以用 systemd-slice 对一组 exec 进行 mark 。或者自己写脚本。

    除此之外,还可以用这个方法对单独的进程设置防火墙或者施行 traffic shaping

    ref:
    这篇博客描述了大概的做法: https://www.evolware.org/?p=369
    防止博客崩掉贴上 Archive: https://web.archive.org/web/20201112024042/https://www.evolware.org/?p=369
    Arch Wiki - cgroups: https://wiki.archlinux.org/index.php/Cgroups
    man pages - cgroups: https://man7.org/linux/man-pages/man7/cgroups.7.html
    man pages - ip-route: https://man7.org/linux/man-pages/man8/ip-route.8.html
    cathedrel
        12
    cathedrel  
    OP
       2021-01-15 17:53:48 +08:00
    @teawithlife 谢谢大侠
    如果要按照程序来指定的话 iptabls 命令该怎么写?您再给个例子吧,谢谢!
    cathedrel
        13
    cathedrel  
    OP
       2021-01-15 17:55:15 +08:00
    @guochao 谢谢!您给的几个链接我回头慢慢看,谢谢!
    LiYanHong
        14
    LiYanHong  
       2021-01-15 22:59:20 +08:00   ❤️ 1
    指定用户走指定网卡
    iptables -t mangle -A OUTPUT -m owner –uid-owner 1001 -j MARK –set-mark 1001
    iptables -t nat -A POSTROUTING -m mark –mark 1001 -j SNAT –to-source 10.20.30.40
    1001 是用户 id,10.20.30.40 是网卡 ip 地址
    teawithlife
        15
    teawithlife  
       2021-01-16 09:31:01 +08:00   ❤️ 1
    @cathedrel #12 iptables 肯定是能实现,11 楼的方法应该就可以。但是很抱歉,因为我自己没操作过,所以没法给你例子,你得自己看看文档了
    tkl
        16
    tkl  
       2021-01-16 12:33:32 +08:00
    @guochao 学到了 原来 cg 还可以控制网络
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   969 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:58 · PVG 05:58 · LAX 14:58 · JFK 17:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.