While working with systemd user sessions, you've probably used loginctl enable-linger and loginctl disable-linger commands to manage persistent user sessions. But what if you need to check the current status? Surprisingly, this isn't as straightforward as you might expect.
The proper way to check lingering status is using the loginctl show-user command:
loginctl show-user username --property=Linger
For user 'foo', this would return:
Linger=yes
or
Linger=no
You can also get this information from the user listing:
loginctl list-users --no-legend | grep foo
This shows output like:
foo 1000 yes
Where the third column indicates lingering status (yes/no).
For those who prefer filesystem inspection, lingering status is stored at:
/var/lib/systemd/linger/username
Simply check for file existence:
[ -f /var/lib/systemd/linger/foo ] && echo "Lingering enabled" || echo "Lingering disabled"
Here's a bash function to check lingering status:
check_linger() {
local user=$1
if loginctl show-user "$user" --property=Linger | grep -q 'yes'; then
echo "Lingering is ENABLED for $user"
else
echo "Lingering is DISABLED for $user"
fi
}
# Usage:
check_linger foo
Understanding lingering status is crucial when:
- Debugging user session issues
- Automating system configuration
- Managing long-running user services
- Troubleshooting systemd behavior
Be aware that:
- Root user (UID 0) always appears as lingering
- Non-existent users will show error messages
- Network users might have different behavior
Lingering in systemd determines whether user services should continue running after the user logs out. This feature is particularly useful for:
- Long-running background services
- User-specific daemons
- Persistent development environments
While loginctl enable-linger and loginctl disable-linger are well-documented, checking the current status isn't as straightforward. Here's how to properly query it:
# Check lingering status for current user
loginctl show-user $(whoami) | grep Linger
# Check for specific user
loginctl show-user foo | grep Linger
The command returns one of two possible values:
Linger=yes: Lingering is enabledLinger=no: Lingering is disabled
For situations where loginctl isn't available, you can inspect the underlying files:
# Systemd creates this file when lingering is enabled
ls /var/lib/systemd/linger/ | grep foo
# Or more directly:
test -f /var/lib/systemd/linger/foo && echo "Enabled" || echo "Disabled"
Here's a bash function to automate the check:
is_linger_enabled() {
local user=$1
if [[ $(loginctl show-user "$user" | grep -oP 'Linger=\K\w+') == "yes" ]]; then
return 0
else
return 1
fi
}
# Usage example:
if is_linger_enabled "foo"; then
echo "User foo has lingering enabled"
else
echo "User foo has lingering disabled"
fi
If you encounter problems:
- Ensure you have proper permissions (typically requires root)
- Verify the user exists in the system
- Check if systemd is running with
systemctl status systemd-logind