Skip to content

Commit 0a45c12

Browse files
committed
feat: add --container-runuser option to use runuser instead of su
Add new --container-runuser/-R option that allows using runuser instead of su inside container when unshare_groups is enabled. This can be configured via: - Command line: --container-runuser or -R - Environment variable: DBX_CONTAINER_RUNUSER=1/true - Config file: distrobox_container_runuser=1/true The option defaults to disabled (0) to maintain backward compatibility.
1 parent 1e97ce2 commit 0a45c12

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

distrobox-enter

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# DBX_CONTAINER_MANAGER
3333
# DBX_CONTAINER_NAME
3434
# DBX_CONTAINER_CLEAN_PATH
35+
# DBX_CONTAINER_RUNUSER
3536
# DBX_NON_INTERACTIVE
3637
# DBX_VERBOSE
3738
# DBX_SKIP_WORKDIR
@@ -109,6 +110,7 @@ headless=0
109110
skip_workdir=0
110111
verbose=0
111112
clean_path=0
113+
use_runuser=0
112114
version="1.8.2.3"
113115

114116
# Source configuration files, this is done in an hierarchy so local files have
@@ -146,6 +148,9 @@ if [ "$(id -ru)" -ne 0 ]; then
146148
distrobox_sudo_program=${DBX_SUDO_PROGRAM:-${distrobox_sudo_program:-"sudo"}}
147149
fi
148150

151+
# Read distrobox_container_runuser from config or env
152+
distrobox_container_runuser=${DBX_CONTAINER_RUNUSER:-${distrobox_container_runuser:-0}}
153+
149154
[ -n "${DBX_CONTAINER_MANAGER}" ] && container_manager="${DBX_CONTAINER_MANAGER}"
150155
[ -n "${DBX_CONTAINER_NAME}" ] && container_name="${DBX_CONTAINER_NAME}"
151156
[ -n "${DBX_CONTAINER_CLEAN_PATH}" ] && clean_path=1
@@ -158,6 +163,10 @@ fi
158163
[ "${non_interactive}" = "false" ] && non_interactive=0
159164
[ "${verbose}" = "true" ] && verbose=1
160165
[ "${verbose}" = "false" ] && verbose=0
166+
[ "${distrobox_container_runuser}" = "true" ] && use_runuser=1
167+
[ "${distrobox_container_runuser}" = "false" ] && use_runuser=0
168+
[ "${distrobox_container_runuser}" = "1" ] && use_runuser=1
169+
[ "${distrobox_container_runuser}" = "0" ] && use_runuser=0
161170

162171
# show_help will print usage to stdout.
163172
# Arguments:
@@ -193,6 +202,7 @@ Options:
193202
--root/-r: launch podman/docker/lilipod with root privileges. Note that if you need root this is the preferred
194203
way over "sudo distrobox" (note: if using a program other than 'sudo' for root privileges is necessary,
195204
specify it through the DBX_SUDO_PROGRAM env variable, or 'distrobox_sudo_program' config variable)
205+
--container-runuser/-R: inside the container, use runuser instead of su for user switching (only when unshare_groups is enabled)
196206
--dry-run/-d: only print the container manager command generated
197207
--verbose/-v: show more verbosity
198208
--version/-V: show version
@@ -231,6 +241,10 @@ while :; do
231241
shift
232242
skip_workdir=1
233243
;;
244+
-R | --container-runuser)
245+
shift
246+
use_runuser=1
247+
;;
234248
-n | --name)
235249
if [ -n "$2" ]; then
236250
container_name="$2"
@@ -696,7 +710,7 @@ if [ "${container_custom_command}" -eq 0 ]; then
696710
fi
697711

698712
# If we have a command and we're unsharing groups, we need to execute those
699-
# command using su $container_command_user
713+
# command using su $container_command_user (or runuser if --runuser is specified)
700714
# if we're in a tty, also allocate one
701715
if [ "${unshare_groups:-0}" -eq 1 ]; then
702716
# shellcheck disable=SC2089,SC2016
@@ -707,7 +721,11 @@ if [ "${unshare_groups:-0}" -eq 1 ]; then
707721
fi
708722
set -- "-m" "$@"
709723
set -- "${container_command_user}" "$@"
710-
set -- "su" "$@"
724+
if [ "${use_runuser}" -eq 1 ]; then
725+
set -- "runuser" "$@"
726+
else
727+
set -- "su" "$@"
728+
fi
711729
fi
712730

713731
# Generate the exec command and run it

docs/usage/distrobox-enter.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ If using it inside a script, an application, or a service, you can specify the
2525
--root/-r: launch podman/docker/lilipod with root privileges. Note that if you need root this is the preferred
2626
way over "sudo distrobox" (note: if using a program other than 'sudo' for root privileges is necessary,
2727
specify it through the DBX_SUDO_PROGRAM env variable, or 'distrobox_sudo_program' config variable)
28+
--container-runuser/-R: inside the container, use runuser instead of su for user switching (only when unshare_groups is enabled)
2829
--dry-run/-d: only print the container manager command generated
2930
--verbose/-v: show more verbosity
3031
--version/-V: show version
@@ -57,6 +58,7 @@ You can also use environment variables to specify container manager and containe
5758

5859
DBX_CONTAINER_NAME
5960
DBX_CONTAINER_MANAGER
61+
DBX_CONTAINER_RUNUSER
6062
DBX_SKIP_WORKDIR
6163
DBX_SUDO_PROGRAM
6264

@@ -85,3 +87,12 @@ run podman/docker/lilipod as root, such as 'pkexec' or 'doas', you may specify i
8587
Additionally, in one of the config file paths that distrobox supports, such as `~/.distroboxrc`,
8688
you can also append the line `distrobox_sudo_program="doas"` (for example) to always run
8789
distrobox commands involving rootful containers using 'doas'.
90+
91+
Similarly, to use runuser instead of su inside the container (when unshare_groups is enabled),
92+
you can specify it with `DBX_CONTAINER_RUNUSER` environment variable. For example:
93+
94+
DBX_CONTAINER_RUNUSER="true" distrobox enter -n container
95+
96+
Additionally, in one of the config file paths that distrobox supports, such as `~/.distroboxrc`,
97+
you can also append the line `distrobox_container_runuser="true"` to always use runuser
98+
instead of su inside the container.

0 commit comments

Comments
 (0)