Assign each NIC an ip range or subnet

I have a manjaro linux server with at least two network interface controllers (NICs) and would like to route all traffic going to certain IP address ranges or subnets over one, and everything else over one of the others.

So far I've configured the networks via nmcli, but I only have a tenuous grasp of the subject.

Ideally the solution would work via the command line, be persistent and not depend strongly on the distribution used.

Edit: As per Route IP Traffic using nmcli / Centos 7 something along the lines of

nmcli connection modify "Wired connection 1" +ipv4.routes "10.0.0.0/16"

should work, however the packages don't seem to be routed across the correct network.


Solution 1:

General Pre-Information about this Answer

I assume my Answers in Generally on my Knowledge based on Debian Systems, however in this part I skip the Network-Manager Part due I am not using it anyway and in my personal mind makes it only complicated.

You Asked, and in the Comments it seems to be nearly clear, that you want to route the 10.0.0.0/16 to another network card.

You Provided "Wired connection 1" +ipv4.routes "10.0.0.0/16"

-- which device name should the right one, can be found out via ip address or ifconfig -- Usually you unsure to it, unplug the cards cable and look to dmesg or /var/log/syslog

So in My Example, assume the Following:

  • "Wired connection 1" = eth0
  • "10.0.0.0/16" or 10.0.0.0/255.255.0.0 (or 10.0.0.0-10.0.255.255) should be routed to eth0

The Short way for testing or pre-set, not Persistent:

ip route add 10.0.0.0/16 dev eth0

The Tricky Part for now is, getting it Persistent.

On RHEL/CentOS/Fedora/Scientific based Systems,

  • edit /etc/sysconfig/network-scripts/route-eth0

Adding here

 10.0.0.0/16 via 10.0.0.1

Where the First is the Network you want to match, second the IP of the Router. In My example, 10.0.0.1

Using the Debian Way. (Debian Bases Systems, Like Ubuntu)

on some Systems, /etc/network/interfaces may seem to be empty. In that case, you have to look into vi /etc/network/interfaces.d, but we assume the basic /etc/network/interfaces file to be used.

  • edit /etc/network/interfaces

Look for your Interface eth0

iface eth0 inet static
        address  10.0.0.2
        netmask  255.0.0.0

Modify, that it looks like

iface eth0 inet static
        address  10.0.0.2
        netmask  255.0.0.0
###EITHER with Gateway
#post-up route add -net 10.0.0.0 netmask 255.255.0.0 gw 10.0.0.1 dev eth0
#post-down route del -net 10.0.0.0 netmask 255.255.0.0 gw 10.0.0.1 dev eth0

###or without Gateway
#post-up route add -net 10.0.0.0 netmask 255.255.0.0 dev eth0
#post-down route del -net 10.0.0.0 netmask 255.255.0.0 dev eth0

The Generic, Static Way (Might be outdated on Systemd enabled systems)

Edit /etc/rc.d/rc.local or /etc/rc.local

add

  • ip route add 10.0.0.0/16 dev eth0

This answer is meaned to solve it in generally.