#!/usr/bin/env bash # shellcheck disable=SC2034 # shared helpers set variables not every script uses # Lets a user run docker without sudo by adding them to the docker group. # Usage: ... | sudo bash (adds the user who ran sudo) # ... | sudo bash -s -- NAME (adds NAME) # Source: https://bianconi.tech/s/docker-group # Author: Luis Fernando Bianconi (https://bianconi.tech) main() { SCRIPT_URL="https://bianconi.tech/s/docker-group" SITE_URL="https://bianconi.tech" # ---- common helpers (shared by every script) ---- set -euo pipefail info() { printf '\033[1;34m==>\033[0m %s\n' "$*"; } ok() { printf '\033[1;32m✔\033[0m %s\n' "$*"; } warn() { printf '\033[1;33m!\033[0m %s\n' "$*" >&2; } fail() { printf '\033[1;31m✘\033[0m %s\n' "$*" >&2; exit 1; } require_root() { [ "$(id -u)" -eq 0 ] || fail "Run as root: curl -fsSL ${SCRIPT_URL} | sudo bash" } # Detects the package manager (apt or dnf) from /etc/os-release detect_os() { [ -r /etc/os-release ] || fail "Unsupported system: /etc/os-release not found" # shellcheck disable=SC1091 . /etc/os-release OS_ID="${ID:-unknown}" OS_VERSION="${VERSION_ID:-}" OS_CODENAME="${UBUNTU_CODENAME:-${VERSION_CODENAME:-}}" case "${ID:-} ${ID_LIKE:-}" in *debian*|*ubuntu*) PM=apt ;; *rhel*|*fedora*|*centos*) PM=dnf ;; *) fail "Unsupported distribution: ${PRETTY_NAME:-$OS_ID}. Supported: Ubuntu, Debian, RHEL, Rocky, AlmaLinux, Fedora" ;; esac info "Detected ${PRETTY_NAME:-$OS_ID}" } APT_UPDATED=0 pkg_install() { if [ "$PM" = apt ]; then if [ "$APT_UPDATED" -eq 0 ]; then apt-get update -qq; APT_UPDATED=1; fi DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null else dnf install -y -q --allowerasing "$@" >/dev/null fi } # Forces the next pkg_install to refresh apt lists (after adding a repository) apt_refresh() { APT_UPDATED=0; } # EPEL provides extra packages (htop, fail2ban...) on RHEL-like systems enable_epel() { [ "$PM" = dnf ] || return 0 case "$OS_ID" in fedora) return 0 ;; rhel) dnf install -y -q "https://dl.fedoraproject.org/pub/epel/epel-release-latest-${OS_VERSION%%.*}.noarch.rpm" >/dev/null ;; *) dnf install -y -q epel-release >/dev/null ;; esac } cpu_arch() { case "$(uname -m)" in x86_64|amd64) echo amd64 ;; aarch64|arm64) echo arm64 ;; *) fail "Unsupported CPU architecture: $(uname -m)" ;; esac } has_systemd() { [ -d /run/systemd/system ]; } # The user who ran sudo (empty when logged in directly as root) TARGET_USER="${SUDO_USER:-}" [ "$TARGET_USER" = root ] && TARGET_USER="" # For scripts that configure a user's home: sets USER_NAME, USER_GROUP and HOME_DIR setup_user() { USER_NAME="${TARGET_USER:-root}" HOME_DIR="$(getent passwd "$USER_NAME" | cut -d: -f6)" [ -d "$HOME_DIR" ] || fail "Home folder for $USER_NAME not found" USER_GROUP="$(id -gn "$USER_NAME")" } # Runs a command as that user, so files in their home stay theirs run_as_user() { if [ "$USER_NAME" = root ]; then bash -c "$1" else sudo -u "$USER_NAME" -H bash -c "$1" fi } TMP_DIR="$(mktemp -d)" trap 'rm -rf "$TMP_DIR"' EXIT # ---- end of common helpers ---- require_root USER_NAME="${1:-$TARGET_USER}" [ -n "$USER_NAME" ] || fail "No user given. Run with: curl -fsSL ${SCRIPT_URL} | sudo bash -s -- USERNAME" id "$USER_NAME" >/dev/null 2>&1 || fail "User $USER_NAME does not exist" getent group docker >/dev/null || groupadd docker usermod -aG docker "$USER_NAME" ok "Added $USER_NAME to the docker group" info "Log out and back in (or run: newgrp docker) for the change to take effect" } main "$@"