1

I have an Ubuntu Desktop machine. My scope is to take a screenshot of this machine via SSH. This machine is playing a video, and I would like to take a screenshot of this video via SSH. I've tried as root user:

root@user-desktop:~# export DISPLAY=:0
root@user-desktop:~# echo $DISPLAY
:0
root@user-desktop:~# scrot screenshot.png
No protocol specified
Can't open X display. It *is* running, yeah?root@user-desktop:~#

Then I've tried with DISPLAY=:0.0. instead of DISPLAY:=0 (I don't know if this matters).

root@user-desktop:~# export DISPLAY=:0.0
root@user-desktop:~# echo $DISPLAY
:0.0
root@user-desktop:~# scrot screenshot.png
No protocol specified
Can't open X display. It *is* running, yeah?root@user-desktop:~#

Then I've done the same attempts as non-root user:

user@user-desktop:~$ export DISPLAY=:0
user@user-desktop:~$ echo $DISPLAY
:0
user@user-desktop:~$ scrot screenshot.png
Invalid MIT-MAGIC-COOKIE-1 keyCan't open X display. It *is* running, yeah?user@user-desktop:~$

user@user-desktop:~$ export DISPLAY=:0.0
user@user-desktop:~$ echo $DISPLAY
:0.0
user@user-desktop:~$ scrot screenshot.png
Invalid MIT-MAGIC-COOKIE-1 keyCan't open X display. It *is* running, yeah?user@user-desktop:~$
LearningC
  • 11
  • 1
  • Hm, first question: you're running on X, and not on Wayland, right? – Marcus Müller Nov 16 '22 at 09:25
  • Maybe your ssh block X connections (frequent, for security reasons: most people do not want that a remote program can access so many thing from the local computer). I recommend you to use some screenshot program on your local machine – Giacomo Catenazzi Nov 16 '22 at 09:41
  • Ssh has no chance to block any x connection here, @GiacomoCatenazzi, I think. The X connection doesn't go across ssh – Marcus Müller Nov 16 '22 at 09:49
  • @MarcusMüller What about sshd configuration: `X11Forwarding`, `AllowTcpForwarding`, etc.? -- ssh builds a tunnel (which eventually several channels). terminal is just one, but we uses a lot ssh to tunnel TCP traffic. – Giacomo Catenazzi Nov 16 '22 at 09:50
  • All irrelevant, since the asker runs the screenshotting program on the machine that is running X, not on a different machine. – Marcus Müller Nov 16 '22 at 09:52
  • So, the x machine requires a `xhost +`, so that external console programs can access X (programs not opened by X) – Giacomo Catenazzi Nov 16 '22 at 09:55
  • Uff, that would tear down all access control and might not be a good idea on a machine where people can log on via ssh – Marcus Müller Nov 16 '22 at 10:10
  • @GiacomoCatenazzi, since he runs `scrot` on the same machine that runs the `X11` and using the local display (he doesn't need to actually display anything on the screen, he uses the `DISPLAY` just so `scrot` could take it's screenshot), he doesn't need `xhost +`. It would only be needed if you tried to run `scrot` from a remote machine using the `DISPLAY` of the host he wants to take the screenshot at. – aviro Nov 16 '22 at 13:10

2 Answers2

0

Could you try this:

ssh -x user@user-desktop<<'EOF'
export XAUTHORITY=$(
    ps -u $(id -u) -o pid= |
    xargs -I{} cat /proc/{}/environ 2>/dev/null |
    tr '\0' '\n' |
    grep -m1 '^XAUTHORITY='
)
export DISPLAY=$(
   ps -u $(id -u) -o pid= |
   xargs -I{} cat /proc/{}/environ 2>/dev/null |
   tr '\0' '\n' |
   grep -m1 '^DISPLAY='
)

scrot screenshot.png
EOF

Based on this

Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
  • 2
    You have few issues here: 1. The result looking inside the `environ` files will be `DISPLAY=xxx` and `XAUTHORITY=xxx`, which means that `DISPLAY` would actually contain `DISPLAY=xxx` (instead of the actual of value of the `DISPLAY` it found) and `XAUTHORITY` will have `XAUTHORITY=xxx`. 2. If the first process it catches with a `DISPLAY` environment variable is `sshd` (or one of its children), it might use the port forwarded by `ssh` instead of the actual local display. – aviro Nov 16 '22 at 12:57
  • 1
    3. The first `ssh` won't work because `scrot` will try to take a screenshot of the `X11` tunnel created by `ssh` instead of the actual local X11 on the destination host. 4. You don't need to use the `-X` flag on your second `ssh`, because you don't want to enable X11 forwarding, since you will change the `DISPLAY` environment variable anyway. It might be even better to disable it with `-x`. 4. – aviro Nov 16 '22 at 13:05
0

You can try and do it in the same environment and as the same user as that of a process running in that X11 session.

sudo -u "$(ps -o user= -p "$pid")" xargs -r0a /proc/"$pid"/environ sh -c '
  env -i -- "$@" scrot -' sh > screenshot.png

Where $pid could be the pid of some process running gnome-session or xterm or the window manager for instance in there.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501