13

I lock my computer a lot, and was wondering if there was a way to get the lock screen when I hit Ctrl + Alt + L. Right now when I use that key combination it turns the screen black, and I have to hit a key or click to bring up the screen where it shows the option to enter your password or switch users, as well as displaying the desktop background behind it.

Is there anyway to avoid having to hit that extra key, to bring up the screen I want? And is there anyway to keep it that way? It's really a matter of personal preference, and I don't see any reason why not to have it stay that way as my monitor eventually is turned off and/or my computer suspended.

1 Answer 1

18

Unfortunately, the lock-screen utility xdg-screensaver lock (formerly gnome-screensaver-command), which the Ctrl+Alt+L shortcut uses, will also activate the blank-screen "screensaver" whenever it is told to lock the screen.

The trick then is to "fake" a keystroke immediately after the screen is locked to get rid of the blank-screen and leave the lock-screen on. We'll do this with xdotool, a small command-line utility which allows automation of keyboard and mouse clicks/movements.

  1. Install xdotool with

    sudo apt-get install xdotool
    
  2. Paste the following to a file in your home directory, such as locknoblank.sh:

    #!/usr/bin/env bash
    gnome-screensaver-command -l
    xdotool sleep 1 key Ctrl
    
  • This simply locks the screen normally, but one second later, sends a dummy Ctrl keystroke to "unblank" the screen.

  • Make the file executable with

    chmod +x ~/locknoblank.sh
    
  1. Go to System Settings...Keyboard...Shortcuts and remove Ctrl+Alt+L as the lock-screen shortcut so we can re-use it for our custom command. Click on "System" on the left sidebar, then click on the right of "Lock Screen" where it says "Ctrl+Alt+L"; it will now say "New Accelerator...". Press Backspace to remove the shortcut.

    screenshot

  2. Now go to Custom Shortcuts on the left, and add a shortcut, naming it whatever you want to with the command as /home/username/locknoblank.sh. Click Apply, then click on its right where it says "Disabled" to get "New Accelerator", and press Ctrl+Alt+L.

    screenshot

Close System Settings, and you're done!


To prevent reactivation of the screensaver (idle timeout)

Open a terminal, and type/paste:

gsettings set org.gnome.desktop.session idle-delay 7200  # Blank Screen Delay

This sets the screensaver idle activation time to 2 hours (or 7200 seconds). You can set it to anything you want; the default is 600 (10').

screenshot

Screen lock

For Ubuntu 18.04+, use

xdg-screensaver lock

On older versions was gnome-screensaver-command -l.

5
  • Thank you very much! It worked perfectly... at first. I also asked if there was a way to keep it that way for a while (e.g. 1 hour). I think the screen saver reinitialized after a minute or two. I'm new to scripting, but is there perhaps a way to loop the fake keystrokes for, say, and hour? Or maybe I could disable/lengthen the time it takes for the screensaver to reinitialize? I looked around for a configuration file and couldn't find one.
    – user81572
    Aug 6, 2012 at 23:12
  • Sorry, I missed this one. Please see edit at the bottom of the answer on how to prevent screensaver reactivation.
    – ish
    Sep 26, 2012 at 23:55
  • Thanks for the tip, but it sadly doesn't seem to work for me. I'm new to Linux, but an avid programmer (why I moved to Linux) and I made a for loop in the bash script that loops the xdotools. However, the shortcut didn't work and I find it a bit cumbersome to lock my computer by running a script. Is there anyway to just do away with the gnome screensaver? I don't really see a use for it, as I usually just close the lid or turn my laptop off when not using it. I'm not familiar with gsettings as well, so I don't know where I could find the proper node.
    – user81572
    Oct 5, 2012 at 4:02
  • 3
    Just curious if you are planning on updating this answer for 2018 and Gnome under Ubuntu 18.04? It's come up a few times in Ask Ubuntu and I've started researching it. I like most people will be using 18.04 soon and have been playing with Gnome DM lately... Jul 10, 2018 at 23:30
  • 4
    For Ubuntu 18.04, use xdg-screensaver lock instead of gnome-screensaver-command -l
    – Dejv
    Aug 3, 2018 at 4:50

You must log in to answer this question.

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