为什么我不能在Ubuntu创建原始套接字? [英] Why I cant create raw socket in Ubuntu?

查看:156
本文介绍了为什么我不能在Ubuntu创建原始套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在Linux的原始套接字工作。我试图创建一个这样的插座:

I'm learning how to work with raw sockets in Linux. I'm trying to create a socket like that:

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

但我只推出后得到的是:

But all I got after launch is:

插座()失败:不允许操作

socket() failed: Operation not permitted

我知道,只有根能够创建原始套接字,但如果我有SUID位或sudo运行它 -
问题是相同的。怎么了?该系统是Ubuntu的11.04。

I know that only root can create raw sockets, but if I run it with SUID bit or sudo - the problem is the same. What's wrong? The system is Ubuntu 11.04.

也许我不用包含头文件?

Maybe I'm including needless headers?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>

和我想知道 - 为什么SUID是没用的。

And I'm wondering - why SUID is useless?

推荐答案

我的钱c正确。

我已经复制并粘贴您的确切code到一个空的的main()。我得到了同样的错误,如果我运行它自己,但它在须藤正常运行。这是在Ubuntu。

I've copied and pasted your exact code into an empty main(). I get the same error if I run it as myself, but it runs correctly under sudo. This is on Ubuntu.

在code:

#include <sys/socket.h>
#include <netinet/in.h>

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身份运行:

Run as root:

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

这篇关于为什么我不能在Ubuntu创建原始套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆