Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aont/d999b9ca6962fc7f588a3d3e30bd29e3 to your computer and use it in GitHub Desktop.

Select an option

Save aont/d999b9ca6962fc7f588a3d3e30bd29e3 to your computer and use it in GitHub Desktop.

Bridge a system-level systemd target to the user instance

  1. Sets up user linger and units: The script enables linger for the current user so their user-level systemd instance can run without an active login, and installs the user-network-online@.service unit at the system level plus a network-online.target marker unit in the user-level systemd directory.

  2. Bridges system network-online to user instance: The user-network-online@.service runs as a system service after network-online.target, and uses systemctl --user start network-online.target (with the appropriate XDG_RUNTIME_DIR) to trigger a corresponding network-online.target in the user’s systemd instance.

  3. Enables automatic startup on boot: By enabling user-network-online@<UID>.service with systemctl enable --now, the system ensures that whenever the system reaches multi-user.target and the network is online, the user-level network-online.target is started automatically for that user.

#!/bin/sh
loginctl enable-linger $USER
sudo cp -t /etc/systemd/system/ user-network-online@.service
sudo systemctl daemon-reload
USER_SYSTEMD_PATH="$HOME"/.config/systemd/user
mkdir -p "$USER_SYSTEMD_PATH"
cp -t "$USER_SYSTEMD_PATH" network-online.target
systemctl --user daemon-reload
sudo systemctl enable --now user-network-online@$(id -u).service
[Unit]
Description=User-level Network is Online (marker only)
Documentation=man:systemd.special(7)
[Unit]
Description=Trigger user-level network-online.target for %i
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
User=%i
Environment=XDG_RUNTIME_DIR=/run/user/%i
ExecStart=/usr/bin/systemctl --user start network-online.target
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment