Unlock login screen using command line
Solution 1:
I'm assuming you have a recent linux system with systemd
(e.g. Ubuntu 16.04 or newer).
If you need to unlock your own session, just run loginctl unlock-session
(no root required because it's your own session). If you have multiple sessions and want to select just one, run loginctl list-sessions
to identify session and then run e.g. loginctl unlock-session c187
.
If you need to unlock all sessions, just run sudo loginctl unlock-sessions
. Note that this will immediately unlock ALL sessions no matter which user is running the screen saver.
If you need more information to identify the correct session, you can try something like this:
loginctl list-sessions --no-legend | while read id rest; do echo; loginctl show-session $id; done
Solution 2:
The problem with executing commands like gnome-screensaver-command
from an SSH session is usually that they don't automatically connect to the appropriate session bus for the active desktop session - usually, setting the DISPLAY variable will fix that, for example these work for me (logged in via SSH as the same user who owns the locked X session, which is on DISPLAY :0):
$ DISPLAY=:0 gnome-screensaver-command -d
to unlock, and
$ DISPLAY=:0 gnome-screensaver-command -l
to lock.
Alternatively, you can toggle the active state using dbus-send
- for example
$ export DISPLAY=:0
$ dbus-send --session \
--dest=org.gnome.ScreenSaver \
--type=method_call \
--print-reply \
--reply-timeout=20000 \
/org/gnome/ScreenSaver \
org.gnome.ScreenSaver.SetActive \
boolean:false
Source: https://people.gnome.org/~mccann/gnome-screensaver/docs/gnome-screensaver.html#gs-examples