vnc through SSH: how to control your desktop remotely

I won’t go into too much detail in this post, as there are multiple guides that go above and beyond in explaining the behind-the-scenes of tunnelling VNC connections through SSH. Note that this solution uses TigerVNC’s x0vncserver to control the remote X server. Simply use vncserver if you want to control a different X server.

The setup is quite simple, but requires two terminal windows. In the first terminal, SSH into your remote machine with

user@local-machine $ ssh -Y <remote.machine.com> -L 5900:localhost:5900

and then,

remoteuser@remote-machine $  x0vncserver -display :0 -passwordfile ~/.vnc/passwd

Now, in the second terminal on your local machine, you can simply type

user@local-machine $ vncviewer localhost:5900

The -L flag in the SSH connection sets up port forwarding from the local machine to the remote machine. In short, all unbound traffic that goes through the specified port on the local machine is forward to the other port on the remote machine. Here we choose port 5900 on both machines as it is the default port for x0vncserver, but both can be changed freely.

Make sure to generate a password file for VNC by using vncpasswd and pass it to x0vncserver on the passwordfile flag. Here we choose the default.

The same method with generic ports would read

user@local-machine $ ssh -Y <remote.machine.com> -L <xxxx>:localhost:<yyyy>
remoteuser@remote-machine $  x0vncserver -rfbport <yyyy> -display :0 \
                                         -passwordfile ~/.vnc/passwd
and
user@local-machine $ vncviewer localhost:<xxxx>

Until next time, cheers!

comments powered by Disqus