Search code examples
debianiproute

How do I correctly use named tables with 'ip route'


I'm using this on my server to route any traffic to certain ip over a specific interface

ip route add 80.100.141.27/32 via 167.99.208.1 dev eth0 src 10.18.0.10

This works great, traffic to 80.100.141.27 will go over 10.18.0.10

Now to organise these rules I'm thinking I could use named tables.

First I create a new table called 'table-name'

    - name: create named ip route table
      lineinfile:
        path: /etc/iproute2/rt_tables
        line: '200 table-name'
        create: yes
      tags: ip_route

Then I can add rules as follows

ip route add table table-name 80.100.141.27/32 via 167.99.208.1 dev eth0 src 10.18.0.10

Rules are now added to this table

ip route show table table-name

However the rules are not active. Traffic does not go over the specified route.


Solution

  • According to the man page:

    the kernel only uses this table when calculating routes.

    speaking about the main table.

    So you probably have to indicate, from the main table, to use your other table, with something like:

    ip rule add from 167.99.208.1 table table-name