Skip to content
systemcoding.dev
Virtualization

Create a user namespace with unshare(CLONE_NEWUSER)

Call unshare(CLONE_NEWUSER) as a normal user on Linux 6.12 to get a namespace where you hold every capability, and see why geteuid shows 65534.

1 min read
Summarize in AI

A user namespace is the one namespace an unprivileged process can create, and it is what makes rootless containers possible. Inside it the process holds a full set of capabilities, but only over resources the namespace owns.

userns.cc
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <unistd.h>

int main(void) {
    if (unshare(CLONE_NEWUSER) == -1) { perror("unshare"); return 1; }
    printf("euid=%u egid=%u\n", geteuid(), getegid());
    return 0;
}

Run as an ordinary user:

Text
euid=65534 egid=65534

The ID is 65534, the overflow ID from /proc/sys/kernel/overflowuid, because the new namespace has no uid_map yet, so your real UID has nothing to map to. The process already has capabilities in the namespace, but it cannot create a file as a meaningful owner until you write the map; the next snippet shows how. See user_namespaces(7).

If unshare fails with EPERM or ENOSPC, the system disabled user namespaces: user.max_user_namespaces set to 0, or a distribution policy that restricts unprivileged creation. The call also fails for a process that is multithreaded, since CLONE_NEWUSER needs a single-threaded caller, so do it before starting any threads. Having capabilities inside does not let you touch the host: mounting a block device or loading a module still fails.

Also inKernels
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.