Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am in need to lock my screen with a bash command. The commands below will provide a blank screen, however the desktop is brought back without a password prompt. Is there a better way to do this? Also, is there a setting to offer a password prompt on hibernation resume?

! gnome-screensaver-command --lock gnome-screensaver-command -l

share|improve this question
Which Ubuntu version? Are you looking to add a Unity icon, to lock-screen? (I've already done that.) Do you also want a (graphic) screensaver, and are using Gnome screensaver? – david6 Jul 22 '12 at 0:32
Opps, it is Ubuntu 12.04 and Unity. Both commands work, and I have the "password lock" "on" with the box checked for "Require my password when waking from suspend" in the settings. It still does not prompt me for a password. Also, this is a brand new installation. – surgemcgee Jul 22 '12 at 21:18
Have you run Ubuntu updates yet? – david6 Jul 22 '12 at 23:50

Well, this is embarrassing but I found out why the password prompt is not working. These commands need to be run as user who owns the Unity session. My script is ran as user root. The gnome-screensaver-command -l command works now, but the pm-suspend still requires root privileges. So I will need to have a work around for that.

share|improve this answer

When I run gnome-screensaver-command --lock, it prompts me for a password.

If you're using Precise, try going to System Settings > Brightness and Lock > and then see if the settings there help. I have lock on and have it set to prompt for a password after suspend, both of which seem to work as intended.

share|improve this answer

Here is a workaround if you still need it:

su <user> -c 'export DISPLAY=:0 && gnome-screensaver-command -l'
share|improve this answer

Your Answer


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.