Disable incoming requests on Public IP

Hi guys,

Set up a droplet having the private networking option enabled. My aim is to:

  1. Keep internet available on the droplet (so killing eth0 interface is not an option).
  2. Block all incoming traffic from the internet.
  3. Communicate with the droplet using the private IP (from another droplet).

I tried both these iptables (flipped order between two options), to absolutely no avail:

:OUTPUT ACCEPT [255:30170]
-A FORWARD -i eth0 -j DROP


:OUTPUT ACCEPT [255:30170]
-A FORWARD -i eth0 -j DROP

Testing the changes this way: Go on another droplet, and ssh using both of these: ssh username@targetpublicip and ssh username@targetprivateip

In both cases, both keep working fine.

When I tried just this in the iptables: iptables -I INPUT -i eth0 -j DROP

I couldn’t SSH using the private IP either.

I’m using the ams2 datacentre for both droplets.

Thanks and regards

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

First clear the existing iptables rules by running these commands

iptables -F
iptables -X

After you clean you can view the rules with

iptables -L

Then run in terminal the following rules

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

allow loopback device, some software requires it

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

allow ssh connection

iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

replace x.x.x.x with the droplet ip you want to allow connections with, this is for ipv4

iptables -A INPUT -s x.x.x.x -j ACCEPT
iptables -A OUTPUT -d x.x.x.x -j ACCEPT

Note: Save the iptables rules before restarting your droplet so they don’t get lost