I'm working in Ubuntu 14.04 I have an C++ application which I have compiled. I get the executable which name is "program". Well the thing is that this program use opencv and sockets then if I try to execute it in the terminal like this:


I got this error:

 listener: socket: Operation not permitted
 setsockopt: Bad file descriptor

This error appear because I using socket and I need to run my application as root, so I typed this:

sudo ./program

This request my password and the program works property.

The thing is that I want to run this "program" automatically when I start my Ubuntu, but it does not work. I have another application the name is "camera" which only uses opencv, no socket, then I do not need to run as root. So I put this application in the startup application of Ubuntu and the "camera" application start at the beginning without problem.

So how can I automatic start an application which uses sockets in Ubuntu?

I tried to do this:

sudo chown root:root ./program
sudo chmod a+s ./program

But my program uses video so then when I try to watch the video the program stops. This is due my program is not free of security holes, so that they're vulnerable to buffer overflow exploits, etc.

Basically I just need to know how can I throw my program as start up without problem.

  • 1
    You need to provide more details. We can't help you with both an anonymous "program" and no source code at the same time. We need to know one or the other at least. We don't need all of the code, just the code around where the error occurs, which appears to be a socket() call. It may be enough to provide the tail end of the results of strace -e socket ./program, again assuming that the error occurs on a socket() call, and not something else. Oct 21, 2014 at 14:42
  • Hi Warren many thanks for your quick answer. My program uses the socket because I use the RecvRawEth.c file for recieves ethernet data. As I said above I just want to automatic start my application, and (In my opinion, that is maybe wrong) I need to start the program as root, because If I want to use sockets I need it. I edited the answer, I hope can help. Many thanks again Oct 21, 2014 at 15:04
  • Is your program is using a port lower than 1024? Those ports are supposed to be used by well known daemons with root permision. Have you tried using a higer port lik 6666 or something? I think those ports are accessible without the need of root permision.
    – YoMismo
    Feb 11, 2015 at 8:25

1 Answer 1


Based on the RecvRawEth.c file name you posted in your comment above, I'm guessing that you're attempting to use raw sockets (SOCK_RAW). This always requires that the program be run as root because raw sockets have the potential to let you bypass other security mechanisms, such as the restriction on privileged ports.

You really should re-think this application. Very few programs legitimately require raw sockets. There is almost always a better way.

You're throwing at least two additional problems into the mix, which really is not kosher on Stack Exchange type sites, but I will address them anyway:

  1. setuid root

    You are attempting to get around the restriction on raw sockets by marking it to run as root no matter who actually started it. You are potentially opening up one or more security holes by doing this. Trust me: decades of Unix history tell us that complicated setuid root programs are likely to have exploitable problems. It can take many years to find and excise all of them.

    If you absolutely must use raw sockets and thus need some element of the program to run as root, it's best to write it as a limited-scope daemon. That is to say, make the program as small as possible, doing only the absolute bare minimum you must do as root, plus whatever is necessary to export the resulting information to a program running with normal privileges.

  2. Start on boot

    If you create a daemon, setting it up to run on system boot is trivial. Any decent tutorial showing you how to create a daemon will go into this. On Ubuntu, I believe this means dealing with systemd.

    As for the GUI piece, you simply need to add it to the session startup.

  • Hi Warren do you mean that I need to split my program in two pieces?. The thing is that I was using this exactly program in an ARM, in which I started it at the boot without problem. The thing is that when I switch the ARM on always I start as root, then I can run my application on boot. Could I do something similar here? Oct 21, 2014 at 15:33
  • @user3766585: Yes, splitting the app into two parts — a root daemon and a non-privileged GUI part — is one solution to the problem. Ubuntu purposely does not enable the root account for interactive logins, again for security reasons. While you could "fix" this, you may be better off choosing a different Linux distro to base your app on. If this app needs to start up and completely take over the entire system, that's more of a kiosk type application, which means you probably need to customize the whole OS startup, which is more of an Arch type application than an Ubuntu one. Oct 21, 2014 at 15:37
  • Many thanks for your answer Warren, but just let me undestand, It does mean that if I use an application which uses socket I can not start it automatic from my user account? Do I have to split the program in two part one with root privileged?? and all of this is because I using sockets??? Oct 21, 2014 at 15:54
  • So I mean there is no way to do somenthing like "sudo ./myprogram" in some script if I using sockets? Oct 21, 2014 at 15:57
  • 1
    Ubuntu has decided to switch to systemd, but 14.04 is still on Upstart. Oct 22, 2014 at 15:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .