How can I lock the screen using the new lockscreen from the command line?

Ubuntu 14.04 introduces a new lockscreen that looks similar to the login screen.

New lockscreen

I am still able to lock the screen using the command gnome-screensaver-command --lock (as I always did before), which under 14.04 does lock the screen, but:

  • If you are running it from the desktop, it locks the screen using the new lockscreen.
  • If you are running it from a virtual console or via SSH, it locks the screen using the old lockscreen.

I've also tried using xdg-screensaver lock, but it works in the same way as gnome-screensaver-command --lock does (only uses the new lockscreen when it's ran from the desktop).

Running light-locker-command --lock didn't work, as light-locker is not installed.

I guess there must be some differences between running the same command from the desktop and running it via SSH/virtual console, but I was not able to find any difference in the environment variables.

So, my question is: How can I lock the screen using the new Unity lockscreen and not the old lockscreen from the command line?

This question is probably related as it asks for the new command that needs to be used, so here's another question: Why does running the same lock command give different results?

Note that gnome-screensaver is not being used anymore in 14.04, according to this bug report:

That's not really a gnome-screensaver bug, that component stopped being used in trusty.


Solution 1:

I don't have gnome-screensaver installed, and the only command I can find that works is this:

dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock

Solution 2:

The accepted answer has the right idea, except that the lockscreen is related to unity panel, while org.gnome.ScreenSaver is obviously a Gnome-related type of schema,so it may or may not work.

There exists however a dbus interface for Unity, and all we need is to call this method com.canonical.Unity.Session.Lock like so

qdbus com.canonical.Unity  /com/canonical/Unity/Session com.canonical.Unity.Session.Lock

Side note:

The proof of the fact that it's related to unity panel is simple, when you have the screen locked, the process /usr/lib/unity/unity-panel-service will change to /usr/lib/unity/unity-panel-service --lock-mode as observed from tty1.

Solution 3:

I recently found out that xdg-screensaver lock works aswell and is quite a simple solution too!