101

I am using Ubuntu 12.04. Is there any way to lock the screen or session from a terminal command or script?

Zanna
  • 69,223
  • 56
  • 216
  • 327
Raja G
  • 100,643
  • 105
  • 254
  • 328

11 Answers11

101

Simple:

gnome-screensaver-command -l

The following can also work, if the screensaver is set to lock when activate (see screensaver settings), since the command activates the screensaver:

gnome-screensaver-command -a

You can add an alias to the command by editing the file .bashrc (or .bash_aliases) in your home directory:

gedit $HOME/.bashrc

and adding the following line:

alias lock='gnome-screensaver-command -l'

Then from terminal:

source .profile

This will activate the alias. From now on, the alias lock in a terminal will have the effect of locking the screen.

muru
  • 193,181
  • 53
  • 473
  • 722
January
  • 35,223
  • 15
  • 82
  • 101
  • Sorry, I had to modify your changes. (i) no need to use sudo (ii) better .profile than .bashrc, since .bashrc gets called every time you create a new shell and (iii) alias works only when called from shell. – January Sep 21 '12 at 14:18
  • ok now its looking good , you like the idea ? – Raja G Sep 21 '12 at 14:50
  • 1
    sure. I usually lock with a single keypress, though, makes it quicker when leaving the office for a coffee. – January Sep 21 '12 at 14:56
  • I think if we add more answer about creating a custom-shortcut key will improve , what do you say ? – Raja G Sep 21 '12 at 14:57
  • 3
    Doesn't work, on my Linux mint mate, instead `xdg-screensaver lock` works. – Eric Jun 02 '21 at 12:50
  • 1
    @user218867 perfect for Pop_OS too. Another command is `loginctl lock-session`. To unlock the screen `loginctl unlock-session`. – aasutossh Sep 08 '21 at 07:21
  • `gnome-screensaver-command` doesn't appear to be installed but `xdg-screensaver lock` confirmed to work on Ubuntu 22.04. – Batwam Jul 09 '22 at 16:13
  • On Ubuntu 22.04, `gnome-screensaver-command` didn't come installed. I had to install it first with `sudo apt install gnome-screensaver`. – Gabriel Staples Jun 26 '23 at 05:29
33

In addition to what January said, this also works:

gnome-screensaver-command --lock

or

gnome-screensaver-command -l

According to the gnome-screensaver-command man page...

-l, --lock                 Tells the running screensaver process to lock the screen immediately
-a, --activate             Turn the screensaver on (blank the screen)

For further clarification, here is another question/answer (also by January) which describes the differences between invoking the lock and activating your screensaver:

Difference between gnome-screensaver-command -a and gnome-screensaver-command -l

Aaron
  • 6,646
  • 5
  • 34
  • 48
24

Starting in Ubuntu 14.04, Unity's lock screen no longer uses gnome-screensaver. The command gnome-screensaver-command -l will still work in most cases, but see this question for exceptions.

If that command does not work (say, for instance, that gnome-screensaver is not installed), bringing up the proper Unity lock screen (not the greeter where you can switch users) can be done via this command in a terminal:

dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
David Foerster
  • 35,754
  • 55
  • 92
  • 145
Fern Moss
  • 8,685
  • 6
  • 42
  • 61
18

Please install vlock. Then you can switch to a VT (text terminal, using Ctrl+Alt+F1) and run

vlock -a -s

This works whether you have X11 running or not.

Peachy
  • 7,077
  • 10
  • 37
  • 45
Anand Vaidya
  • 191
  • 2
  • 1
    looking nice . ok I'll try and let you know . thank you . – Raja G Sep 14 '12 at 07:14
  • 1
    ok its looking good , but suits only for TTY . – Raja G Sep 14 '12 at 12:59
  • Version `2.0.4` of `vlock` does not seem to have the `-s` switch anymore. Just running `vlock -a` works fine. – friederbluemle Nov 27 '17 at 11:08
  • In Version 2.2.2, the `s` flag exists and is defined as `-s or --disable-sysrq: disable SysRq while consoles are locked to prevent killing vlock with SAK`. `SAK` is the secure attention key, which protects against trojan password capturing according to [kernel.org](https://www.kernel.org/doc/Documentation/SAK.txt). In a tty the `a` flag is enough to password protect. – Timo May 31 '20 at 04:56
16
$!(sleep 10s ;  xset dpms force suspend) & xdg-screensaver lock

this starts the screen saver in locked mode and then puts your display in standby. sweet and simple, no sudo. command line or shell script works fine. i use this for a keyboard hotkey. Ubuntu Mate 15.10

muru
  • 193,181
  • 53
  • 473
  • 722
blazedego
  • 171
  • 1
  • 3
  • Works on Kubuntu 16.04 too. – jippie Oct 23 '16 at 09:52
  • ty for xdg-screensaver lock, +1, however your answer didn't do the trick on ubuntu 16.10 gnome 3, i use: `xdg-screensaver lock && sleep 2s && sudo pm-suspend`. i have this aliased, and also with pm-suspend in my sudoers.d: `yourusername yourhostame = NOPASSWD: /usr/sbin/pm-suspend` – zamnuts Jan 03 '17 at 07:52
  • `ERROR: Unknown command 'lock'` in `Lubuntu 18.4. LTs` – Timo Jun 05 '20 at 04:20
  • Thank you - this works without installing gnome screen save – Amin Jul 14 '20 at 19:52
15

In case that you need to lock the screen on a terminal in a non-GUI environment you could make use of screen

While in screen press the following key combinations to lock the terminal.

Ctrl + a

Ctrl + x

pl1nk
  • 6,229
  • 5
  • 26
  • 46
12

In my case xdg-screensaver lock is works perfectly fine. Also I save it by the setting or when I press window+L it will lock the screen immediately

Zanna
  • 69,223
  • 56
  • 216
  • 327
HeRo
  • 121
  • 1
  • 2
8

For LightDM users, try dm-tool switch-to-greeter.

user10962
  • 2,489
  • 2
  • 16
  • 12
  • 1
    `dm-tool lock` seems better as that just locks the screen. "switch to greeter" ended my session when I tried it. – pbhj Sep 07 '15 at 16:19
  • Using this in a cron job does not work? Script : lock_script.sh `#!/bin/sh dm-tool switch-to-greeter` Cron-job : `42 9 * * * /home/user/lock-script.sh` – Dinesh May 04 '17 at 07:43
  • `dm-tool` is not very safe. Not on my system, anyway. Try going to a text TTY (Alt-Ctrl-F2) then back to your X TTY (Ctrl-Alt-F7 for me). It makes the lock screen disappear! – Rolf May 30 '17 at 15:16
6

A dirty hack of using the shortcut Ctrl+Alt+L for the locking the screen from a terminal:

  1. Install xdotool from the software center or from the terminal as follows:

    sudo apt-get install xdotool
    
  2. Type the following to lock the screen from the terminal:

    xdotool key Ctrl+alt+l
    

Refer to the manual page for xdotool for more.

jobin
  • 27,142
  • 16
  • 100
  • 116
  • This was definitely the best - because the other "screen-saver" based ones gave you a different "lock screen" which ONLY allows you to enter a password (no changer-user, logout, restart, etc). Also: May want to do stuff like: DISPLAY=:0 sudo -u username xdotool key Ctrll+alt+l – Brad Nov 18 '17 at 16:01
  • nothing happens in dwm – holms Mar 16 '21 at 22:41
4

It depends on your display manager. I have lightdm, I can do dm-tool lock to bring up a lock screen. It's not really secure, though, I found an easy way to bypass it.

Rolf
  • 1,911
  • 1
  • 15
  • 12
1

A similar situation to lock the screen in lubuntu

in lubuntu 17.10 (not ubuntu) this works xset dpms force off

ptetteh227
  • 1,834
  • 14
  • 26