我如何强制Chrome使用特定的网络接口(例如,tun1)?
一些程序(如curl
)可以选择特定的网络接口。
在Windows中,ForceBindIP允许您强制应用程序使用特定的网络接口/ IP地址。
我想知道在Ubuntu/Linux中是否有这样的解决方案。
或者,因为我们可以在Chrome中设置一个代理;我们是否可以将本地IP重定向到网络接口,反之亦然?
发布于 2022-10-12 21:53:22
这是网络名称空间的完美用例,自2016年或更早以来,网络命名空间一直是Linux的一部分。下面是一个包含注释的示例,可以让您开始工作:
# enable forwarding
sysctl -w net.ipv4.ip_forward=1
# create the network namespace
ip netns add chrome
# create the virtual nic and it's peer
ip link add chrome type veth peer name chrome-peer
# assign the peer to the network namespace
ip link set chrome-peer netns chrome
# assign an ip address
ip addr add 192.0.2.1/24 dev chrome
# bring up interface
ip link set chrome up
# similar network setup for network namespace
ip netns exec chrome ip link set lo up
ip netns exec chrome ip addr add 192.0.2.2/24 dev chrome-peer
ip netns exec chrome ip route add default via 192.0.2.1
ip netns exec chrome ip link set chrome-peer up
# allow forwarding and add enable NAT
iptables -I FORWARD -s 192.0.2.0/24 -j ACCEPT
iptables -t nat -I POSTROUTING -s 192.0.2.0/24 -o tun1 -j MASQUERADE
# pop a shell in the namespace
ip netns exec chrome bash
# check that you're in the namespace
ip netns identify
# run the browser as your local user
runuser -u Barry google-chrome
https://askubuntu.com/questions/1313755
复制