I know about changing owners/permissions on particular programs/files, but how can I control which users have access to the network card (NIC) or have the power to create network sockets?
2 Answers
As described here, you can block all Internet access for certain users using this iptables
command:
sudo iptables -A OUTPUT -m owner --uid-owner {USERNAME} -j REJECT
sudo ip6tables -A OUTPUT -m owner --uid-owner {USERNAME} -j REJECT
If you want this command to run automatically when the system starts up, you should add it to the end of your /etc/rc.local
file.
While this won't make it impossible for those users to create sockets, it will block all outgoing traffic (like a firewall).

- 327
- 2
- 11

- 4,231
If firewalld is part of your setup, you can block network access for a specific user using a direct rule, es:
/etc/firewalld/direct.xml
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
<?xml version="1.0" encoding="utf-8"?>
<direct>
<chain ipv="ipv4"
table="filter"
chain="restrict_user_ipv4"/>
<rule ipv="ipv4"
table="filter"
chain="OUTPUT"
priority="1">-m owner --uid-owner user -j restrict_user_ipv4</rule>
<rule ipv="ipv4"
table="filter"
chain="restrict_user_ipv4"
priority="3">-j DROP</rule>
<chain ipv="ipv6"
table="filter"
chain="restrict_user_ipv6"/>
<rule ipv="ipv6"
table="filter"
chain="OUTPUT"
priority="1">-m owner --uid-owner user -j restrict_user_ipv6</rule>
<rule ipv="ipv6"
table="filter"
chain="restrict_user_ipv6"
priority="3">-j DROP</rule>
</direct>
Don't forget to reload with
# firewall-cmd --reload
References

- 327
- 2
- 11
-
1Big thank you! Googling half day for this. And here are my commands to block internet for a specific user but allow loopback:
firewall-cmd --direct --permanent --add-chain ipv4 filter no_internet firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 1 -m owner --uid-owner some-user -j no_internet firewall-cmd --direct --permanent --add-rule ipv4 filter no_internet 2 -d 127.0.0.1/32 -j ACCEPT firewall-cmd --direct --permanent --add-rule ipv4 filter no_internet 3 -j DROP firewall-cmd --reload
– Volodymyr Krupach Jun 22 '22 at 10:48
/etc/rc.local
, it's not persistent, and if you have, then you can just remove that line. – Frxstrem Aug 26 '15 at 16:36/etc/rc.local
? (or maybe would this still work, even with systemd?) – Basj Apr 25 '20 at 14:45