I'm using Xubuntu 12.04. In the Power Manager, I have checked Extended -> Lock screen when going for suspend/hibernate. If I choose to suspend from the main menu / Panel 1, the screen is locked. However, if I use dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend
(see How can I suspend/hibernate from command line?; I use this command via a keyboard shortcut), the screen is not locked. How can I (1) suspend the system (2) have the screen locked at the same time (3) do not require root rights to do so [since I would like the command to be assigned to a keyboard shortcut].

- 1,669
4 Answers
You can do that by using the following:
gnome-screensaver-command --lock && dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend
In a keyboard shortcut you need to put sh -c "<command>"
replacing <command>
with the command above, like:
sh -c "gnome-screensaver-command --lock && dbus-send --system --print-reply --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
If the above command does not work, you can also try using a shell script file (for example suspend_lockscreen.sh
). First create the file call the following commands in a console:
cd ~
echo "gnome-screensaver-command --lock" >> suspend_lockscreen.sh
echo "dbus-send --system --print-reply --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend" >> suspend_lockscreen.sh
chmod +x suspend_lockscreen.sh
That will create a file named suspend_lockscreen.sh in the user home folder. Then, in the shortcut just call: ./suspend_lockscreen.sh
.
Note: gnome-screensaver-command
can be replaced by xscreensaver-command
, as pointed out in one comment.
I was using amfcosta's dbus command to suspend from the command line. However, upon resume, eth0 was down.
I then found this command:
xfce4-session-logout --suspend
This command disables networking, suspends, and then re-enables networking when the computer wakes up.
xfce4-session-logout
is xfce specific, but other desktop environments may have similar *-session-logout
commands.
I am running Xubuntu 14.04.
Update: This may not affect others, but it turns out that on my Asus EEE 900, the wired ethernet connection only works every other time the EEE wakes up. This is, of course, very annoying, and means that suspend is unusable.

- 1,355
With Ubuntu 15.10, this works as well:
gnome-screensaver-command --lock && systemctl -i suspend

- 446
Note: the xscreensaver-command
only works with option -lock
(with 1 hyphen, not a double as with the gnome-screensaver-command
). Yet, the command only works when the screensaver is running, so not very useful.

- 71
gnome-screensaver-command
can also be replaced byxscreensaver-command
so no additional installation is required. Without the&& dbus-send...
part one simply gets a lock (without suspend); with it, one gets a suspend with lock on wake-up. Perfect. – Marius Hofert May 17 '12 at 23:34i3lock
instead. Given that all it does is lock the screen, I think it should avoid whatever interaction is going on here with the screensaver programs. – Chan-Ho Suh May 18 '12 at 23:35xflock4
instead ofxscreensaver-command --lock
– Marius Hofert May 19 '12 at 06:02