1

我在 2 个 VLAN 中有 4 个 IP 地址。我需要使用不同的地址从 Ubuntu 服务器出去:

curl --interface '<IP ADDRESS>' ifconfig.co

所有 VLAN 都通过一个 NIC 中的一根以太网电缆。

我在做什么:

1)这是我的 /etc/network/interfaces

auto eno1.155
iface eno1.155 inet static
    address 175.176.95.119
    netmask 255.255.255.0
    gateway 175.176.95.1
    dns-nameservers 175.176.90.254 175.176.90.253
    vlan-raw-device eno1

auto eno1.155:0
iface eno1.155:0 inet static
    address 175.176.95.120
    netmask 255.255.255.0
    gateway 175.176.95.1
    dns-nameservers 175.176.90.254 175.176.90.253
    vlan-raw-device eno1

auto eno1.156
iface eno1.156 inet static
    address 175.176.96.119
    netmask 255.255.255.0
    gateway 175.176.96.1
    dns-nameservers 175.176.90.254 175.176.90.253
    vlan-raw-device eno1

auto eno1.156:0
iface eno1.156:0 inet static
    address 175.176.96.120
    netmask 255.255.255.0
    gateway 175.176.96.1
    dns-nameservers 175.176.90.254 175.176.90.253
    vlan-raw-device eno1

2) 在/etc/iproute2/rt_tables 中创建路由表。在文件末尾添加:

1 rt0
2 rt1

3)运行以下命令后:

sysctl -w net.ipv4.conf.eno1.rp_filter=0
sysctl -w net.ipv4.conf.tun0.rp_filter=0
sysctl -w net.ipv4.conf.all.rp_filter=0
sysctl -w net.ipv4.conf.default.rp_filter=0
sysctl -w net.ipv4.conf.lo.rp_filter=0

sysctl -w net.ipv4.conf.all.forwarding=1
sysctl -w net.ipv4.conf.default.forwarding=1
sysctl -w net.ipv4.conf.eno1.forwarding=1
sysctl -w net.ipv4.conf.lo.forwarding=1
sysctl -w net.ipv4.conf.tun0.forwarding=1

sysctl -w net.ipv6.conf.all.forwarding=1
sysctl -w net.ipv6.conf.default.forwarding=1
sysctl -w net.ipv6.conf.eno1.forwarding=1
sysctl -w net.ipv6.conf.lo.forwarding=1
sysctl -w net.ipv6.conf.tun0.forwarding=1

sysctl -w net.ipv4.tcp_fwmark_accept=1

iptables --table nat --append POSTROUTING -j MASQUERADE

iptables -t mangle -A OUTPUT -s 175.176.95.0/24 -j MARK --set-mark 1
iptables -t mangle -A OUTPUT -s 175.176.96.0/24 -j MARK --set-mark 2

iptables -t nat -I POSTROUTING -o eno1 -s 175.176.95.119 -j SNAT --to-source 175.176.95.119
iptables -t nat -I POSTROUTING -o eno1 -s 175.176.95.120 -j SNAT --to-source 175.176.95.120

iptables -t nat -I POSTROUTING -o eno1 -s 175.176.96.119 -j SNAT --to-source 175.176.96.119
iptables -t nat -I POSTROUTING -o eno1 -s 175.176.96.120 -j SNAT --to-source 175.176.96.120

ip route add 175.176.95.0/24 dev eno1.155 src 175.176.95.119 table rt0
ip route add default via 175.176.95.1 dev eno1.155 table rt0

ip route add 175.176.96.0/24 dev eno1.156 src 175.176.96.119 table rt1
ip route add default via 175.176.96.1 dev eno1.156 table rt1

ip rule add from all fwmark 1 lookup rt0
ip rule add from all fwmark 2 lookup rt1

当我尝试时: curl --interface '175.176.95.119' ifconfig.co 它可以工作。另外,当我尝试: curl --interface '175.176.95.120' ifconfig.co 它可以工作。但是地址:175.176.96.119 和 175.176.96.120 它不起作用。

我该怎么做才能使用 175.176.96.119 和 175.176.96.120?谢谢!

4

0 回答 0