73

I have struggled throughout the years to get a solid understanding on iptables. Any time I try and read through the man pages my eyes start to glaze over.

I have a service that I only want to allow the localhost to have access to.

What terms (or configuration, if someone is feeling generous) should I Google for to allow only localhost host to have access to a given port?

iptablessuck
  • 733
  • 1
  • 6
  • 4

3 Answers3

82

If by service you mean a specific port, then the following two lines should work. Change the "25" to whatever port you're trying to restrict.

iptables -A INPUT -p tcp -s localhost --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --dport 25 -j DROP
Hyppy
  • 15,608
  • 1
  • 38
  • 59
35

I'd recommend:

iptables -A INPUT -i lo -p tcp --dport $APP_PORT -j ACCEPT
iptables -A INPUT -p tcp --dport $APP_PORT -j DROP

Because, self-addressed packets do not necessarily have 127.0.0.1 as its source, but they all 'enter' from the lo interface.

Now, if you really want to understand iptables the first thing you should do is to download and print good diagrams explaining the relations of the netfilter tables. Here are two great ones:

Finally, read a lot of iptables HOWTO's. The practical examples would help you get up-to-speed real quick :)

pepoluan
  • 5,038
  • 4
  • 47
  • 72
  • ty! `lo` shows up for me after using these commands with the last command from this link http://www.cyberciti.biz/faq/howto-display-linux-iptables-loaded-rules/ `iptables -L -v -n --line-numbers` –  Aug 25 '13 at 03:01
  • 2
    @Gracchus I find it much easier to use `iptables-save` , save the output to a file, edit it with `vim` or `emacs`, and reimport the edited file using `iptables-apply` – pepoluan Aug 25 '13 at 17:16
  • I think depending on the use case, the second rule should explicitly REJECT instead of drop silently. Is it dropped silently as a best practice? Is REJECT safe to use? – Mr. Doomsbuster Dec 31 '15 at 05:25
  • 2
    @tech-pro Yes, REJECT is safe to use. It depends on what you are trying to accomplish and whether you want to be courteous to people trying to use the port. REJECT will send back a TCP RST packet telling the client that the machine is up but the port is closed. If you are closing a port that people might legitimately expect to use, then REJECT is good. If you expect only port scanners then DROP is better. – Law29 Jan 05 '16 at 07:17
  • I had a web server running on the port that I want be accessible only via local host. In other to deny immediately to someone who uses from the browser I thought REJECT might be better. If it's dropped the browser hangs on till the timeout occurs. Does reject make sense in this use case? – Mr. Doomsbuster Jan 05 '16 at 13:31
  • @TechPro depends on whether you want to annoy people accessing that port or not :-) ... for ports that are not open to the public, only to localhost (by design), I put in `DROP`. So someone who's being nosy will have a practical joke played upon them. (Does not deter determined crackers, though, but a good lesson for too-nosy employees.) :D – pepoluan Jun 30 '16 at 07:42
  • 1
    @pepoluan Can you provide an example or two when self-addressed packets don't have `127.0.0.1` as their source address? – x-yuri Mar 14 '18 at 22:46
  • 2
    @x-yuri For example, I run `unbound` as DNS Cache, in front of `dnscrypt-proxy`. `unbound` binds to `127.0.53.1:53`, and `dnscrypt-proxy` binds to `127.0.53.2:53`. When an app requests either unbound or dnscrypt-proxy to resolve an FQDN, guess what source address will unbound/dnscrypt-proxy responds from? – pepoluan Apr 01 '18 at 08:50
0

I had a similar problem. I configured iptables to deny incoming requests from all ports except the ones I specifically want to allow. I didn't bother to allow 27107 because I mistakenly reasoned that iptables affects only traffic from other hosts, and I don't need to expose this instance of mongodb to the outside world.

I was wrong about iptables. When I added this rule, it worked again:

-A INPUT -p tcp -m tcp -s localhost --dport 27017 -j ACCEPT

This tells netfilter to accept incoming traffic to port 27017 as long as it's from the local host.

If I want to access this instance of mongo from a different host (such as my laptop), I can still do so with an IP tunnel.