fix: app runs as dedicated user, not root

- install.sh detects the real user (SUDO_USER or whoami)
- If root, creates receiptnext system user
- If regular user, uses that user for the service
- User is added to ollama group for CLI access
- Systemd service uses User=receiptnext (or detected user)
- All file ownership set to the app user
This commit is contained in:
Claus Lohmar 2026-05-30 15:44:10 +00:00
parent b6c2c3f99d
commit 59788ba0c8

View file

@ -261,7 +261,31 @@ fi
sudo_if chmod 600 "$INSTALL_DIR/.env"
info "Configuration saved to $INSTALL_DIR/.env"
# ── 8. Systemd service ───────────────────────────────────────────
# ── 8. Determine app user ──────────────────────────────────────────
# Detect the real user (the one who invoked the script, not root via sudo)
APP_USER="${SUDO_USER:-$(whoami)}"
# If still root, create a dedicated system user
if [ "$APP_USER" = "root" ]; then
APP_USER="${SERVICE_NAME}"
if ! id -u "${APP_USER}" &>/dev/null 2>&1; then
info "Creating system user '${APP_USER}'..."
sudo_if useradd --system --no-create-home --home-dir "${INSTALL_DIR}" --shell /usr/sbin/nologin "${APP_USER}"
fi
else
info "Using existing user '${APP_USER}'"
# Add user to ollama group if it exists (so they can run ollama CLI)
if getent group ollama &>/dev/null; then
sudo_if usermod -aG ollama "${APP_USER}" 2>/dev/null || true
fi
fi
# ── 9. Set ownership ───────────────────────────────────────────────
sudo_if chown -R "${APP_USER}:${APP_USER}" "${INSTALL_DIR}"
sudo_if chmod 755 "${INSTALL_DIR}" "${INSTALL_DIR}/templates" "${INSTALL_DIR}/static"
# Storage needs write permission for uploaded receipts
sudo_if chmod 775 "${INSTALL_DIR}/storage"
# ── 10. Systemd service ────────────────────────────────────────────
info "Creating systemd service..."
sudo_if tee "/etc/systemd/system/${SERVICE_NAME}.service" > /dev/null << SERVEOF
[Unit]
@ -272,7 +296,8 @@ Wants=ollama.service
[Service]
Type=simple
User=root
User=${APP_USER}
Group=${APP_USER}
WorkingDirectory=${INSTALL_DIR}
ExecStart=${INSTALL_DIR}/receiptnext
Restart=always