1

I would like to restrict outbound traffic to only localhost using iptables. I already have a default DROP policy on OUTPUT and a rule REJECTing all traffic. I need to add a rule above that in the OUTPUT chain.

I have seen a couple different examples for this type of rule, the most common being:

-A OUTPUT -o lo -j ACCEPT

and

-A OUTPUT -o lo -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

Is there any reason to use the latter rather than the former? Can packets on lo have an address other than 127.0.0.1?

3 Answers 3

2

If your machine has multiple interfaces, and you try to communicate with the IP on one of these other interfaces, the traffic will actually go over the lo interface. Linux is smart enough to figure out this traffic is destined for itself, and not try to use the real interface.

The rule -A OUTPUT -o lo -j ACCEPT will allow this other traffic, while the rule -A OUTPUT -o lo -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT would reject it.

.

You can see everything the kernel will route over the loopback interface by running

ip route show type local table all

(just note the first value, which is either an IP or a network/mask)

2

Yes it can have other addresses. You can possibly see anything from 127.0.0.0/8 in-use. On Debian/Ubuntu system addresses in the 127.0.0.0/8 range are used to deal with an Apache issue.

Other addresses can also be assigned, but this is very rare.

0

I already have a default DROP policy on OUTPUT and a rule REJECTing all traffic

That makes no sense; they achieve the same end. The default policy to DROP is sufficient.

The rule you're looking for is probably

iptables -A OUTPUT -o lo -j ACCEPT

But some more experience with iptables will (hopefully) teach you not to do that; use input rules instead.

1
  • In this case since I'd prefer for the blocked outbound traffic to be REJECTed and not DROPped and since REJECT is not a valid default policy I have that rule. In any case, that part isn't relevant to my question regarding localhost. Mar 22, 2012 at 17:31

You must log in to answer this question.

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