如何禁用用户的网络访问?

如何禁用用户的网络访问?

尝试禁用用户的网络访问:

[root@notebook ~]# iptables -I OUTPUT -m owner --uid-owner tempuser -j DROP
[root@notebook ~]# ip6tables -I OUTPUT -m owner --uid-owner tempuser -j DROP
Could not open socket to kernel: Address family not supported by protocol
[root@notebook ~]# 
[root@notebook ~]# iptables -I INPUT -m owner --uid-owner tempuser -j DROP
iptables: Invalid argument. Run `dmesg' for more information.
[root@notebook ~]# ip6tables -I INPUT -m owner --uid-owner tempuser -j DROP
Could not open socket to kernel: Address family not supported by protocol
[root@notebook ~]# 

测试它:

[root@notebook ~]# su - tempuser
[tempuser@notebook ~]$ ping google.com
ping: unknown host google.com
[tempuser@notebook ~]$ 
[tempuser@notebook ~]$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=56 time=4.80 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=56 time=4.07 ms
^C
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1057ms
rtt min/avg/max/mdev = 4.071/4.439/4.807/0.368 ms
[tempuser@notebook ~]$ 
[tempuser@notebook ~]$ exit
logout
[root@notebook ~]# ping google.com
PING google.com (216.58.209.174) 56(84) bytes of data.
64 bytes from bud02s21-in-f14.1e100.net (216.58.209.174): icmp_seq=1 ttl=55 time=5.05 ms
^C
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 572ms
rtt min/avg/max/mdev = 5.059/5.059/5.059/0.000 ms
[root@notebook ~]# 

问题:如何在 Linux 下禁用给定用户的网络访问? (输入/输出/IPv4/IPv6?) - 为什么我仍然可以与用户 ping IPv4 地址?

答案1

尝试这个,

iptables -A OUTPUT -o ethX -m owner --uid-owner {USERNAME} -j DROP

在哪里,

--uid-owner { USERNAME } :如果数据包是由具有给定有效用户名的进程创建的,则匹配。 -A :将规则附加到给定的表/链 -I :将规则插入到表/链的头部

例如,我的 oracle 用户 id 是 1000,所以我将附加以下规则:

/sbin/iptables -A OUTPUT -o eth0 -m owner --uid-owner 1000 -j DROP

service iptables save

答案2

您可以使用 TCP 包装器来执行此操作,/etc/host.allow 或 /ect/host.deny 您可以在此文件中向用户提及您的网络池。

相关内容