Configuring Floating IPs as the source of outbound traffic

Configuring Floating IPs as the source of outbound traffic

Floating IP is a public accessible IP address that can be assigned to a dedicated server and then reassigned to another dedicated server as needed. It can be used to send outbound traffic or to implement a fail over mechanism. In this case, you are going to approach the outbound traffic use case. 

By default, when you assign a Floating IP to a dedicated server, the traffic originating from the server to the Internet does not use the floating IP as source address.

In Linux, your default gateway always uses the first IP address of the public network interface.

So this guide describes how you can (re)configure your operating system to use the Floating IP as the source address for outbound traffic in three different operating systems:

To check which IP is used as the source for outbound traffic, you can use a service like ifconfig.co 

$ curl -4 ifconfig.co
212.32.230.67

In this example, 212.32.230.67 is the public IP of the dedicated server.

CentOS 7

Check the current Outbound IP

By running the following command you are able to get the current Outbound IP of your dedicated server:

# curl -4 https://ifconfig.co/
212.32.230.67

Where 212.32.230.67 is the public IP of the dedicated server.

Add the Floating IP to the machine

Check which interface of the dedicated server has the Public IP by running the following command:

# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp2s0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e6 brd ff:ff:ff:ff:ff:ff
    inet 212.32.230.67/26 brd 212.32.230.127 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet6 fe80::2a92:4aff:fe33:48e6/64 scope link 
       valid_lft forever preferred_lft forever
3: eno1:  mtu 9000 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e7 brd ff:ff:ff:ff:ff:ff
    inet 10.32.18.210/27 brd 10.32.18.223 scope global dynamic eno1
       valid_lft 12307sec preferred_lft 12307sec
    inet6 fe80::2a92:4aff:fe33:48e7/64 scope link 
       valid_lft forever preferred_lft forever

You can see that the Public IP 212.32.230.67 of the dedicated server is in the interface enp2s0. So you need to edit the interface by running the following command:

# vi /etc/sysconfig/network-scripts/ifcfg-enp2s0

Ensure the content is like this:

DEVICE=enp2s0
BOOTPROTO=none
ONBOOT=yes
PREFIX=26
IPADDR=212.32.230.67
#GATEWAY=212.32.230.126
DOMAIN=dedi.leaseweb.net
DNS1=23.19.53.53
DNS2=23.19.52.52
#DEFROUTE=yes

You then need to add the Floating IP to the interface by creating the ifcfg-enp2s0:1 file via:

# vi /etc/sysconfig/network-scripts/ifcfg-enp2s0:1

with the following content:

BOOTPROTO=static
DEVICE=enp2s0:1
IPADDR=
PREFIX=32
TYPE=Ethernet
USERCTL=no
ONBOOT=yes

Don’t forget to replace with your floating IP like E.F.G.H.

After that we need to add the route to use the Floating IP instead of the Public IP of the dedicated server by creating the following file via:

# vi /etc/sysconfig/network-scripts/route-enp2s0

with the following content:

default via  dev enp2s0 src 

Don’t forget to replace with the GATEWAY value that was commented in the first edited file and for the Floating IP.

So the last step is to apply the configuration that we have changed by running the following command:

# systemctl restart network

Note: Please keep in mind that it would take some seconds before the connection gets established again.

Check if the IP was added to the interface successfully

# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp2s0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e6 brd ff:ff:ff:ff:ff:ff
    inet 212.32.230.67/26 brd 212.32.230.127 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet /32 brd  scope global enp2s0:1
       valid_lft forever preferred_lft forever
    inet6 fe80::2a92:4aff:fe33:48e6/64 scope link 
       valid_lft forever preferred_lft forever
3: eno1:  mtu 9000 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e7 brd ff:ff:ff:ff:ff:ff
    inet 10.32.18.210/27 brd 10.32.18.223 scope global dynamic eno1
       valid_lft 84397sec preferred_lft 84397sec
    inet6 fe80::2a92:4aff:fe33:48e7/64 scope link 
       valid_lft forever preferred_lft forever

In the example above, the was added to the interface eno0 where our public IP of the machine 212.32.230.67 is already assigned to.

Check if the Floating IP is the new Outbound IP

By running the following command, you should see the Floating IP instead of the Public IP of the dedicated server:

# curl -4 https://ifconfig.co/


Where is your desired Floating IP.

Debian 12

Check the current Outbound IP

By running the following command, you are able to get the current Outbound IP of your dedicated server:

# curl -4 https://ifconfig.co/
212.32.230.67

Where 212.32.230.67 is the public IP of the dedicated server.

Add the Floating IP to the machine

Check which interface of the dedicated server has the Public IP by running the following command:

# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e6 brd ff:ff:ff:ff:ff:ff
    altname enp2s0
    inet 212.32.230.67/26 brd 212.32.230.127 scope global eno0
       valid_lft forever preferred_lft forever
    inet6 fe80::2a92:4aff:fe33:48e6/64 scope link 
       valid_lft forever preferred_lft forever
3: eno1:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e7 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 10.32.18.210/27 brd 10.32.18.223 scope global dynamic eno1
       valid_lft 85332sec preferred_lft 85332sec
    inet6 fe80::2a92:4aff:fe33:48e7/64 scope link 
       valid_lft forever preferred_lft forever

We can see that the Public IP 212.32.230.67 of the dedicated server is in the interface eno0. So you need to edit the interface by adding the following block of code by running the following command:

# vi /etc/network/interfaces

Ensure the content is like this:

```bash
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

auto eno0
iface eno0 inet static
        address 212.32.230.67
        netmask 255.255.255.192
        network 212.32.230.64
        broadcast 212.32.230.127
        gateway 212.32.230.126
        dns-search dedi.leaseweb.net
        dns-nameservers 23.19.53.53 23.19.52.52

auto eno1
iface eno1 inet dhcp
        mtu 9000

Add the following block of code after the eno0 interface definition:

auto eno0:1
iface eno0:1 inet static
        address 
        netmask 32
        up ip route replace default via  dev eno0 src 

Don’t forget to replace with the gateway value that is in the eno0 definition block and for the Floating IP.

So the last step is to apply the configuration that we have changed by running the following command:

# systemctl restart networking

Note: Please keep in mind that it would take some seconds before the connection gets established again.

Check if the IP was added to the interface successfully

# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e6 brd ff:ff:ff:ff:ff:ff
    altname enp2s0
    inet 212.32.230.67/26 brd 212.32.230.127 scope global eno0
       valid_lft forever preferred_lft forever
    inet /32 scope global eno0:1
       valid_lft forever preferred_lft forever
    inet6 fe80::2a92:4aff:fe33:48e6/64 scope link 
       valid_lft forever preferred_lft forever
3: eno1:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e7 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 10.32.18.210/27 brd 10.32.18.223 scope global dynamic eno1
       valid_lft 86395sec preferred_lft 86395sec
    inet6 fe80::2a92:4aff:fe33:48e7/64 scope link 
       valid_lft forever preferred_lft forever

In the example above, the was added to the interface eno0 where our public IP of the machine 212.32.230.67 is already assigned to.

Check if the Floating IP is the new Outbound IP

By running the following command, you should see the Floating IP instead of the Public IP of the dedicated server:

# curl -4 https://ifconfig.co/


Where is your desired Floating IP.

Ubuntu 22.04

Check the current Outbound IP

By running the following command, you are able to get the current Outbound IP of your dedicated server:

# curl -4 https://ifconfig.co/
212.32.230.67

Where 212.32.230.67 is the public IP of the dedicated server.

Add the Floating IP to the machine

# vi /etc/netplan/01-netcfg.yaml

With the following content:

---
network:
  version: 2
  renderer: networkd
  ethernets:
    eno0:
      dhcp4: no
      dhcp6: no
      addresses: 
        - A.B.C.D
        - 
      routes:
        - to: default
          via: 212.32.230.126
          from: 
          metric: 100
          on-link: true
      nameservers:
        search: [dedi.leaseweb.net]
        addresses: [23.19.53.53, 23.19.52.52]

    eno1:
      dhcp4: yes
      mtu: 9000

Do not forget to replace with your Floating IP.

After that, you need to apply the configuration that you have changed by running the following command:

# netplan apply

Check if the IP was added to the interface successfully

# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e6 brd ff:ff:ff:ff:ff:ff
    altname enp2s0
    inet A.B.C.D/26 brd 212.32.230.127 scope global eno0
       valid_lft forever preferred_lft forever
    inet  scope global eno0
       valid_lft forever preferred_lft forever
    inet6 fe80::2a92:4aff:fe33:48e6/64 scope link 
       valid_lft forever preferred_lft forever
3: eno1:  mtu 9000 qdisc fq_codel state UP group default qlen 1000
    link/ether 28:92:4a:33:48:e7 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 10.32.18.210/27 metric 100 brd 10.32.18.223 scope global dynamic eno1
       valid_lft 86181sec preferred_lft 86181sec
    inet6 fe80::2a92:4aff:fe33:48e7/64 scope link 
       valid_lft forever preferred_lft forever

In the example above, the was added to the interface eno0 where our public IP of the machine A.B.C.D is assigned to.

Check if the Floating IP is the new Outbound IP

By running the following command, you should see the Floating IP instead of the Public IP of the dedicated server:

# curl -4 https://ifconfig.co/



Where is your desired Floating IP.