mkosi-sandbox(1)
okay so welcome to mkosi-sandbox(1) from the General Commands Manual β this is the page where we learn how to run commands in a custom sandbox without letting the host systemβs whole vibe crash the party β¨
π NAME π
mkosi-sandbox β run commands in a custom sandbox π«Ά
πΈ SYNOPSIS πΈ
mkosi-sandbox [options...] command [arguments]
π¦ DESCRIPTION π¦
mkosi-sandbox runs the given command in a custom sandbox. The sandbox is configured by specifying command line options that configure individual parts of the sandbox. Literally build-your-own-isolated-space energy π
If no command is specified, mkosi-sandbox will start bash in the sandbox.
ngl bestie, this sandbox is not designed to be a security boundary. Its intended purpose is to allow running commands in an isolated environment so they are not affected by the host system. Sheβs a vibe isolator, not a security vault π
It is possible to use mkosi-sandbox to create an in process sandbox for python applications by importing it as a module and invoking its enter function with only options. No command line to execute is needed in this case. As an example:
import mkosi.sandbox
mkosi.sandbox.enter(["--become-root"])
print(os.getuid())
Only the enter function can be invoked. Invoking any other functions from mkosi.sandbox is not supported and may break in future versions. so likeβ¦ donβt go poking the other functions, bestie π«Ά
β¨ OPTIONS β¨
--tmpfs DSTβ Mounts a newtmpfsatDSTin the sandbox. fresh little memory filesystem moment β¨--dev DSTβ Sets up a private/devatDSTin the sandbox. This private/devwill only contain the basic device nodes required for a functioning sandbox (e.g./dev/null) and no actual devices. no actual devices, just the essentials π--dir DSTβ Creates a directory and all missing parent directories atDSTin the sandbox. All directories are created with mode755unless the path ends with/tmpor/var/tmpin which case it is created with mode1777.--bind SRC DSTβ The source pathSRCis recursively bind mounted toDSTin the sandbox. The mountpoint is created in the sandbox if it does not yet exist. Any missing parent directories in the sandbox are created as well. The source path may optionally be prefixed with a+character. If so, the source path is interpreted relative to the sandbox root directory instead of the host root directory.--bind-try SRC DSTβ Like--bind, but doesnβt fail if the source path doesnβt exist. lowkey forgiving π--ro-bind SRC DSTβ Like--bind, but does a recursive readonly bind mount.--ro-bind-try SRC DSTβ Like--bind-try, but does a recursive readonly bind mount.--bind-foreign SRC DSTβ Like--bind, butmkosi-sandboxwill allocate a user namespace and provision it with a transient UID range usingsystemd-nsresourcedand then idmap the foreign UID range to this transient UID range usingsystemd-mountfsdfor each--bind-foreignmount. Finally,mkosi-sandboxwill join the user namespace just before executing the configure command in the sandbox. Using this option requiressystemd-nsresourcedandsystemd-mountfsdv260 or newer. fancy namespace choreography π₯--ro-bind-foreign SRC DSTβ Like--bind-foreign, but does a readonly mount.--bind-nofollow SRC DSTβ Like--bind, but does not follow symlinks forSRC. IfSRCis itself a symlink, the symlink is bind mounted as-is rather than its target.--ro-bind-nofollow SRC DSTβ Like--bind-nofollow, but does a readonly mount.--symlink SRC DSTβ Creates a symlink atDSTin the sandbox pointing toSRC. IfDSTalready exists and is a file or symlink, a temporary symlink is created and mounted on top ofDST. symlink sleight of hand π--write DATA DSTβ Writes the string fromDATAtoDSTin the sandbox.--overlay-lowerdir DIRβ AddsDIRfrom the host as a new lower directory for the next overlayfs mount.--overlay-upperdir DIRβ Sets the upper directory for the next overlayfs mount toDIRfrom the host. If set totmpfs, the upperdir and workdir will be subdirectories of a freshtmpfsmount.--overlay-workdir DIRβ Sets the working directory for the next overlayfs mount toDIRfrom the host.--overlay DSTβ Mounts a new overlay filesystem atDSTin the sandbox. The lower directories, upper directory and working directory are specified using the--overlay-lowerdir,--overlay-upperdirand--overlay-workdiroptions respectively. After each--overlayoption is parsed, the other overlay options are reset. so rude, but so organized π--unsetenv NAMEβ Unsets theNAMEenvironment variable in the sandbox.--setenv NAME VALUEβ Sets theNAMEenvironment variable toVALUEin the sandbox--chdir DIRβ Changes the working directory toDIRin the sandbox.--same-dirβ Changes to the working directory in the sandbox to the current working directory thatmkosi-sandboxis invoked in on the host. matching dirs, matching vibes π«Ά--become-rootβ Maps the current user to the root user in the sandbox. If this option is not specified, the current user is mapped to itself in the sandbox. Regardless of whether this option is specified or not, the current user will have a full set of ambient capabilities in the sandbox. This includesCAP_SYS_ADMINwhich means that the invoked process in the sandbox will be able to do bind mounts and other operations. Ifmkosi-sandboxis invoked as the root user, this option wonβt do anything. main character root energy π--map-foreignβ Map the foreign UID range in the sandbox. For more information on the foreign UID range, see Users, Groups, UIDs and GIDs on systemd Systems. When runningmkosi-sandboxunprivileged, this option requiressystemd-nsresourcedv260 or newer.--map-delegate Nβ MapNadditional 64K UID/GID ranges in the sandbox. This is needed to executesystemd-nspawnor any program that tries to allocate a transient UID range viasystemd-nsresourcedfrom within themkosi-sandboxenvironment. This option requiressystemd-nsresourcedv260 or newer.--suppress-chownβ Specifying this option causes all calls tochown()or similar system calls to become a noop in the sandbox. This is primarily useful when invoking package managers in the sandbox which might try tochown()files to different users or groups which would fail unlessmkosi-sandboxis invoked by a privileged user. chown? never heard of her π--suppress-syncβ Specifying this option causes all calls tosync()or similar system calls to become a noop in the sandbox. This is primarily useful when invoking package managers in the sandbox which might try tosync()files when extracting packages which is unnecessary when building images and slows down execution time significantly. sync? not today, bestie π--unshare-netβ Specifying this option makesmkosi-sandboxunshare a network namespace if possible.--unshare-ipcβ Specifying this option makesmkosi-sandboxunshare an IPC namespace if possible.--pack-fdsβ Pack inherited file descriptors together starting at file descriptor number3and set$LISTEN_FDSto the number of packed file descriptors and$LISTEN_PIDto the current process pid.--debugβ Log various messages describing how the sandbox is set up.--versionβ Show package version.--help,-hβ Show brief usage information.
π EXAMPLES π
Start bash in the current working directory in its own network namespace as the current user.
mkosi-sandbox --bind / / --same-dir --unshare-net
Run id as the root user in a sandbox with only /usr from the host plus the necessary symlinks to be able to run commands.
mkosi-sandbox \
--ro-bind /usr /usr \
--symlink usr/bin /bin \
--symlink usr/lib /lib \
--symlink usr/lib64 /lib64 \
--symlink usr/sbin /sbin \
--dev /dev \
--bind /proc /proc \
--tmpfs /tmp \
--become-root \
id
π¦ SEE ALSO π¦
π and thatβs the mkosi-sandbox(1) page from the General Commands Manual, bestie! source/version + date sign-off energy lives in the footer, and we are absolutely not copying the footer line β periodt, slay, bye π
β¨
π¦ where this page comes from
- arch rolling: mkosi 27.1-1 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms