Question

iptables not blocking ports

For some reason, iptables isn’t blocking ports on a droplet, when the exact same rules work fine on a VirtualBox VM. The output from iptables -S is:

root@public:~# iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -j DROP

This is modelled exactly on the tutorial at https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-using-iptables-on-ubuntu-14-04, with a small change to the conntrack line due to Ansible’s iptables module kind of doing that whether I liked it or not.

But that’s not a helpful explanation, since the behaviour of the exact same rule set on Debian 8 is so very different.

Any ideas why this doesn’t block on a droplet, i.e., even with these iptables in place, a telnet command to port 554 will connect.


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.

@stuart250691

I’ve ran in to cases where not setting a default policy results in some connections being allowed.

When I setup iptables or ufw, I always set the default policy to deny that way it’s the first rule in my set. That basically says deny everything except what I explicitly allow.

With iptables you can do this by running:

sudo iptables -P INPUT DROP

Though you’d need to remove that last rule in your set first (i.e. -A INPUT -j DROP).

With ufw, it’d be:

sudo ufw default deny

I tend to prefer ufw over directly using iptables as the commands and arguments are a little more straight-forward than they are with iptables (and it’s available on both Ubuntu and Debian).

For example, allowing connections on ports 22, 80, and 443, with ufw, is achieved using:

sudo ufw allow 22/tcp \
&& sudo ufw allow 80/tcp \
&& sudo ufw allow 443/tcp

or by setting the default policy first:

sudo ufw default deny \
&& sudo ufw allow 22/tcp \
&& sudo ufw allow 80/tcp \
&& sudo ufw allow 443/tcp

My main reason for using ufw, other than the fact that it’s faster to set rules, is the fact that the rules are not ephemeral as they are with iptables (unless you install another package to make them stick).

So when the web server is restarted, ufw will maintain my rules, whereas without another package, iptables will not.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel