Restrict local port access to a specific user

access-controlfirewalliptablesnetworkingSecurity

I'm trying to restrict access to a particular port for a particular user on my Debian.

Let's say user's id is 1000 and port I would like to block is 5000.

I tried using iptables with the following command :

iptables -I OUTPUT -o lo -p tcp --dport 5000 --match owner --uid-owner 1000 -j DROP

It works if the user does curl 127.0.0.1:5000 or curl <machine_ip>:5000 but not if the user execute curl localhost:5000.

I don't understand why it's not working. I though localhost was converted to 127.0.0.1. What's the difference ?

In my /etc/hosts file, I have

127.0.0.1   localhost

# The following lines are desirable for IPv6 capable hosts
::1 localhost   ip6-localhost ip6-loopback

Best Answer

Do the same for IPv6 ... localhost resolves to both an IPv4 and IPv6 address, and v6 is preferred.

Edit 1:

ip6tables -I OUTPUT -o lo -p tcp --dport 5000 --match owner --uid-owner 1000 -j DROP
Related Question