How do I lock the screen from a terminal?

146,419

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

$!(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

Share:
146,419

Related videos on Youtube

mx7
Author by

mx7

LinkedIn Profile: https://in.linkedin.com/in/raja-genupula-05205531 Endorse me If I helped you. Blog : http://thelinuxmen.blogspot.com/ Ubuntu - CentOS - Fedora - Windows - Severs - IIS - FTP - Security Thank you.

Updated on September 18, 2022

Comments

  • mx7
    mx7 over 1 year

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

  • mx7
    mx7 over 11 years
    Thank you . now I'm in mobile . I'll try and let you know
  • mx7
    mx7 over 11 years
    looking nice . ok I'll try and let you know . thank you .
  • mx7
    mx7 over 11 years
    ok its looking good , but suits only for TTY .
  • January
    January over 11 years
    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.
  • mx7
    mx7 over 11 years
    ok now its looking good , you like the idea ?
  • January
    January over 11 years
    sure. I usually lock with a single keypress, though, makes it quicker when leaving the office for a coffee.
  • mx7
    mx7 over 11 years
    I think if we add more answer about creating a custom-shortcut key will improve , what do you say ?
  • Bohr
    Bohr almost 9 years
    For Tmux, set-option -g lock-command vlock (require vlock)
  • pbhj
    pbhj over 8 years
    dm-tool lock seems better as that just locks the screen. "switch to greeter" ended my session when I tried it.
  • jippie
    jippie over 7 years
    Works on Kubuntu 16.04 too.
  • zamnuts
    zamnuts over 7 years
    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
  • Dinesh
    Dinesh about 7 years
    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
  • Rolf
    Rolf almost 7 years
    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!
  • Brad
    Brad over 6 years
    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
  • friederbluemle
    friederbluemle over 6 years
    Version 2.0.4 of vlock does not seem to have the -s switch anymore. Just running vlock -a works fine.
  • Timo
    Timo almost 4 years
    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. In a tty the a flag is enough to password protect.
  • Timo
    Timo almost 4 years
    ERROR: Unknown command 'lock' in Lubuntu 18.4. LTs
  • Amin
    Amin almost 4 years
    Thank you - this works without installing gnome screen save
  • holms
    holms about 3 years
    holms@debian:~$ xdg-screensaver lock ERROR: Unknown command 'lock'
  • holms
    holms about 3 years
    nothing happens in dwm
  • Eric
    Eric almost 3 years
    This works on Mint mate 20.1, which is based on Ubuntu 20.04.
  • Eric
    Eric almost 3 years
    Doesn't work, on my Linux mint mate, instead xdg-screensaver lock works.
  • electrovir
    electrovir almost 3 years
    The accepted answer uses a command that I don't even have installed on Ubuntu 20.04. This one works without installing anything extra.
  • aasutossh
    aasutossh over 2 years
    @user218867 perfect for Pop_OS too. Another command is loginctl lock-session. To unlock the screen loginctl unlock-session.
  • Admin
    Admin almost 2 years
    gnome-screensaver-command doesn't appear to be installed but xdg-screensaver lock confirmed to work on Ubuntu 22.04.