How can I lock my screen in Gnome 3 without GDM?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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

gnome-screensaver-command --lock

Method 4

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.

Method 5

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

Method 6

@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.

Method 7

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

Method 8

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.

Method 9

If you are in Arch Linux: yay slock

Method 10

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.

Method 11

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

Method 12

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.

Method 13

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

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

Method 14

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).

Method 15

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x