Dave's Brain

Browse - Programming Tips - For security, I want the daemon (server) I am writing to become user "nobody". How do I do that?

Date: 2010jan2
OS: Linux
Language: C/C++

Q.  For security, I want the daemon (server) I am writing to become user "nobody".  How do I do that?

A.  Here's a function that does that:

bool become_nobody() { struct passwd *pw; if ((pw = getpwnam("nobody")) == NULL) return false; seteuid(pw->pw_uid); setegid(pw->pw_gid); return true; } // Example use: main() { become_nobody(); // Do very first // Do everything else }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.