Ubuntu – How to lock the screen from a terminal?

command linelock-screen

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

Best Answer

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.