Tutorials:Routing multiple public ip addresses on a linux router

From Knowledge base
Jump to: navigation, search

Let say we have at least 3 connections.

One private, one public (default outgoing), and a second public.

This is a typical config /etc/network/interfaces:

auto lo
iface lo inet loopback

#private network
auto eth0
iface eth0 inet static
	post-up ip rule add to lookup main prio 1000
	post-down ip rule del to lookup main prio 1000

#default public network for all outgoing connection
auto eth1
iface eth1 inet static
	address YOUR_PUBLIC_IP_1
	broadcast YOUR_PUBLIC_IP_1
	post-up route add YOUR_ROUTER_IP dev eth1
	post-up route add default gw YOUR_ROUTER_IP
	dns-nameservers YOUR_DNS

auto eth2
iface eth2 inet static
	address YOUR_PUBLIC_IP_2
	broadcast YOUR_PUBLIC_IP_2
	post-up route add YOUR_ROUTER_IP dev eth2
	post-up ip route add default via YOUR_ROUTER_IP dev eth2 table public2
	post-up ip rule add from YOUR_PUBLIC_IP_2 lookup public22 prio 1002
	post-down ip rule del from YOUR_PUBLIC_IP_2 lookup public2 prio 1002

And you will need additional route table in /etc/iproute2/rt_tables:

# reserved values
255     local
254     main
253     default
0       unspec
# local
1 public2

So this config, create a second route table 'public2'. Any incoming connection that passes through the public IP 2 go out by this same IP.

By default, your network goes out by the default IP (public IP 1).

Share your opinion