6

My screen doesn't get locked after I close my laptop lid. The laptop suspends but the screen is not locked.

I checked multiple settings:

gnome-control-center > brightness & lock > lock on and require password when waking from suspend enabled
dconf-editor > org/gnome/desktop/lockdown/disable-lock-screen disabled
dconf-editor > org/gnome/desktop/screensaver/lock-enabled enabled
dconf-editor > org/gnome/desktop/screensaver/ubuntu-lock-on-suspend enabled

But can't get to lock my screen after suspend. What else can I try?

Tim
  • 32,861
  • 27
  • 118
  • 178
simao
  • 281

3 Answers3

3

Not a solution, but you may use this script as a workaround:

#!/bin/bash
qdbus com.canonical.Unity /com/canonical/Unity/Session com.canonical.Unity.Session.Lock &&
qdbus com.canonical.Unity  /com/canonical/Unity/Session com.canonical.Unity.Session.Suspend

This works with stock Ubuntu with Unity desktop. The approach is specific to Unity, although there are dbus calls to suspend regardless of the desktop environment. For 16.04 , one could use systemctl suspend command. See the relevant post: https://askubuntu.com/a/1795/295286

Alternative (older answer, edited)

One of the other tools for suspending, that works regardless of the Ubuntu version is pm-suspend. Problem is that it requires root privilege. To work around that, add pm-suspend to /etc/sudoers file to be executed without password ( more info here ).

In particular, in my /etc/sudoers I have the following:

# Allow using pm-suspend for my user without password
serg ALL = NOPASSWD: /usr/sbin/pm-suspend

Thus, you can use:

sh -c 'gnome-screensaver-command -l && sudo pm-suspend'

The sudo pm-suspend won't prompt you for password then.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Hi, it didn't work for me. Using Ubuntu 14.04. It suspends, but that's it. No locking. – Akeshwar Jha Feb 06 '17 at 06:38
  • @AkeshwarJha Interesting. Check if you have gnome-screensaver-command. It's one of the standard ones. Are you using the stock Ubuntu with Unity desktop ? – Sergiy Kolodyazhnyy Feb 06 '17 at 06:49
  • yes. I'm using the stock Ubuntu with Unity desktop. I ran gnome-screensaver-command -l independently and it did lock my screen. i ran 'sudo pm-suspend' independently, and it suspended my system without lock. But combining these two only suspends my computer, doesn't lock it. – Akeshwar Jha Feb 06 '17 at 07:10
  • 1
    @AkeshwarJha OK, so I figured out that when one tries to run gnome-screensaver-comand -l as root, it tries to lock for root's GUI session, which isn't running. I've edited my answer accordingly and added alternative solution with qdbus. That one works with Unity and doesn't require no sudo password. – Sergiy Kolodyazhnyy Feb 06 '17 at 07:46
  • Here's an alternative one-liner that both locks the screen and suspends the computer in one command. This keeps me from having to edit the /etc/sudoers file. Good answer, by the way. Upvoted. – Gabriel Staples Jun 26 '23 at 06:22
0

How to lock the screen and put the computer to sleep in a 1-liner from the command line (could be assigned to an Ubuntu shortcut key)

Tested on Ubuntu 22.04.

This isn't an answer directly to the question, but it may help some people:

You can lock the screen and put the computer to sleep in a single command like this:

# lock the screen and put the computer to sleep
sudo true && gnome-screensaver-command -l && sudo pm-suspend

You can assign the above command to a shortcut key if you want to quickly put your computer to sleep.

After putting your computer to sleep with the command above, you can then wake up your computer and verify it went to sleep like this:

journalctl -n 1000 -e | grep "PM: suspend"

You'll see some suspend entries. Here's an example run and output for me. Check the timestamps to ensure this is for the suspend you just did:

$ journalctl -n 1000 -e | grep "PM: suspend"
Jun 25 23:07:02 gabriel-my-computer-name kernel: PM: suspend entry (s2idle)
Jun 25 23:07:17 gabriel-my-computer-name kernel: PM: suspend exit

Explanation of the first command above, and how it works:

  1. sudo true prompts your user for a password to run sudo. Once you type in your sudo password, it gets temporarily cached. true is a dummy command to always pass. So, on to the next command.
  2. gnome-screensaver-command -l runs as non-sudo, locking the screen for your user's session.
  3. Now that the screen is locked, sudo pm-suspend runs to suspend the system. It uses your cached sudo password that you entered and which was cached a moment ago when you ran sudo true.

The trick is to use sudo true first, to cached your password, since obviously once the screen is locked, you cannot type the password, and gnome-screensaver-command -l must run as non sudo!

What not to do

Don't do this! It will freeze your computer and require a hard reboot where you have to hold down your power button:

# bad: this locks up your computer; freezes laptop!; requires hard reboot
sudo pm-suspend && gnome-screensaver-command -l

References

  1. Where I first learned about sudo pm-suspend, and journalctl, in general: https://learnubuntumate.weebly.com/draining-battery.html

See also

  1. My shorter answer which references this one: How can I suspend/hibernate from command line?
0

I was facing similar issue this answer solved my issue. Install Unity Tweak Tool and in System > Security all the options should be unticked. The Desktop Lock was ticked in my case uncheck this.

uncheck

If the above mentioned option is ticked then in System Settings > Brightness and Lock the lock option is disabled which causes all the problem.

lock option

sempaiscuba
  • 1,433