From https://unix.stackexchange.com/a/505644/674

When you're using SSH, you can access the screen you're connected from by using ssh -X or ssh -Y . Programs can perfectly run on a machine and display on another one. So if you run light-locker through SSH on the remote machine, it will lock your local display and not the remote one (just as running gnome-terminal or any other command will run it on the remote machine but display it locally).

if you want the screen to lock again, yes, you'll have to restart it, but be careful to restart it from the X session you want to lock, not from ssh.

I ssh to a machine, and then want to lock its screen. Could anyone tell me why I failed and what I shall do? Thanks.

$ xscreensaver-command -lock
xscreensaver-command: warning: $DISPLAY is not set: defaulting to ":0.0".
xscreensaver-command: no screensaver is running on display :0.0

$ DISPLAY=:0 xscreensaver-command -lock
xscreensaver-command: no screensaver is running on display :0

Not sure how one can tell there is no X server running on the remote host:

$ ps -A | grep -i xorg
  448 ?        00:00:29 Xorg
  605 ?        00:00:27 Xorg
  766 tty7     05:19:26 Xorg
 2175 ?        00:02:58 Xorg
 3343 ?        00:00:24 Xorg
 8520 ?        00:00:28 Xorg
12564 ?        00:00:22 Xorg
24655 ?        00:00:13 Xorg
26692 ?        00:00:28 Xorg
26704 ?        00:00:25 Xorg
26852 ?        00:00:29 Xorg
27034 ?        00:00:23 Xorg
  • Read my answer for you in another question, the X server is not running on your remote machine, so you fail and will never succeed because your remote machine has no screen at all. – 炸鱼薯条德里克 Mar 15 at 17:18
  • 2
    If the server has a gnome graphical desktop, you can lock the screen according to this link. I tested it right now via ssh (from another computer) with and without prefix DISPLAY=:0 as in your question. It works in both cases. – sudodus Mar 15 at 17:26
  • @sudodus (1) The remote is using LXDE and has no gnome-screensaver installed. What command does dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock invoke? (2) How can you unlock the remote desktop? – Tim Mar 15 at 17:53
  • @sudodus unix.stackexchange.com/questions/506567/… – Tim Mar 15 at 18:00
  • @炸鱼薯条德里克 Where is "my answer for you in another question"? How do you know "the X server is not running on your remote machine"? – Tim Mar 15 at 18:00

You need to learn about the workings of X. You have your local machine LOC and ssh to the remote machine REM. You have two X displays: LOC:0.0 and REM:0.0 . When you run ssh -X REM from LOC you create a new virtual X display on REM which is "REM:10.0" and points back to "LOC:0.0". So when you run a program on REM, its graphics get send to REM:10 and from there to LOC:0.0 where they appear.

If you run a VNC server on REM, it will appear as "REM:1.0" (for instance). You can have many different X displays running on the same machine, sending output from applications to different physical or virtual screens.

Now while you're connected on REM, if there is a user logged locally, using "REM:0.0", "REM:1.0" or other X displays (VNC are just special X displays) you can display things on this screen through ssh by changing the value of the special variable "DISPLAY".

If you connect on the local console of a machine, and run echo $DISPLAY you'll get :0.0

If you connect from LOC to REM with ssh and want to display applications you launch on REM's screen (REM:0.0), change the value of DISPLAY:

export DISPLAY=:0.0

Run a program, and it will display on REM screen (if a session is opened by the same user, of course).

In your case, there must be an open session on "REM:0.0" which isn't the case. If the session is ran using VNC, it must be "REM:1.0", so to run xscreensaver there you must set DISPLAY accordingly:

export DISPLAY=:1.0
xscreensaver-command -lock
  • ssh -X simply request sshd to start listening on 6010, sshd doesn't create X display nor provide X server functionality. VNC server is not a display, some VNC server aren't X server, even when they're, VNC is still a completely different thing from X. Login through local console never ensures DISPLAY env. – 炸鱼薯条德里克 Mar 16 at 23:45
  • @炸鱼薯条德里克 X is complex enough to understand I try to keep the explanation simple and to talk only about what OP is trying to do. On Linux/Unix, when you run an autonomous VNC server it can be considered as another X server and behave as such even though it is something else, using a different protocol, etc. and you can manipulate windows exactly the way I described, it works. – wazoox Mar 18 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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