Routing through a Floating IP on Hetzner Cloud

Recently we started up a Hetzner Cloud server and wanted to have a static IP that we could whitelist. So we added a Floating IP to the server and set it up. That was great, all incoming traffic worked perfectly... But the outgoing traffic... Well, that still had the IP address associated with the server when it was started up (the IPv4 address you get when you specify you need public access on creation). I'm going to call this one the "Ephemeral IP".

So I went through a bit of a rabbit hole trying to figure out a way to route outgoing traffic through the Floating IP, not the Ephemeral IP since they were on the same interface so using standard routing rules wouldn't work.

It turns out, you need to Source NAT the traffic and specify the outgoing IP. Using iptables you would run the following command:

iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to xxx.xxx.xxx.xxx

This would have to be run as sudo and you'll need to adjust the name of the interface (In my case it was eth0) depending on the distro you chose.

Keep in mind that this doesn't persist when you reboot the machine. If you use iptables straight then check out this article: https://www.cyberciti.biz/faq/how-to-save-iptables-firewall-rules-permanently-on-linux/

I hope this helps!