在同一台服务器上,以 root 身份,我得到了正确的接口的 internet ip
root@server:~# curl -s --interface ppp0 ipcheck.com
111.111.111.111root@server:~#
然而...
如果不是超级用户,我会得到默认路由/接口 eth0 或 eth1 互联网 ip 的 ip,而不是 ppp0 的互联网 ip,这不是我所期望的。
matrix@server:~$ curl -s --interface ppp0 ipcheck.com
222.222.222.222matrix@server:~$
我的问题是为什么不同的用户会获得不同的 IP/路由?这是同一个应用程序,同一个界面,我已经正确设置了路由...
在 /etc/iproute2/rt_tables 中,我添加了
20 isp1
30 isp2
40 ppp0
50 ppp1
60 ppp2
ip route 的前三行:
default
nexthop via 222.222.222.1 dev eth0 weight 1
nexthop via 222.222.222.2 dev eth1 weight 1
10.200.20.1 dev ppp0 proto kernel scope link src 10.200.21.4
10.200.20.1 dev ppp1 proto kernel scope link src 10.200.21.9
10.200.20.1 dev ppp2 proto kernel scope link src 10.200.21.7
ip 规则:
0: from all lookup local
32761: from all fwmark 0x60 lookup ppp2
32762: from all fwmark 0x50 lookup ppp1
32763: from all fwmark 0x40 lookup ppp0
32764: from all fwmark 0x30 lookup ISP2
32765: from all fwmark 0x20 lookup ISP1
32766: from all lookup main
32767: from all lookup default
iptables:
-A PREROUTING -t mangle -i eth0 -m state --state NEW,RELATED,ESTABLISHED -d 222.222.222.222 -j CONNMARK --set-mark 0x20
-A PREROUTING -t mangle -i eth1 -m state --state NEW,RELATED,ESTABLISHED -d 222.222.222.223 -j CONNMARK --set-mark 0x30
-A PREROUTING -t mangle -i br0 -m connmark --mark 0x20 -j CONNMARK --restore-mark
-A PREROUTING -t mangle -i br0 -m connmark --mark 0x30 -j CONNMARK --restore-mark
-A OUTPUT -t mangle -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
-A PREROUTING -t mangle -i ppp0 -m state --state NEW,RELATED,ESTABLISHED -d 10.200.21.4 -j CONNMARK --set-mark 0x40
-A PREROUTING -t mangle -i br0 -m connmark --mark 0x40 -j CONNMARK --restore-mark
-A PREROUTING -t mangle -i ppp1 -m state --state NEW,RELATED,ESTABLISHED -d 10.200.21.9 -j CONNMARK --set-mark 0x50
-A PREROUTING -t mangle -i br0 -m connmark --mark 0x50 -j CONNMARK --restore-mark
-A PREROUTING -t mangle -i ppp2 -m state --state NEW,RELATED,ESTABLISHED -d 10.200.21.7 -j CONNMARK --set-mark 0x60
-A PREROUTING -t mangle -i br0 -m connmark --mark 0x60 -j CONNMARK --restore-mark
-A OUTPUT -t mangle -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
-A POSTROUTING -t nat -o eth0 -j MASQUERADE
-A POSTROUTING -t nat -o eth1 -j MASQUERADE
-A POSTROUTING -t nat -o ppp+ -j MASQUERADE
-A FORWARD -i br0 -o eth0 -j ACCEPT
-A FORWARD -i br0 -o eth1 -j ACCEPT
-A FORWARD -i br0 -o ppp+ -j ACCEPT
-A FORWARD -m state -i eth0 -o br0 --state ESTABLISHED,RELATED -j ACCEPT
-A FORWARD -m state -i eth1 -o br0 --state ESTABLISHED,RELATED -j ACCEPT
-A FORWARD -m state -i ppp+ -o br0 --state ESTABLISHED,RELATED -j ACCEPT
-A FORWARD -i br0 -o br0 -j ACCEPT