6

I would like to learn how to setup my network card with two IP addresses

I tried as bellow, but now

network:
  version: 2
  renderer: NetworkManager
  ethernets:
     enp0s3:
        dhcp4: no
        dhcp6: no
        addresses: [ 192.168.0.2/32, 172.16.0.2/16 ]
        gateway4: 192.168.0.1
        gateway4: 172.16.0.1
        nameservers:
              search: [usp.br]
              addresses: [ 192.168.0.100, 192.168.0.102 ]
        optional: true

Each IP has its own gateway....

Could you please help me how to it?

Thank you

3 Answers 3

7

There are often issues with having multiple gateways set for interfaces. Routing packets over to one and then the other may lead to confusion on the network, so you're often best served by having a single default gateway unless you really know what you are doing.

In short, you usually want only one

gateway4: xx.xx.xx.xx

line in your config.

If you must have multiple routes for the individual IPs of the interface, you can control exactly how they will behave by writing static routes (and setting the metric based on the priority of the addresses), like so:

network:
  version: 2
  renderer: NetworkManager
  ethernets:
     enp0s3:
        dhcp4: no
        dhcp6: no
        addresses: [ 192.168.0.2/32, 172.16.0.2/16 ]
        nameservers:
              search: [usp.br]
              addresses: [ 192.168.0.100, 192.168.0.102 ]
        routes:
          - to: 0.0.0.0/0
            via: 192.168.0.1
          - to: 0.0.0.0/0
            via: 172.168.0.1

You also don't need optional: true: it only stops delaying boot if the interface isn't coming up fast enough, which shouldn't be an issue given that you specific static addresses. Adding optional: true also has the side effect of not blocking for some targets in systemd, which might mean a server will not start all the services it should right away if these services need the network.

1

A bit late, but... It makes little sense to have 2 default gateways. Think about it: If i do ping 8.8.8.8, what gateway should it use?

You CAN setup priority, if one line is slower/more expensive/only for backup/... If both have same priority, I suspect it will try to do round-robbin.

1

I recently ran into exactly the same problem. I saw the answer here (from Mathieu Trudel-Lapierre), but it doesn't really solve the problem. The netplan will issue an error that there are two default routes for one routing table. It's right to do so:

  1. To add the 800 table in routing tables:

    sudo vi /etc/iproute2/rt_tables
    

    To add there a line in the end:

    800 second_ip
    
  2. Your netplan configuration plan should be:

    network:
      version: 2
      renderer: NetworkManager
      ethernets:
         enp0s3:
            dhcp4: no
            dhcp6: no
            addresses: [ 192.168.0.2/32, 172.16.0.2/16 ]
            nameservers:
                  search: [usp.br]
                  addresses: [ 192.168.0.100, 192.168.0.102 ]
            routes:
                - to: 0.0.0.0/0
                  via: 192.168.0.1
                - to: 0.0.0.0/0
                  via: 172.16.0.1
                  table: 800
            routing-policy:
                - from: 172.16.0.2
                  table: 800
    

    It is important to note here that the first gateway (192.168.0.1) should be into the main table (that is, we do not specify routes/table:), and the second gateway go into the 800 table (routes/table: 800). And we have to use the routing-policy for the second IP address (172.16.0.2) - it is necessary that if packets come from this IP address, they will go exactly to the 800 table and will be sent to the second gateway (172.16.0.1).

    If you just run ping 8.8.8.8, then the server needs to understand from which IP address to send packets to it. In this case, it will search for the default route in the main table and find the gateway 192.168.0.1, and it will understand from which IP address to send packets by default - from 192.168.0.2. It is important that if there is no default route in the main table (for example, if I had defined the first default route in another table, for example 801) - you will not be able to initiate packets from the server (for example ping 8.8.8.8) - the server will only be able to respond to packets that came to its IP addresses from outside.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .