106

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

Zanna
  • 72,312
Raja G
  • 105,327
  • 107
  • 262
  • 331

11 Answers11

102

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
  • 207,228
January
  • 37,208
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,764
26

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
  • 36,890
  • 56
  • 97
  • 151
Fern Moss
  • 8,925
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,235
  • 10
  • 40
  • 47
17

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
  • 72,312
HeRo
  • 171
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
  • 207,228
blazedego
  • 171
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,667
8

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

user10962
  • 2,619
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
  • 28,567
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
  • 2,229
1

A similar situation to lock the screen in lubuntu

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

ptetteh227
  • 2,044