how to setup source based (policy based) routing on debian / ubuntu

3.7
(7)

Content: how to make the computer respond on the Internet to all its IP addresses on all its interfaces, each of which has a default gateway. It applies to both servers and desktops.

Summary:
Configure the iproute2 utility using the config (all of a sudden, it has a config!) – give names to three routing tables
Set up routes in three routing tables – more precisely, set default routes
Specify the rules by which traffic will be distributed across three route tables

In our example, we will have 2 interfaces. Which are called ens33 and ens192.

Gateway 1 ip address – 192.168.1.1

Gateway 2 ipaddress – 192.168.2.1,

server interface ens33 ip – 192.168.1.10

server interface ens192 ip – 192.168.2.10

  1. Create routing tables.
# echo 100 ens33-route >>/etc/iproute2/rt_tables
# echo 101 ens192-route >>/etc/iproute2/rt_tables

2. We set default gateways for each interface

ip route add default via 192.168.2.1 dev ens192 table ens192-route
ip route add default via 192.168.1.1 dev ens33 table ens33-route

3. Set ip rules

ip rule  add from 192.168.2.10 lookup ens192-route
ip rule  add from 192.168.1.10 lookup ens33-route

Now our server responds to the interface from which the request comes. Even without rebooting the system

If you want this to work after a system reboot, do the following:

Open /etc/network/interfaces . Then config:

iface ens192 inet static
   address 192.168.2.10
   netmask 255.255.255.0
   post-up ip route add default via 192.168.2.1 dev ens192 table ens192-route
   post-up ip rule  add from 192.168.2.10 lookup ens192-route


iface ens33 inet static
   address 192.168.1.10
   netmask 255.255.255.0
   post-up ip route add default via 192.168.1.1 dev ens33 table ens33-route
   post-up ip rule  add from 192.168.1.10 lookup ens33-route

Save and reboot.

Similar Posts:

8,359

How useful was this post?

Click on a star to rate it!

Average rating 3.7 / 5. Vote count: 7

No votes so far! Be the first to rate this post.

Scroll to Top