How to achieve the effect of MS remote desktop on Linux

freerdpremoteremote desktopxorg

On Windows machines, if one machine A runs the remote desktop service, and another machine B (which in fact doesn't have to be Windows) runs a remote desktop client – then the user on B can 'steal' control of the session on A, see the app windows which were opened there and interact with them. Note that RDP also allows you to open a new session if there's no existing one, but that's not what the question is about.

My question: How can I achieve the same with A being a Linux machine, running an X server with an X session in progress.

Notes:

  • A solution need not be based on MSRDP itself, it could be NX, or VNC, or whatever else you like.
  • I'm willing to install software on A and on B, including installation as the root user.
  • Commercial solutions are relevant, though I would rather not resort to them.
  • Solution may involve opening additional ports on A (in addition to, say, port 22 for SSH).
  • Assume the distributions are Debian-based and fairly recent.
  • This question is similar to this one, but it's not the same, since : 1. I'm willing to run services other than just X and 2. In that question, the OP seems more interested in getting specific windows, while for me it's important to see the whole thing.
  • Solutions in which both the users on A and B can interact with the same session are also relevant.

Best Answer

To take control of a running X session you will need to configure your VNC server to connect to the same display as X. Generally, X will be connected to the first display - :0.

You will also need the X Authority for that X session. This can be found by accessing the X Authority file that the remote Display Manager is using. This depends on your Display Manager and can be found using ps aux and grep. For example: psaux | grep Xorg gives you something similar to:

/usr/libexec/Xorg -background none :0 -seat seat0 -auth /var/run/lightdm/root/:0 -listen tcp vt1 -noswitch

Note the -auth option as you'll need it with VNC. You then need to run something like the following as root on the remote computer after installing x11vnc:

/usr/bin/x11vnc -norc -forever -shared -bg -autoport 5900 -auth /var/run/lightdm/root/:0

The easiest way would be to create a service that runs this for you on boot. On distros that use systemd, create a file /etc/systemd/system/x11vnc.service similar to the following:

[Unit]
Description=VNC Server for X11
Requires=display-manager.service
After=display-manager.service

[Service]
Type=forking
ExecStart=/usr/bin/x11vnc -norc -forever -shared -bg \
        -autoport 5900 \
        -rfbauth /etc/x11vnc.pass \
        -o /var/log/x11vnc.log \
        -auth /var/run/lightdm/root/:0

[Install]
WantedBy=graphical.target

Note the -rfbauth option. You'll need to create the password file beforehand using:

$ sudo x11vnc -storepasswd /etc/x11vnc.pass

Alternatively, if you don't feel you need password protected VNC sessions, then simply remove the -rfbauth line.

Enable at boot with:

$ sudo systemctl enable x11vnc.service

Start it manually with:

$ sudo systemctl start x11vnc.service

You can now connect to the remote computer with your VNC client and take over the session.

Related Question