Mirroring a Ubuntu desktop using SSH and VNC (and dealing with the login screen)


June 13, 2022

A common problem when remotely connecting to PCs on different networks is that various remote desktop software packages are blocked due to security risks.

A nice way to replicate the functionality of these packages when connecting to Linux-based systems is to connect to the remote computer using SSH (connecting to a VPN if necessary) and then connect using VNC. However, the standard way to use VNC is to create a virtual desktop to work on the PC instead of viewing the actual desktop of the currently logged-in user ("scraping" the display).

In order to mirror the actual desktop and deal with the login screen, the following instructions might be useful as a good basic starting point. As always, there are multiple ways to set up something like this, but I found the following to be quite simple to use overall . Note that I have also included all the necessary links to the useful sites that I found along the way.

Requirements:
Make sure that the following is done before starting:
  • Installed on Windows: Putty, VNC Viewer (e.g. TightVNC)
  • Installed on Ubuntu: lightdm, xdotool, TigerVNC Server (tutorial¬†here for setting up the VNC server - don't forget to modify ~/.vnc/xstartup as described here)
  • IP address of the remote Ubuntu PC
  • Required network ports are open (e.g. 22 and 5900)
  • SSH is configured on the Ubuntu PC (for Ubuntu 20.04, there is a tutorial here)
  • Root user is set up on the Ubuntu PC (tutorial here)
  • lightdm is being used instead of gdm3 for the login screen (tutorial here)
Instructions:
Connect to the VPN network using the desired client (e.g. Fortclient) on your Windows PC. Once sure that the Ubuntu PC is at the login screen (wait a minute or so), do the following:
  • Open Putty on the Windows PC and SSH into the Ubuntu PC using the hostname and port (e.g. hostname: [email protected], port: 22)
  • Login using your standard user password and then execute (type then press Enter):
su root
  • Enter the root password to get root access. Once in root, execute the following:
export XAUTHORITY=/var/lib/lightdm/.Xauthority
export DISPLAY=:0
xdotool type "YOURPASSWORD"
xdotool key Return
exit
  • After exiting root, execute the following to load the VNC server:
x0vncserver -passwordfile ~/.vnc/passwd -display :0
  • Note that display 0 should work but might need to be changed depending on the active session e.g. change to display 1 in the commands above
  • Leaving the Putty window open, open TightVNC on Windows and use the IP address of the Ubuntu PC and the port shown in Putty to connect (e.g. 192.168.1.1:5900). Use the password set up while installing TigerVNC on the Ubuntu machine
  • And that's it! This should display the Ubuntu desktop! Press Ctrl + C in the Putty window to disconnect (gracefully!) before closing everything on your Windows PC
Note: You will only have to execute the root commands if you have restarted your Ubuntu PC. This means that if you disconnect from your Ubuntu PC and want to connect again, you will only have to load the VNC server. Also, one caveat is that the clipboard (copying and pasting between PCs) does not work with this setup, but this issue can likely be solved with a little extra configuration.