14

I was wondering if someone could help me with the following iptables rule:

We would like to allow ANY and ALL locally originating (as in, on the server running iptables) traffic.

DNS, HTTP, etc... all of it. Any connection initiated by the server running iptables should be allowed.

Currently we are using basically OUTPUT default policy, ACCEPT. Is this correct? Inputs are blocked, so I am assuming this means that the connections (except those we allow) cannot be started because they will be dropped before our side can hit the OUTPUT policy?

Sorry, my iptables skills are weak ;)

Thank you kindly.

2 Answers 2

30

You need two rules to do that:

iptables -I OUTPUT -o eth0 -d 0.0.0.0/0 -j ACCEPT
iptables -I INPUT -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT

Some notes.

  • Preexisting rules that you may have may do this already, but look different.
  • This uses -I to force these rules to be first. iptables rules are evaluated top down.
  • The -o and -i flags mean "out" and "in" respectively. Replace eth0 with the proper ethernet interface name.
5
  • we have both :) i failed to mention the 2nd one because like i said... my iptables skills are weak sauce. thanks for the clarification :) Sep 18, 2012 at 11:43
  • 5
    -d 0.0.0.0/0 is redundant and not needed.
    – zapstar
    Sep 20, 2018 at 10:41
  • Great answer. But now I am able to ping the server, and I don't understand why. Can you explain?
    – Daniel
    Feb 22, 2019 at 6:07
  • @Daniel Because if you only allow outbound traffic, then the ping reply will get dropped by the packet filter when it reaches your host. To prevent this, you also need to allow incoming traffic that is associated with the same session as, or is related to, any outgoing traffic. In other words, because the ping reply is related to the ping request, it should be permitted. If there is an incoming ping request and no outgoing ping recorded in the session table then the incoming packet would be dropped. Hope that makes sense!
    – bahamat
    Feb 25, 2019 at 20:02
  • @bahamat: After I tried again, I found I need to allow icmp traffic explicitly. And I was speaking about being able to ping the server, where I implemented the rules above. Cannot reproduce it btw., maybe I made a mistake somewhere. Thanks anyway.
    – Daniel
    Feb 27, 2019 at 6:44
0

Currently we are using basically OUTPUT default policy, ACCEPT.

That's enough for OUTPUT because Netfilter doesn't need special rules to start stateful connections tracking.

But if you want to filter out inbound traffic according to "default deny" policy it can be done with switching INPUT-chain to DROP: iptables -P INPUT DROP

Afterwards it all would be set with just 2 rules:

iptables -A INPUT -j ACCEPT -i lo
iptables -A INPUT -j ACCEPT -m state --state RELATED,ESTABLISHED

Pay your attention to the rule allowing input traffic on loopback interface — as I pointed out in my blog posting "Minimal firewall for end user", unless allowed explicitly, loopback traffic won't be handled by "established" state checking, compared to return traffic over, say, eth0.

To ensure this minimal ruleset is loaded "as is" w/o interfering with rules that already might be there, it's convenient to make use of iptables-restore in SHELL-session:

lptables-restore <<__EOF__
-P INPUT DROP
-A INPUT -j ACCEPT -i lo
-A INPUT -j ACCEPT -m state --state RELATED,ESTABLISHED
__EOF__

Before doing that make sure you won't cut your own networking connection1, although already open SSH sessions should continue to work normally, the attempts to open new ones won't work.

__

  1. Of course you can add other rules to allow such connections. It can be as simple as just -A INPUT -j ACCEPT -p tcp --dport 22 — no need to tinker with -m state here. Also don't forget to fix lptables-restore back to iptables-restore before trying it out ;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .