Ubuntu server: Multiple VLANS, Virtual Ports and Forwarding

830 views Asked by At

I have 4 IP addresses in 2 VLANS. I need to go out from Ubuntu server with different address:

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

All VLANS comes through one Ethernet cable in one NIC.

What I am doing:

1) Here is my /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) Create routing tables in /etc/iproute2/rt_tables. Add in the end of the file:

1 rt0
2 rt1

3) After it run the following commands:

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

When I try: curl --interface '175.176.95.119' ifconfig.co it works. Also when I try: curl --interface '175.176.95.120' ifconfig.co it works. But with address: 175.176.96.119 and 175.176.96.120 it doesnt work.

What can I do to be able to use 175.176.96.119 and 175.176.96.120? Thanks!

0

There are 0 answers