Is is possible to change root's password using setuid?

What I tried is writing a simple program which calls system("passwd root") owned by root and its setuid bit is set but it didn't work. The output is: You may not view or modify password information for root.


In your program, you probably overlooked making the real and effective uid set to the same value. Gids also should match root's gid. Something like this:


See for example sue (a simple setuid/setgid wrapper).

  • Might he also need to set USER environment variable? – Otheus Feb 6 '16 at 17:49
  • He might - and LOGNAME - but the lack of proper permissions is the most likely problem. – Thomas Dickey Feb 6 '16 at 20:22
  • OK, after some testing, I found only setuid(geteuid()) is need. Interestingly enough, running the program via ltrace or strace prevented seteuid from happening, so those traces were bogus. – Otheus Feb 6 '16 at 22:19

Thomas Dickey is absolutely right. However, don't do this. Use sudo instead.

Create /etc/sudoers.d/change_root_pass

YOURUSERNAME ALL = (ALL) passwd root


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.