Skip to content

Instantly share code, notes, and snippets.

@HackingGate
Last active April 21, 2025 12:44
Show Gist options
  • Save HackingGate/ff63228a8d1e368d5703f02e7b1d04e9 to your computer and use it in GitHub Desktop.
Save HackingGate/ff63228a8d1e368d5703f02e7b1d04e9 to your computer and use it in GitHub Desktop.
Installs and configures acpid to lock the session on a short power‑button press (Ubuntu 24.04).
#!/usr/bin/env bash
set -euo pipefail
# 1) Install acpid if missing
if ! dpkg -s acpid >/dev/null 2>&1; then
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y acpid
fi
# 2) Enable/start it if needed
if ! systemctl is-enabled acpid >/dev/null; then
systemctl enable acpid
fi
if ! systemctl is-active acpid >/dev/null; then
systemctl start acpid
fi
# 3) Ensure logging is set
DEFAULTS=/etc/default/acpid
if ! grep -q -- '--logevents' "$DEFAULTS"; then
cat >"$DEFAULTS" <<'EOF'
OPTIONS="--logevents"
EOF
fi
# 4) Drop in the event rule
RULE=/etc/acpi/events/powerbtn-lock
if ! grep -q 'lock-sessions' "$RULE"; then
mkdir -p /etc/acpi/events
cat >"$RULE" <<'EOF'
event=button/power.*PBTN.*
action=/usr/bin/loginctl lock-sessions
EOF
fi
# 5) Reload
systemctl restart acpid
@HackingGate
Copy link
Author

Tested on Surface Go 2 (Ubuntu 25.04, default GNOME 48, Wayland)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment