Skip to content

Instantly share code, notes, and snippets.

@sparkcodeuk
Created March 2, 2018 21:47
Show Gist options
  • Save sparkcodeuk/7715387829935b3c699892e926b931eb to your computer and use it in GitHub Desktop.
Save sparkcodeuk/7715387829935b3c699892e926b931eb to your computer and use it in GitHub Desktop.
Digital Ocean floating IP gateway script (force droplet to use the assigned floating IP for outbound traffic as well as inbound traffic)
#!/bin/bash
# Force outbound traffic through the attached floating IP
NET_INT="eth0"
CURL_TIMEOUT=3
echo -n "Setting floating IP as the default gateway: "
# Check there's a floating IP attached to this droplet
if [ "$(curl -s --connect-timeout $CURL_TIMEOUT http://169.254.169.254/metadata/v1/floating_ip/ipv4/active)" != "true" ]; then
echo "Error: this droplet doesn't have a floating IP assigned to it."
exit 1
fi
# Get the gateway IP for the floating IP
GATEWAY_IP=$(curl -s --connect-timeout $CURL_TIMEOUT http://169.254.169.254/metadata/v1/interfaces/public/0/anchor_ipv4/gateway)
if [ -z "$GATEWAY_IP" ]; then
echo "Error: failed getting gateway IP for this droplet."
exit 1
fi
# Check we haven't already got the floating IP as a default gateway
if [ ! -z $(ip route ls 0/0|awk '{print $3}'|grep "$GATEWAY_IP") ]; then
echo "Error: gateway IP already a default route."
exit 1
fi
# Add the new route before we remove any
sudo route add default gw $GATEWAY_IP $NET_INT
# Delete any other default gatways for this interface
ip route ls 0/0 dev $NET_INT|awk '{print $3}'|grep -v "$GATEWAY_IP"|xargs -n1 -I{} sudo route del default gw {}
echo "Done."
@hriad
Copy link

hriad commented Oct 16, 2018

Awesome. Thank you

@philross88
Copy link

Does this script still work? I can't seem to make it work on ubuntu 16.04
When I run the script (basically remove the old route), I loose the connection with the VPC and I have to reboot it and connect via old route.

@sparkcodeuk
Copy link
Author

Does this script still work? I can't seem to make it work on ubuntu 16.04
When I run the script (basically remove the old route), I loose the connection with the VPC and I have to reboot it and connect via old route.

Hi Phil, it's a while since I used, I'll try and find some time today/tomorrow on an Ubuntu instance to see whether I also have an issue with it.

@ufukty
Copy link

ufukty commented Dec 30, 2019

Line #30 looks like outdated.
Change it with:

sudo ip route replace default via $GATEWAY_IP dev $NET_INT

Works on ubuntu 18.04.3
And, since its replace, not add; you don't have to remove the old gateway separately.
Thank you for the code btw.

@mkjmdski
Copy link

This literally has saved my day! Confirmed to work on CentOS 7.6 with the update of @ufukty

@ricardomm85
Copy link

First thanks to @ufukty and @sparkcodeuk!

Second, is a way to revert this?

@Sugavanas
Copy link

First thanks to @ufukty and @sparkcodeuk!

Second, is a way to revert this?

I made a script to revert this. It works on Ubuntu 20.04 for me. You can find it here.

@stas-sl
Copy link

stas-sl commented Aug 12, 2020

It seems that do-agent stops sending metrics to the backend after this

https://www.digitalocean.com/community/questions/firewall-prevents-do-agent-from-pushing-monitoring-data

@quentinadam
Copy link

It does create issues with the do-agent monitoring agent, which can be solved by adding a back a route for 169.254.169.254 via the original gateway :

ip route add 169.254.169.254 via [original_gateway_ip] dev eth0

@slonia
Copy link

slonia commented Nov 11, 2021

After running this script my ssh session is frozen and I cannot access server via static ip. Also my domains that should resolve to static ip doesn't work. So seems inbound traffic is also affected. Or I'm doing it wrong?

@ufukty
Copy link

ufukty commented Nov 11, 2021

@slonia Did you check your firewall settings? And try to use new ip address when connecting to server with SSH again.

@slonia
Copy link

slonia commented Nov 12, 2021

@ufukty I'm looking for setup to make some app do requests from floating ip, while my static sites and other apps are using static ip

@edwardselby
Copy link

epic, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment