Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

are there any changes happen in 13.04 ?

share|improve this question
add comment

4 Answers

up vote 34 down vote accepted

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 .profile in your home directory:

gedit $HOME/.profile

and adding the following line:

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

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

share|improve this answer
    
hope you won't mind :D –  raaz Sep 21 '12 at 14:10
    
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 ? –  raaz 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 ? –  raaz Sep 21 '12 at 14:57
add comment

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

share|improve this answer
add comment

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

share|improve this answer
    
Thank you . now I'm in mobile . I'll try and let you know –  raaz Sep 12 '12 at 11:00
add comment

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.

share|improve this answer
1  
looking nice . ok I'll try and let you know . thank you . –  raaz Sep 14 '12 at 7:14
    
ok its looking good , but suits only for TTY . –  raaz Sep 14 '12 at 12:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.