Connect to VNC Server via SSH Tunnel

0
1161

How do you remotely connect to a VNC server? Well in this guide, we are going to learn how to connect to VNC server via SSH tunnel. VNC connections and unencrypted and thus poses security risks. To secure VNC connection, connections can be made through the use of SSH protocol.

Connect to VNC Server via SSH Tunnel

In our previous guide, on how to install and configure VNC server on CentOS 8, we saw how to test local VNC server connection using TigerVNC client, vncviewer.

  • You can connect to a VNC server through the VNC client (vncviewer) with the -via command line option which enables it to automatically create an encrypted TCP tunnel to VNC server before connection is established between the client and the server. It then connects to the server through that tunnel. It basically invokes SSH local port forwarding.
  • You can also connect to VNC server using SSH local port forwarding. Once connection is established, you can locally connect to VNC server via vncviewer.

Connect to VNC Server with vncviewer -via Option

Install TigerVNC Client on CentOS/RHEL Derivatives

TigerVNC client (vncviewer) is provided by the tigervnc package on CentOS and similar derivatives. In case you have not already installed it, simply run the command below to install;

dnf install tigervnc

Install TigerVNC Client on Ubuntu/Debian Derivatives

apt install tigervnc-viewer

You can even use xvnc4viewer;

apt install xvnc4viewer

Once the installation is done, you can now connect to a VNC server by executing the command below;

vncviewer -via [email protected] [host:displayNum]

Where:

  • USER: is any user that can ssh into the VNC server
  • HOST: is the VNC server address. It can be IP address or hostname (if you have DNS)
  • [host:displayNum]: specifies the display number to connect to on the VNC server. If you restricted VNC access to allow only encrypted connections from the loopback interface, you would simply use the localhost:1 to connect to the display number 1. Otherwise, you can specify the IP address or hostname if VNC allows connection on all interfaces.

For example, to login to second display number of the VNC server accepting connections on loopback interface as user amos;

vncviewer -via [email protected] localhost:2

When run, the command first prompts you to enter the SSH password. After that, you are prompted to enter the password to access the specified display number.

VNC Viewer Free Edition 4.1.1 for X - built Feb 25 2015 22:57:51
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
[email protected]'s password: SSH PASSWORD

Sun Oct  6 13:52:08 2019
 CConn:       connected to host localhost port 5599
 CConnection: Server supports RFB protocol version 3.8
 CConnection: Using RFB protocol version 3.8
Password: DISPLAY-NUMBER PASSWORD
...

Connect to VNC Server via SSH Local Port Forwarding

With ssh local port forwarding to the VNC server, from your desktop Linux system with SSH client installed, execute the command;

ssh -L 5001:localhost:5901 [email protected]

Where

  • Option -L specifies SSH local port forwarding
  • 5001 is the local port on the local Linux system from where you will connect to remote VNC server.
  • locahost:5901 is the remote VNC server and the display number to connect to. (In this case, it is the same VNC server as VNC-SERVER-ADDRESS since we configured it to allow connections on loopback interface only)
  • [email protected] is the SSH user for login to VNC Server.

For example, to connect to display number 1 on the remote VNC server (192.168.56.123), that allows VNC connections only on loopback interface, from the local system through SSH tunnel, simply run;

ssh -L 5001:localhost:5901 [email protected]

When prompted, enter the password for logging as the specified user.

Once the tunnel, you can then connect to VNC server on the specified port from another terminal by running;

vncviewer localhost:5001

When run, the command prompts for the VNC session password.

VNC Viewer Free Edition 4.1.1 for X - built Feb 25 2015 22:57:51
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.

Sun Oct  6 15:10:15 2019
 CConn:       connected to host localhost port 5001
 CConnection: Server supports RFB protocol version 3.8
 CConnection: Using RFB protocol version 3.8
Password: ENTER VNC SESSION PASSWORD
...

You should now be able to securely connect to your remote VNC server.

Connect to VNC Server via SSH Tunnel

If you encounter authentication errors, be sure to disable screen lock for the user.

That is all on how to connect to VNC Server via SSH Tunnel. You can now securely connect to your VNC server remote desktop via SSH.

Related Tutorials

Install and Configure VNC Server on CentOS 8

How to Setup Guacamole Web-based Remote Desktop Access Tool on Ubuntu 18.04

How to Install and Use NoMachine Remote Desktop Tool on Ubuntu 18.04

Install Webmin on CentOS 8

Install and Setup Chrome Remote Desktop on Ubuntu 18.04

LEAVE A REPLY

Please enter your comment!
Please enter your name here