setreuid(2)
๐ setreuid(2) โ the System Calls Manual, but make it make sense ๐
okay bestie, sit down, grab your boba, because today we're talking about setreuid(2) โ that's the page from section 2 of the manual (system calls, the kernel-flavored ones ๐ง). It's also lowkey about her sister setregid(2) because they're literally twins, same page, same vibe.
โจ NAME โจ
setreuid, setregid โ set real and/or effective user or group ID. that's it, that's the whole job ๐
๐ LIBRARY ๐
Standard C library โ that's libc, which you link with -lc. girl the standard library ate and left no crumbs ๐ซถ
๐ SYNOPSIS ๐
#include <unistd.h>
int setreuid(uid_t ruid, uid_t euid);
int setregid(gid_t rgid, gid_t egid);
๐ธ Feature Test Macro Requirements for glibc (see feature_test_macros(7)) ๐ธ
so before you can even use these, glibc is gonna vibe check you:
setreuid(), setregid():
_XOPEN_SOURCE >= 500
|| /* Since glibc 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _BSD_SOURCE
no cap, you literally have to define one of those or it's giving "not available" ๐
๐ DESCRIPTION ๐
setreuid() sets the real and effective user IDs of the calling process. main character energy, but for your process identity โจ
Hand it a value of -1 for either the real or effective user ID and the system is like "ok bestie, not touching that one" โ that ID stays unchanged.
Unprivileged processes may only set the effective user ID to one of these three:
- the real user ID
- the effective user ID
- the saved set-user-ID
Unprivileged users may only set the real user ID to one of these two:
- the real user ID
- the effective user ID
And here's the plot twist ๐ โ if the real user ID is set (like, ruid is not -1) or the effective user ID gets set to something that isn't equal to the previous real user ID, then the saved set-user-ID gets set to the new effective user ID. sneak attack, honestly.
Completely analogously (in the same way, for the group chat that missed it), setregid() sets the real and effective group ID's of the calling process โ and every single thing above holds, just with "group" swapped in wherever it said "user". same energy, different ID type ๐
โญ RETURN VALUE โญ
- Success? You get
0. slay. ๐ - Error? You get
-1, anderrnogets set to tell you what went wrong.
Note: there are cases wheresetreuid()can fail even when the caller is UID 0. It is a grave security error to omit checking for a failure return fromsetreuid(). like, actually catastrophic. don't be that girl. check your return values, periodt. ๐
๐ ERRORS ๐
EAGAINโ the call would change the caller's real UID (i.e.,ruiddoes not match the caller's real UID), but there was a temporary failure allocating the necessary kernel data structures. temporary!! like a situationship.EAGAINโruiddoes not match the caller's real UID and this call would bring the number of processes belonging to the real user IDruidover the caller'sRLIMIT_NPROCresource limit. Since Linux 3.1, this error case no longer occurs (but robust applications should check for this error anyway โ be an over-preparer, it's a look); see the description ofEAGAINinexecve(2).EINVALโ one or more of the target user or group IDs is not valid in this user namespace. rude, but okay.EPERMโ the calling process is not privileged (on Linux, that means it doesn't have the necessary capability in its user namespace:CAP_SETUIDin the case ofsetreuid(), orCAP_SETGIDin the case ofsetregid()) and a change other than these three was specified:- swapping the effective user (group) ID with the real user (group) ID
- setting one to the value of the other
- setting the effective user (group) ID to the value of the saved set-user-ID (saved set-group-ID)
i.e., you tried to do a thing you are simply not invited to do ๐ โ๏ธ
๐ฆ VERSIONS ๐ฆ
POSIX.1 does not specify all of the UID changes that Linux permits for an unprivileged process. so there's โจrangeโจ.
For setreuid(), the effective user ID can be made the same as the real user ID or the saved set-user-ID, and it is unspecified whether unprivileged processes may set the real user ID to the real user ID, the effective user ID, or the saved set-user-ID.
For setregid(), the real group ID can be changed to the value of the saved set-group-ID, and the effective group ID can be changed to the value of the real group ID or the saved set-group-ID.
The precise details of what ID changes are permitted vary across implementations. so don't assume, bestie, check.
POSIX.1 makes no specification about the effect of these calls on the saved set-user-ID and saved set-group-ID. it's giving "unspecified".
๐ STANDARDS ๐
POSIX.1-2024. certified iconic. ๐
๐ HISTORY ๐
- POSIX.1-2001, 4.3BSD (first appeared in 4.2BSD). old money, literally.
- Setting the effective user (group) ID to the saved set-user-ID (saved set-group-ID) is possible since Linux 1.1.37 (1.1.38). ๐ง
- The original Linux
setreuid()andsetregid()system calls supported only 16-bit user and group IDs. Subsequently, Linux 2.4 addedsetreuid32()andsetregid32(), supporting 32-bit IDs. The glibcsetreuid()andsetregid()wrapper functions transparently deal with the variations across kernel versions โ so you don't have to think about it, they handle it for you like the good friend they are ๐ซถ
๐ง C library/kernel differences ๐ง
At the kernel level, user IDs and group IDs are a per-thread attribute. but POSIX requires that all threads in a process share the same credentials. conflict!! ๐ญ
The NPTL threading implementation handles the POSIX requirements by providing wrapper functions for the various system calls that change process UIDs and GIDs. These wrapper functions (including those for setreuid() and setregid()) employ a signal-based technique to ensure that when one thread changes credentials, all of the other threads in the process also change their credentials. like a group chat where everyone updates at once, no one left on read โจ
For details, see nptl(7).
๐ SEE ALSO ๐
getgid(2), getuid(2), seteuid(2), setgid(2), setresuid(2), setuid(2), capabilities(7), credentials(7), user_namespaces(7)
go read those next, bestie, they're all in the same friend group ๐
okay that's the whole page!! this was Linux man-pages 6.20, dated 2026-02-08, and it was setreuid(2) all along ๐ mwah, go check your return values, ily, bye ๐โจ๐ฆ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-4-Clause-UC
- Copyright 1983-1991, The Regents of the University of California
- Copyright 2009-2015, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms