59

There is no "Lock" option showing up in the user menu, and the standard shortcuts (Ctrl+L or Ctrl+Alt+L) don't do anything.

I'm running Fedora 19 with Gnome Shell 3.8.3, and XDM 1.1.11.

I'm using XDM because of broken XDMCP support in GDM - but before I upgraded to Fedora 19, I did have the lock option, even when using XDM.

I've posted an answer reflecting the results of my own research. It basically says that it's not possible to have screen-lock integrated into Gnome 3.8 without running GDM.

I really hope there's a better answer available though - so please add your own answer if there's any way to do this that I overlooked.

7
  • 1
    Try Ctrl + Alt + L. Does it work?
    – m0nhawk
    Aug 10, 2013 at 18:07
  • 1
    @m0nhawk No, that doesn't work either. I've updated the question to reflect that.
    – jcsanyi
    Aug 10, 2013 at 18:09
  • Strange, it works for me. And what is set in Settings - Keyboard - Shortcuts - System for Lock screen?
    – m0nhawk
    Aug 10, 2013 at 18:10
  • @m0nhawk on Gnome 3.8 without GDM?
    – jcsanyi
    Aug 10, 2013 at 18:10
  • 2
    @m0nhawk The shortcut is set to Ctrl + L - but that doesn't do anything.
    – jcsanyi
    Aug 10, 2013 at 18:14

15 Answers 15

45

After some research, I think I've got enough information to post an answer to my own question.

In Gnome Shell 3.6 and earlier, the old gnome-screensaver program was present, and if GDM was not running, gnome-screensaver would be invoked - allowing you to lock the screen.

Starting in Gnome Shell 3.8 (included in Fedora 19), gnome-screensaver support has been dropped completely. This was done for three reasons: reduced code complexity coupled with the fact that the screensaver is seen as an unneeded feature, and the fact that the eventual move to Wayland will require screensaver, locking, etc. support to be in the compositor.

So the only Gnome-integrated way of locking the screen is to have GDM running, which will respond to a dbus message telling it to lock the screen. Other display managers (such as XDM) have not been designed to respond to this dbus message, and so the screen cannot be locked.


From this link:

In old versions of gnome the command gnome-screensaver-command -l would lock your screen. As gnome-screensaver is no more in gnome 3.8 you now have to send a dbus call. I think this is then handled by GDM.

$ dbus-send --type=method_call --dest=org.gnome.ScreenSaver \
    /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
3
  • 3
    Thanks @slim, but the commands you edited in don't actually do anything unless you're running GDM - they don't help answer the question.
    – jcsanyi
    Jan 4, 2014 at 4:02
  • 2
    Sure, I'd still like to see the contents of the link brought in, in case that site goes away.
    – slm
    Jan 4, 2014 at 4:09
  • @slim good point. I edited the contents back in as a quote.
    – jcsanyi
    Jan 4, 2014 at 4:43
23

Super+L works in Fedora 24 which is running Gnome 3.20

2
  • 5
    this works on ubuntu gnome too, thanks :)
    – daveoncode
    Apr 26, 2017 at 8:52
  • Also OK on Centos7 / 3.28, good to know.
    – tonioc
    Jun 6, 2018 at 11:53
17

This works for me in Gnome 3.14. Standard installation, no customization.

gnome-screensaver-command --lock
2
  • It need to be installed in Gnome 3.26.2, so if you don't have it, sudo apt-get install gnome-screensaver.
    – Right leg
    Jun 1, 2018 at 9:17
  • Did not work for me, gdm not running because a recent update broke it (had to switch to sddm...) Oct 4, 2018 at 9:12
15

How to enable screen locking in Gnome Shell (3.14) using xscreensaver (on Debian):

(Note: Although the gnome-screensaver package currently exists on Debian sid, it does not seem to be usable)

  1. Install the xscreensaver package (and optionally xscreensaver-data and xscreensaver-data-extra)
  2. Run xscreensaver to configure the screensaver how you want it. Be sure to select 'Lock screen after 0 minutes'.
  3. In the GNOME keyboard settings, select the Shortcuts tab, then 'Custom Shortcuts'
  4. Click the + button, then enter whatever you want as the name and xscreensaver-command -l as the command. Click OK.
  5. Click 'Disabled' and type the key combination you would like to lock the screen (mine is Ctrl-Alt-L). Make sure this does not conflict with the GDM 'Lock Screen' shortcut under 'System'.
  6. Enjoy your lockable screen.

Caveat: Notification popups will still be visible over the locked screen. If this is a problem for you, you will need to find another solution like the LightDM Lock Screen extension.

2
  • 2
    Thanks for providing this option. This is the only one that ended up working for me in Gnome 3.14.1 on Debian Jessie
    – sanimalp
    Sep 8, 2016 at 16:47
  • Since I wrote this answer, I have discovered a different option that involves using mate-screensaver (essentially the same as the old GNOME screensaver). This requires writing a fake session manager as described here).
    – Joel Cross
    Nov 18, 2016 at 14:29
6

A work around I successfully used in debian, ubuntu and rhel7 is creating a short python script that's started automatically when you log into the gnome session. Note that in rhel7 the key combination to lock is super + l.

Create /etc/xdg/autostart/gscreenlock.py like this:

#!/usr/bin/python

import dbus
import dbus.service
import dbus.glib
import gobject
import os

class ScreenDbusObj(dbus.service.Object):
    def __init__(self):
        session_bus = dbus.SessionBus()
        bus_name=dbus.service.BusName("org.gnome.ScreenSaver",bus=session_bus)
        dbus.service.Object.__init__(self,bus_name, '/org/gnome/ScreenSaver')

    @dbus.service.method("org.gnome.ScreenSaver")
    def Lock(self):
        os.system( "xscreensaver-command -lock" )


if __name__ == '__main__':
    object=ScreenDbusObj()
    gobject.MainLoop().run()

Then make it executable:

chmod a+rx /etc/xdg/autostart/gscreenlock.py

And edit /etc/xdg/autostart/gscreenlock.desktop like this:

[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=gscreenlock
TryExec=/etc/xdg/autostart/gscreenlock.py
Exec=/etc/xdg/autostart/gscreenlock.py
NoDisplay=true
NotShowIn=XFCE;KDE;
Comment=Allows screen locking in gnome
5

@jcsanyi gave a background explanation of how it works.

For those folks who aren't able to use GDM3 due to various reasons (say, this bug) and switched to LigthDM there is a command we can use to lock the screen(possibly it could work with other DM too, I haven't checked though) :

dm-tool lock

To make it more convenient go to settings -> Keyboard -> Scroll to the bottom -> Click "+" button -> Create new custom shortcut using the command above like this.

enter image description here

To use Super+L combination you probably have to reassign existing system Lock screen shortcut to some other combination.

2
  • This worked for me on Fedora 30 Gnome 3.32.2. But I wonder why my Fedora has lightdm as the default running display manager instead of GDM, and which program the default lock screen shortcut Super+L is sent to since though it's present in Settings but not working anyway. Aug 16, 2019 at 15:52
  • This worked for me on Debian 10
    – Kenairod
    May 29, 2020 at 15:39
4

This answer is based on Fedora 30 GNOME 3.32.2

For Linux desktops usually the default shortcut key for locking screen is Super-L or Ctrl-Alt-L. But sometimes they may not work and you can't find a way to lock the screen. This has to do with which display manager the machine is currently running. You can check that by running $ systemctl status display-manager. If you see gdm then the default shortcut should work because this shortcut is handled by GDM - GNOME Display Manager. There are other types of display managers such as lightdm, sddm, lxdm, kdm, and xdm. The default shortcut may not work if you are running one of them. However, you can always try to lock by running $ dm-tool lock and if it works you can add a custom shortcut for this command. You may want to switch to another display manager, for example, from lightdm to gdm, and if so do below:

$ dnf install gdm
$ systemctl disable lightdm
$ systemctl enable gdm
$ reboot
1
  • Worked on ArchLinux GNOME 40. Disabled ssdm
    – insign
    Oct 20, 2021 at 8:12
3

Screen locking was handled by gnome-screensaver up until GNOME 3.8. The application has been deprecated by the GNOME team and it functionality has been divided up among gnome-shell, gnome-session, & GDM.

If you just want to get things working you might have better luck using xscreensaver.

3

If you are in Arch Linux: yay slock

2

I also ended up asking myself the same question in 2020 (Fedora 32, Gnome 3.36 on X11), since the current version of GDM appears to have a very annoying bug: My Gnome session freezes up, but I still can move the mouse cursor. Just google for "gnome freeze except mouse". Once I switched to SDDM these issues do not occur anymore. However, now I need to take care about the screen lock by myself, as this is usually what GDM does for you.

Luckily, we still have gnome-screensaver around:

sudo dnf install gnome-screensaver

To make it autostart, we can use the .desktop file from the answer by the user aseq

sudo nano /etc/xdg/autostart/gscreenlock.desktop
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=Gnome screen saver
TryExec=/usr/bin/gnome-screensaver
Exec=/usr/bin/gnome-screensaver
NoDisplay=true
NotShowIn=XFCE;KDE;
Comment=Allows screen locking in gnome

Once gnome-screensaver is running, locking the screen via Super+L works out of the box. I didn't figure out how to change the current lock screen background, but I'm also fine with the default one.

Now the really tricky thing is to make gnome-screensaver lock the screen after resuming from suspend/hibernate. Combining pieces of information from here and here I managed to get the following working.

This is the script for calling gnome-screensaver before suspending

sudo nano /opt/lock-screen.sh
#!/bin/bash

#https://github.com/mikebdotorg/gnome-screensaver-lock/blob/master/lock

# have to grab the newest gnome-session so we don't grab gdm's d-bus session information by mistake
gsPid="`pgrep -n gnome-session | egrep '^[0-9]+$'`"
if [ -z "$gsPid" ]
then
    echo "gnome-session does not appear to be running" 1>&2
    exit 1
fi

export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS="`grep -z ^DBUS_SESSION_BUS_ADDRESS= /proc/${gsPid}/environ | cut -f2- -d=`"

/usr/bin/gnome-screensaver-command --lock; sleep 2
sudo chmod +x /opt/lock-screen.sh

And this is the systemd unit that executes our script

sudo nano /etc/systemd/system/lock-on-wakeup.service
[Unit]
Description=Lock screen when waking up
Before=sleep.target suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

[Service]
User=vs
Type=oneshot
ExecStart=/opt/lock-screen.sh

[Install]
WantedBy=sleep.target suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
sudo systemctl enable lock-on-wakeup.service
sudo systemctl daemon-reload

You can then use systemctl suspend

to test that it works as expected. Hope it helps.

1

I figured out a workaround that works for me on Debian Buster with GNOME Shell version 3.30 and with Lightdm. I don't have any idea if this breaks anything but I haven't noticed anything unusual.

  1. Install package cinnamon-screensaver
  2. Add /usr/bin/cinnamon-screensaver to startup applications
  3. Create a keyboard shortcut for the command: cinnamon-screensaver-command -l
1

I use a custom Fedora-based distro that uses Elogind+Eudev instead of SystemD, so GDM is not an option as screenlocker. My solution was using cinnamon-screensaver and a python3 script based on the (deprecated) python2 script posted by aseq.

Save this as /etc/xdg/autostart/gnome-shell-screenlock.desktop:

[Desktop Entry]
Type=Application
Name=GNOME Shell Screenlock
TryExec=/usr/bin/gnome-shell-screenlock.py
Exec=/usr/bin/gnome-shell-screenlock.py
NoDisplay=true
OnlyShowIn=GNOME;
Comment=Allows screen locking in GNOME Shell
X-GNOME-Autostart-Delay=8
X-GNOME-Autostart-Phase=Application
X-GNOME-Autostart-Notify=true
X-GNOME-Bugzilla-Bugzilla=GNOME
X-GNOME-Bugzilla-Product=cinnamon-screensaver
X-GNOME-Bugzilla-Component=general
X-GNOME-Bugzilla-Version=4.6.0
X-Desktop-File-Install-Version=0.24

Save this as /usr/bin/gnome-shell-screenlock.py:

#!/usr/bin/python3

import dbus
import dbus.service
import dbus.glib
from gi.repository import GObject
import os

class ScreenDbusObj(dbus.service.Object):
    def __init__(self):
        session_bus = dbus.SessionBus()
        bus_name=dbus.service.BusName("org.gnome.ScreenSaver",bus=session_bus)
        dbus.service.Object.__init__(self,bus_name, '/org/gnome/ScreenSaver')

    @dbus.service.method("org.gnome.ScreenSaver")
    def Lock(self):
        os.system( "cinnamon-screensaver-command --lock" )


if __name__ == '__main__':
    object=ScreenDbusObj()
    GObject.MainLoop().run()

Make /usr/bin/gnome-shell-screenlock.py executable:

chmod +x /usr/bin/gnome-shell-screenlock.py

Save this as /etc/xdg/autostart/gnome-shell-screensaver.desktop:

[Desktop Entry]
Type=Application
Name=GNOME Shell Screensaver
Comment=Launch screensaver and locker program in GNOME Shell
Icon=preferences-desktop-screensaver
Exec=cinnamon-screensaver
OnlyShowIn=GNOME;
NoDisplay=true
X-GNOME-Autostart-Delay=6
X-GNOME-Autostart-Phase=Application
X-GNOME-Autostart-Notify=true
X-GNOME-Bugzilla-Bugzilla=GNOME
X-GNOME-Bugzilla-Product=cinnamon-screensaver
X-GNOME-Bugzilla-Component=general
X-GNOME-Bugzilla-Version=4.6.0
X-Desktop-File-Install-Version=0.24

Install cinnamon-screensaver and make sure python3-gobject and python3-dbus are installed.

Logout and login again. Wait until GNOME Shell completes start. Press Super+l. You're welcome.

0

The default shortcut is Shift+Ctrl+Alt+Lock.

You can find this, and edit using "Shortcut" tab in "Keyboard" app.

2
  • See comments. "The shortcut is set to Ctrl + L - but that doesn't do anything."
    – sourcejedi
    Jun 6, 2018 at 11:33
  • If your answer is that this has started working again in Fedora version X even when using XDM instead of GDM, you need to specify what that version of Fedora is :). Hi and welcome to StackExchange.
    – sourcejedi
    Jun 6, 2018 at 11:35
0

Without GDM, you can use i3lock for example, which is pretty minimalist. It should be installable from your package manager. This is what I've done:

  1. Create /usr/bin/blurlock, make it executable, it's a shell script containing:
#!/bin/bash
# /usr/bin/blurlock

# take screenshot
import -window root /tmp/screenshot.png

# blur it, grayscale it, delete tempfiles
convert /tmp/screenshot.png -scale 5% -scale 2000% -colorspace Gray /tmp/screenshotblur.png && rm /tmp/screenshot.png && i3lock -i /tmp/screenshotblur.png && rm /tmp/screenshotblur.png

exit 0
  1. Invoke /usr/bin/blurlock with a keybinding by going to "Keyboard Shortcuts" in Gnome, and binding whatever you wish to that command (I have CTRL+ALT+L).
0

Solution of the problem "unable to lock lock was blocked by an application" given by Vbox windows guest on ubuntu 20.04.

This script started by startup apps solves the problem. It can be improved and / or rewritten in python.

#!/bin/bash
#Turn screen off when idle time end, even when I have a Vbox gest window on top (version 1.0)
#References link:
# https://itectec.com/ubuntu/ubuntu-run-command-when-idle-active/
# https://askubuntu.com/questions/62858/turn-off-monitor-using-command-line

dbus-monitor --session "type=signal,interface=org.gnome.SessionManager.Presence,member=StatusChanged" |
  while read x; do
      case "$x" in
        *"uint32 3"*) xset -display :0.0 dpms force off;;
        *"uint32 0"*) xset -display :0.0 dpms force on;;
      esac
  done

Tested on ubuntu 20.04.1 25/12/2020

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .