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

  • 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
    Commented Jul 22, 2012 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
    Commented Jul 22, 2012 at 21:18
  • Have you run Ubuntu updates yet?
    – david6
    Commented Jul 22, 2012 at 23:50

3 Answers 3


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.


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.


Here is a workaround if you still need it:

su <user> -c 'export DISPLAY=:0 && gnome-screensaver-command -l'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .