为什么我不能在Ubuntu中创建原始套接字?

我正在学习如何在Linux中使用原始套接字。 我正在尝试创建一个这样的套接字:

if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { perror("socket() failed"); exit(-1); } 

但我在发布后获得的是:

socket()失败:不允许操作

我知道只有root可以创建原始套接字,但如果我用SUID位或sudo运行它 – 问题是一样的。 怎么了? 该系统是Ubuntu 11.04。

也许我包括不必要的标题?

 #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  

我想知道 – 为什么SUID没用?

我没钱,你没有正确运行你的代码。

我已将您的确切代码复制并粘贴到空的main() 。 如果我像我一样运行它,我会得到同样的错误,但它在sudo下运行正常。 这是在Ubuntu上。

代码:

 #include  #include  int main() { int sd; if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { perror("socket() failed"); return -1; } return 0; } 

像我一样跑:

 aix@aix:~$ ./a.out socket() failed: Operation not permitted aix@aix:~$ 

以root身份运行:

 aix@aix:~$ sudo ./a.out aix@aix:~$ 

根据man:只允许有效用户ID为0或CAP_NET_RAWfunction的进程打开原始套接字

所以你可以按照下面的建议用sudo运行应用程序,或者为它设置CAP_NET_RAWfunction(实际上你也需要CAP_NET_ADMIN):

 # setcap cap_net_raw,cap_net_admin=eip PATH_TO_YOUR_APPLICATION 

详细信息可以在http://ftp.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.4/capfaq-0.2.txt找到

无论如何,标题不会影响它。

即使您要添加一些不必要的文件,也不会影响程序的运行。