我是root,
ping 127.0.0.1 返回错误;
root@sam-PC:~# ping 127.0.0.1
socket: Permission denied
root@sam-PC:~# ifconfig
eth0 Link encap:Ethernet HWaddr 00:00:ff:ff:00:00
inet addr:192.168.100.104 Bcast:192.168.100.255 Mask:255.255.255.0
inet6 addr: fe80::200:ffff:feff:0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:36 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2220 (2.2 KB) TX bytes:524 (524.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:40 errors:0 dropped:0 overruns:0 frame:0
TX packets:40 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3160 (3.1 KB) TX bytes:3160 (3.1 KB)
root@sam-PC:~# ping 192.168.100.100
socket: Permission denied
谁知道为什么?
答案1
对于这个问题,我们应该添加以下几行/etc/group
:
inet:x:3003:root
net_raw:x:3004:root
答案2
底层 ping 使用 sock_raw。要创建这样的套接字,您必须具有 root 权限。
int main(void) {
rawsock = socket(AF_INET, SOCK_RAW, protocol->p_proto);
if(rawsock < 0){
perror("socket");
return -1;
}
}
如果ping的所有者不是root,错误将无法修复。使用linux能力机制也无法解决这个问题。
答案3
OP 以 root 身份执行 ping,因此这不是对他们的回答,而是对 @leesagacious 的回答的扩展评论。在我的 CentOS Stream 8 上,对于 askubuntu 来说,这更加不合适……除非它也发生在那里,否则 Linux 功能机制确实似乎是答案,赞https://superuser.com/a/1616749/17633:
[manager@smu-centos8-daily-test ~]$ ping -c 1 8.8.8.8
ping: socket: Operation not permitted
[manager@smu-centos8-daily-test ~]$ getcap /usr/bin/ping
[manager@smu-centos8-daily-test ~]$ sudo setcap cap_net_raw+p /usr/bin/ping
[manager@smu-centos8-daily-test ~]$ ping -c 1 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=117 time=4.57 ms
--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 4.567/4.567/4.567/0.000 ms
[manager@smu-centos8-daily-test ~]$ getcap /usr/bin/ping
/usr/bin/ping = cap_net_raw+p
[manager@smu-centos8-daily-test ~]$ sudo setcap -r /usr/bin/ping
[manager@smu-centos8-daily-test ~]$ getcap /usr/bin/ping
[manager@smu-centos8-daily-test ~]$ ping -c 1 8.8.8.8
ping: socket: Operation not permitted
[manager@smu-centos8-daily-test ~]$
我不认为所有者ping 的 uid 与执行 ping 的进程的 uid 相反,是相关的,除非回到 ping 被 setuid 的时代。