Configuring Floating IPs as the source of outbound traffic

Configuring Floating IPs as the source of outbound traffic

A floating IP is a publicly 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 the source address. In Linux, your default gateway always uses the first IP address of the public network interface.

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

  1. By running the following command you can 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

  1. 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
  1. You can see that the Public IP 212.32.230.67 of the Dedicated Server is in the interface enp2s0. You need to edit the interface by running the following command:
# vi /etc/sysconfig/network-scripts/ifcfg-enp2s0
  1. 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
  1. 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
  1. With the following content:
BOOTPROTO=static
DEVICE=enp2s0:1
IPADDR=<floating-ip>
PREFIX=32
TYPE=Ethernet
USERCTL=no
ONBOOT=yes
  1. Do not forget to replace <floating-ip> with your Floating IP like E.F.G.H.
  1. After that 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
  1. With the following content:
default via  dev enp2s0 src <floating-ip>
  1. Do not forget to replace it with the GATEWAY value that was commented on in the first edited file and <floating-ip> for the Floating IP.
  1. The last step is to apply the configuration that we have changed by running the following command:
# systemctl restart network

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

  1. Use 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
    inet <floating-ip>/32 brd <floating-ip> 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 <floating-ip> 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

  1. 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/
<floating-ip> Where <floating-ip> is your desired Floating IP. 

Debian 12

Check the current Outbound IP

  1. By running the following command, you can 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

  1. 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
  1. We can see that the Public IP 212.32.230.67 of the Dedicated Server is in the interface eno0. You need to edit the interface by adding the following block of code, by running the following command:
# vi /etc/network/interfaces
  1. 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
  1. Add the following block of code after the eno0 interface definition:
auto eno0:1
iface eno0:1 inet static
        address <floating-ip>
        netmask 32
        up ip route replace default via  dev eno0 src <floating-ip>
  1. Do not forget to replace <floating-ip> with the gateway value that is in the eno0 definition block and for the Floating IP.
  1. The last step is to apply the configuration that we have changed by running the following command:
# systemctl restart networking

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

  1. Use 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
    inet <floating-ip>/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 <floating-ip> 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

  1. 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/
<floating-ip> Where <floating-ip> is your desired Floating IP. 

Ubuntu 22.04

Check the current Outbound IP

  1. By running the following command, you can 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

  1. Use the following command:
# vi /etc/netplan/01-netcfg.yaml
  1. With the following content:
---
network:
  version: 2
  renderer: networkd
  ethernets:
    eno0:
      dhcp4: no
      dhcp6: no
      addresses: 
        - A.B.C.D
        - <floating-ip> routes: - to: default via: 212.32.230.126 from: <floating-ip> metric: 100 on-link: true nameservers: search: [dedi.leaseweb.net] addresses: [23.19.53.53, 23.19.52.52] eno1: dhcp4: yes mtu: 9000 
  1. Do not forget to replace <floating-ip> with your Floating IP.
  1. 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

  1. Use 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 A.B.C.D/26 brd 212.32.230.127 scope global eno0
       valid_lft forever preferred_lft forever
    inet <floating-ip> 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 <floating-ip> 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

  1. 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/
<floating-ip> 
  • Where <floating-ip> is your desired Floating IP.