unshare() — Linux system interface
Disassociate selected resources from shared execution context; namespace flags can create new namespaces for the caller (PID and time namespaces have special child semantics). CLONE_NEWUTS creates a UTS namespace; authorization depends on capabilities, kernel configuration, user namespace and sandbox policies. EINVAL can mean invalid or unsupported flags; EPERM can mean insufficient authority. The VM-only example creates a new UTS namespace in its own short-lived process and compares namespace inode identity. It makes no hostname changes. Never run on production or to evade sandbox restrictions. Compile-only here; run only in an explicitly authorized disposable VM with appropriate capability and reviewed prerequisites. No privilege elevation instructions are provided. Compile the complete C example in a fresh private working directory using gcc -std=c17 -Wall -Wextra -Wpedantic -Werror example.c -o example; Runtime NOT RUN locally. In the authorized VM only, run ./example --authorized-vm with a timeout after review. Linux-specific interface or flags; examples target Linux/glibc on x86_64. These are libc-facing signatures, not raw architecture-specific syscall ABIs.
Formal Syntax & Structure
#define _GNU_SOURCE#include <sched.h>int unshare(int flags);Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| flags | int | Resource or namespace flags, e.g. CLONE_NEWUTS. |
Return Value
0 on success. On failure, returns -1 and sets errno.
Working Example
#define _GNU_SOURCE#define _POSIX_C_SOURCE 200809L#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sched.h>#include <sys/stat.h>#include <string.h>
/* A failed check ends this short-lived demonstration. */#define CHECK(x) do { if (!(x)) { \ fprintf(stderr, "check failed at line %d: %s\n", __LINE__, #x); \ return EXIT_FAILURE; \} } while (0)
int main(int argc, char **argv){ /* Explicit opt-in is not a security boundary: use only an authorized disposable VM. */ CHECK(argc == 2 && strcmp(argv[1], "--authorized-vm") == 0); struct stat before, after; CHECK(stat("/proc/self/ns/uts", &before) == 0); CHECK(unshare(CLONE_NEWUTS) == 0); CHECK(stat("/proc/self/ns/uts", &after) == 0); CHECK(before.st_dev != after.st_dev || before.st_ino != after.st_ino); puts("new UTS namespace identity; no hostname changed"); return EXIT_SUCCESS;}