Sign In
Sign In

How to Create an SSH Tunnel for Secure Connections over VNC

How to Create an SSH Tunnel for Secure Connections over VNC
Hostman Team
Technical writer
SSH Network
16.06.2025
Reading time: 6 min

One of the major drawbacks of the VNC (Virtual Network Computing) protocol for remote access to computers is the complete lack of session encryption. 

Bf070f03 1241 422f 9f54 4348c6ce45c0

Image source: FAQ on the TightVNC website

One way to address this issue is by creating an SSH tunnel over which the VNC session will run, ensuring full encryption of the VNC session. An SSH tunnel creates an encrypted data channel between the client device and the server. In addition to establishing a secure connection to the remote device, an SSH tunnel can also be used to transfer data.

In this article, we will explore several methods for creating an SSH tunnel, including using the standard ssh utility, as well as third-party client applications such as PuTTY and MobaXterm.

Prerequisites

  • A server or virtual machine with VNC installed. You can use TightVNC for this. We explain how to install it in another article.

  • A second server or virtual machine with a pre-installed Linux OS with a graphical interface. You can use any modern Linux distribution or a home computer or laptop running Windows. Both Home and Professional editions, as well as Windows Server versions, are suitable.

Creating an SSH Tunnel 

Method 1. The ssh Utility

Let’s start by setting up an SSH tunnel using the standard OpenSSH client, which comes pre-installed by default on almost all modern Linux distributions, as well as on Windows operating systems starting from Windows 10 version 1709 and above. Windows Server 2019 and Windows Server 2022 would also work. 

On Windows systems, you can also use any WSL distribution (Windows Subsystem for Linux).

The following command for setting up an SSH tunnel works the same on both Linux and Windows:

ssh -L 5901:localhost:5901 root@<server-IP-address>

Where:

  • -L — the flag for local port forwarding. In local forwarding, a port from the client device is forwarded to the server. All subsequent connections to this local port will pass through the SSH tunnel.
  • 5901:localhost:5901 — syntax for forwarding the remote port. In this example, we inform SSH that we want to forward port 5901 (the port of the VNC server) located on the remote server to gain access to the VNC server. At the same time, we also open port 5901 on our local device (localhost).
  • root@<server-IP-address> — the standard syntax for SSH connection.

After entering the command, the system will prompt for the user’s password, and upon successful entry, you will log into the server. After this, the SSH tunnel will be established. It's important to remain connected to the server; otherwise, the SSH session (and the tunnel) will be interrupted.

If you need to launch the SSH tunnel in "daemon" mode (in the background), use the -fNT options, for example:

ssh -fNT -L 5901:localhost:5901 root@<server-IP-address>

Where:

  • -f — after the password is entered, instead of launching a shell, the ssh process will switch to the background;
  • -N — do not execute any command on the remote server after starting the tunnel;
  • -T — disables the use of a terminal.

Once the SSH tunnel is successfully established, you can connect using any VNC client utility, for example, TightVNC Connection. Launch the utility and enter the address localhost::5901 in the “Remote Host” field:

72e6f220 988b 41f0 A3ba 40c3cebec2bc

Note: The use of two colons after localhost applies only to the TightVNC Connection program.

After entering the address, click the “Connect” button. The program will request the password for the VNC session, which is set during the VNC server configuration:

68c211bd Fa33 4c8f A0ef Cedc6af7322c

After entering the password, a window with the graphical interface of the server will open:

E8aac3d3 7ea3 46cb 888f Bc428eeb715f

All traffic between your device and the VNC server is now fully protected and encrypted.

Method 2. PuTTY

In addition to using the standard ssh utility, a tunnel can also be set up using the popular client utility for connecting to remote servers — PuTTY. To do this, follow these steps:

  1. Launch PuTTY and in the main menu fill in the following fields:

    • Host Name (or IP address): enter the IP address of the VNC server;

    • Port: specify the port used by SSH;

    • Saved Sessions: enter any name for the session so that it can be saved and launched quickly in the future.

  2. Click the “Save” button to save the current session.

6c30817a C871 4947 Af47 D027219eac9a

  1. In the left menu, find the “Connection” section, expand it, and go to “Tunnels”:

5d88be05 C250 4e01 8797 931c510d77d3

  1. In the opened section, fill in the following details:

    • Source port: specify the port to be opened on the client device, e.g., 5901;

    • Destination: enter the IP address of the VNC server and the VNC server’s port.

  2. After entering the data, click the “Add” button:

2caff664 465f 4f2e Ae33 69ab3007a2b4

  1. Return to the PuTTY main menu (the “Session” section) and connect to the server by clicking the “Open” button. During the first login, you will need to accept the host key by clicking the “Accept” button.

3d59508b C610 4780 A95c E6429af39efd

  1. After entering the user account password, the server terminal will open:

3a8778be Fbab 4339 8ee0 D1507f642a96

  1. Without closing the PuTTY session window, open your VNC client application (e.g., TightVNC Connection) and enter the address localhost:5901:

Без Названия

After entering the VNC session password, the server’s graphical interface will be displayed.

Method 3. MobaXterm

Another popular program for Windows OS used to connect to remote servers is MobaXterm. It can also be used to create an SSH tunnel. To do so, follow these steps:

  1. Launch the program and click on the “Tunneling” tab at the top:

11711700 3076 419e Bae8 9fd245feac08

  1. In the tunnel settings window, make sure the option “Local port forwarding” is selected and fill in the following information:

    • In the “My computer with MobaXterm” section, enter the local port (5901) to be opened on the device;

    • In the “SSH server” section, enter the address of the remote VNC server, along with the login and password to connect to the server;

    • In the “Remote server” section, enter localhost as the address and 5901 as the port.

E0d2e70b F582 4df4 A376 Ae8334f46aa6

  1. Click the “Save” button to save the settings.

  2. In the opened window, click the start button in the “Start/stop” section:

29e1ddc7 9281 496f B959 B965e6db227f

Once the SSH tunnel is launched, go to the “Session” section:

954999a6 655c 46c9 Acf0 54a86b9bbabb

  1. In the “Remote hostname or IP address” field, enter localhost, and in the “Port” field, enter 5901:

1e058a96 29a3 4f01 B2d5 9a73767cd65c

  1. Click the “OK” button to connect.

After entering the VNC session password, the server’s graphical interface will appear:

Conclusion

Although the VNC protocol does not encrypt its traffic by default, this issue can be resolved by using an SSH tunnel. In this article, we reviewed several methods for setting up an SSH tunnel on your device.

SSH Network
16.06.2025
Reading time: 6 min

Similar

Linux

How to Copy Files over SSH

The SSH (Secure Shell) protocol is a network protocol for remote command-line management of operating systems, widely considered the standard for remote access to *nix machines. It allows secure login to a server, remote command execution, file management (creating, deleting, copying, etc.), and more. Most cloud and hosting providers require SSH to access their services. In this article, we’ll look at how to copy files over SSH on both Windows and Linux systems. How SSH Works SSH can securely transmit any data (audio, video, application protocol data) through an encrypted communication channel. Unlike outdated and insecure protocols like Telnet and rlogin, SSH ensures data confidentiality and authenticity — essential for internet communications. Here’s how a secure connection between a client and server is established: TCP Connection Setup: By default, the server listens on port 22. Both sides share a list of supported algorithms (compression, encryption, key exchange) and agree on which to use. Authentication: To prevent impersonation, both parties verify each other's identities using asymmetric encryption (public/private key pairs). First, the server is authenticated. On the first connection, the client sees a warning with server details. Trusted server keys are stored in /home/<username>/.ssh/known_hosts. Key Generation: Once the server is verified, both sides generate a symmetric key to encrypt all data exchanged. User Authentication: This is done using either a password or a client-sent public key stored in /home/<username>/.ssh/authorized_keys on the server. The most popular implementation on Linux is OpenSSH, which comes pre-installed on most distributions (Ubuntu, Debian, RHEL-based, etc.). Clients like PuTTY or MobaXterm are used on Windows. Since Windows 10 and Server 2019, OpenSSH tools are also available natively. You can learn more about working with SSH in our tutorial. File Copying via SSH Two main utilities for copying files over SSH in Linux are scp and sftp. Both come with OpenSSH. SSH supports two protocol versions: 1 and 2. OpenSSH supports both, but version 1 is rarely used. Autocompletion Setup To enable Tab-based autocompletion when using scp, set up public key authentication: Generate a key pair: ssh-keygen You’ll see output like: Generating public/private rsa key pair. Enter file in which to save the key (/home/user/.ssh/id_rsa): Enter passphrase (empty for no passphrase): By default, your keys (id_rsa for private and id_rsa.pub for public) are saved to ~/.ssh/. Now copy the public key to the remote machine: ssh-copy-id [username]@[ip-address] After entering the user's password, you’ll see a message confirming the key was added. Secure Copy (SCP) For small data transfers (e.g., service configs), scp is best. Copy from local to remote: scp test.txt user@192.168.1.29:/home/user/ Copy multiple files: scp test1.txt test2.txt user@192.168.1.29:/home/user/ Copy from remote to local: scp user@192.168.1.29:/home/user/test.txt ~/ Copy directories: scp -r testdir user@192.168.1.29:/home/user/ Remote-to-remote copy: scp gendo@192.168.1.25:/home/gendo/test.txt user@192.168.1.29:/home/user/ Secure FTP (SFTP) SFTP is another utility included in OpenSSH. As of OpenSSH 9.0, scp now uses SFTP by default instead of the old SCP/RCP protocol. Unlike classic FTP, sftp transmits encrypted data over a secure tunnel. It does not require a separate FTP server. Example usage: sftp misato@192.168.1.29 sftp> ls sftp> lcd testdir/ sftp> get test.txt sftp> bye Graphical file managers like Midnight Commander and Nautilus use sftp. In Nautilus, the remote server appears like a local folder, e.g., user@ip. Copying Files Over SSH on Windows Use the pscp command-line tool from PuTTY to copy files on Windows. Copy to server: pscp C:\server\test.txt misato@192.168.1.29:/home/misato/ Copy from server: pscp misato@192.168.1.29:/home/misato/test.txt C:\file.txt List files on remote server: pscp -ls user@192.168.1.29:/home/misato Use quotes for paths with spaces: pscp "C:\dir\bad file name.txt" misato@192.168.1.29:/home/misato/ To get help, run: pscp Conclusion We’ve covered how to copy files to and from a server using the secure SSH protocol. If you work with cloud servers, understanding SSH is essential — it’s the standard method for remote access to *nix machines and a vital part of everyday DevOps and system administration.
14 April 2025 · 4 min to read
Linux

How to Use SSH Keys for Authentication

Many cloud applications are built on the popular SSH protocol—it is widely used for managing network infrastructure, transferring files, and executing remote commands. SSH stands for Secure Socket Shell, meaning it provides a shell (command-line interface) around the connection between multiple remote hosts, ensuring that the connection is secure (encrypted and authenticated). SSH connections are available on all popular operating systems, including Linux, Ubuntu, Windows, and Debian. The protocol establishes an encrypted communication channel within an unprotected network by using a pair of public and private keys. Keys: The Foundation of SSH SSH operates on a client-server model. This means the user has an SSH client (a terminal in Linux or a graphical application in Windows), while the server side runs a daemon, which accepts incoming connections from clients. In practice, an SSH channel enables remote terminal management of a server. In other words, after a successful connection, everything entered in the local console is executed directly on the remote server. The SSH protocol uses a pair of keys for encrypting and decrypting information: public key and private key. These keys are mathematically linked. The public key is shared openly, resides on the server, and is used to encrypt data. The private key is confidential, resides on the client, and is used to decrypt data. Of course, keys are not generated manually but with special tools—keygens. These utilities generate new keys using encryption algorithms fundamental to SSH technology. More About How SSH Works Exchange of Public Keys SSH relies on symmetric encryption, meaning two hosts wishing to communicate securely generate a unique session key derived from the public and private data of each host. For example, host A generates a public and private key pair. The public key is sent to host B. Host B does the same, sending its public key to host A. Using the Diffie-Hellman algorithm, host A can create a key by combining its private key with the public key of host B. Likewise, host B can create an identical key by combining its private key with the public key of host A. This results in both hosts independently generating the same symmetric encryption key, which is then used for secure communication. Hence, the term symmetric encryption. Message Verification To verify messages, hosts use a hash function that outputs a fixed-length string based on the following data: The symmetric encryption key The packet number The encrypted message text The result of hashing these elements is called an HMAC (Hash-based Message Authentication Code). The client generates an HMAC and sends it to the server. The server then creates its own HMAC using the same data and compares it to the client's HMAC. If they match, the verification is successful, ensuring that the message is authentic and hasn't been tampered with. Host Authentication Establishing a secure connection is only part of the process. The next step is authenticating the user connecting to the remote host, as the user may not have permission to execute commands. There are several authentication methods: Password Authentication: The user sends an encrypted password to the server. If the password is correct, the server allows the user to execute commands. Certificate-Based Authentication: The user initially provides the server with a password and the public part of a certificate. Once authenticated, the session continues without requiring repeated password entries for subsequent interactions. These methods ensure that only authorized users can access the remote system while maintaining secure communication. Encryption Algorithms A key factor in the robustness of SSH is that decrypting the symmetric key is only possible with the private key, not the public key, even though the symmetric key is derived from both. Achieving this property requires specific encryption algorithms. There are three primary classes of such algorithms: RSA, DSA, and algorithms based on elliptic curves, each with distinct characteristics: RSA: Developed in 1978, RSA is based on integer factorization. Since factoring large semiprime numbers (products of two large primes) is computationally difficult, the security of RSA depends on the size of the chosen factors. The key length ranges from 1024 to 16384 bits. DSA: DSA (Digital Signature Algorithm) is based on discrete logarithms and modular exponentiation. While similar to RSA, it uses a different mathematical approach to link public and private keys. DSA key length is limited to 1024 bits. ECDSA and EdDSA: These algorithms are based on elliptic curves, unlike DSA, which uses modular exponentiation. They assume that no efficient solution exists for the discrete logarithm problem on elliptic curves. Although the keys are shorter, they provide the same level of security. Key Generation Each operating system has its own utilities for quickly generating SSH keys. In Unix-like systems, the command to generate a key pair is: ssh-keygen -t rsa Here, the type of encryption algorithm is specified using the -t flag. Other supported types include: dsa ecdsa ed25519 You can also specify the key length with the -b flag. However, be cautious, as the security of the connection depends on the key length: ssh-keygen -b 2048 -t rsa After entering the command, the terminal will prompt you to specify a file path and name for storing the generated keys. You can accept the default path by pressing Enter, which will create standard file names: id_rsa (private key) and id_rsa.pub (public key). Thus, the public key will be stored in a file with a .pub extension, while the private key will be stored in a file without an extension. Next, the command will prompt you to enter a passphrase. While not mandatory (it is unrelated to the SSH protocol itself), using a passphrase is recommended to prevent unauthorized use of the key by a third-party user on the local Linux system. Note that if a passphrase is used, you must enter it each time you establish the connection. To change the passphrase later, you can use: ssh-keygen -p Or, you can specify all parameters at once with a single command: ssh-keygen -p old_password -N new_password -f path_to_files For Windows, there are two main approaches: Using ssh-keygen from OpenSSH: The OpenSSH client provides the same ssh-keygen command as Linux, following the same steps. Using PuTTY: PuTTY is a graphical application that allows users to generate public and private keys with the press of a button. Installing the Client and Server Components The primary tool for an SSH connection on Linux platforms (both client and server) is OpenSSH. While it is typically pre-installed on most operating systems, there may be situations (such as with Ubuntu) where manual installation is necessary. The general command for installing SSH, followed by entering the superuser password, is: sudo apt-get install ssh However, in some operating systems, SSH may be divided into separate components for the client and server. For the Client To check whether the SSH client is installed on your local machine, simply run the following command in the terminal: ssh If SSH is supported, the terminal will display a description of the command. If nothing appears, you’ll need to install the client manually: sudo apt-get install openssh-client You will be prompted to enter the superuser password during installation. Once completed, SSH connectivity will be available. For the Server Similarly, the server-side part of the OpenSSH toolkit is required on the remote host. To check if the SSH server is available on your remote host, try connecting locally via SSH: ssh localhost If the SSH daemon is running, you will see a message indicating a successful connection. If not, you’ll need to install the SSH server: sudo apt-get install openssh-server As with the client, the terminal will prompt you to enter the superuser password. After installation, you can check whether SSH is active by running: sudo service ssh status Once connected, you can modify SSH settings as needed by editing the configuration file: ./ssh/sshd_config For example, you might want to change the default port to a custom one. Don’t forget that after making changes to the configuration, you must manually restart the SSH service to apply the updates: sudo service ssh restart Copying an SSH Key to the Server On Hostman, you can easily add SSH keys to your servers using the control panel. Using a Special Copy Command After generating a public SSH key, it can be used as an authorized key on a server. This allows quick connections without the need to repeatedly enter a password. The most common way to copy the key is by using the ssh-copy-id command: ssh-copy-id -i ~/.ssh/id_rsa.pub name@server_address This command assumes you used the default paths and filenames during key generation. If not, simply replace ~/.ssh/id_rsa.pub with your custom path and filename. Replace name with the username on the remote server. Replace server_address with the host address. If the usernames on both the client and server are the same, you can shorten the command: ssh-copy-id -i ~/.ssh/id_rsa.pub server_address If you set a passphrase during the SSH key creation, the terminal will prompt you to enter it. Otherwise, the key will be copied immediately. In some cases, the server may be configured to use a non-standard port (the default is 22). If that’s the case, specify the port using the -p flag: ssh-copy-id -i ~/.ssh/id_rsa.pub -p 8129 name@server_address Semi-Manual Copying There are operating systems where the ssh-copy-id command may not be supported, even though SSH connections to the server are possible. In such cases, the copying process can be done manually using a series of commands: ssh name@server_address 'mkdir -pm 700 ~/.ssh; echo ' $(cat ~/.ssh/id_rsa.pub) ' >> ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys' This sequence of commands does the following: Creates a special .ssh directory on the server (if it doesn’t already exist) with the correct permissions (700) for reading and writing. Creates or appends to the authorized_keys file, which stores the public keys of all authorized users. The public key from the local file (id_rsa.pub) will be added to it. Sets appropriate permissions (600) on the authorized_keys file to ensure it can only be read and written by the owner. If the authorized_keys file already exists, it will simply be appended with the new key. Once this is done, future connections to the server can be made using the same SSH command, but now the authentication will use the public key added to authorized_keys: ssh name@server_address Manual Copying Some hosting platforms offer server management through alternative interfaces, such as a web-based control panel. In these cases, there is usually an option to manually add a public key to the server. The web interface might even simulate a terminal for interacting with the server. Regardless of the method, the remote host must contain a file named ~/.ssh/authorized_keys, which lists all authorized public keys. Simply copy the client’s public key (found in ~/.ssh/id_rsa.pub by default) into this file. If the key pair was generated using a graphical application (typically PuTTY on Windows), you should copy the public key directly from the application and add it to the existing content in authorized_keys. Connecting to a Server To connect to a remote server on a Linux operating system, enter the following command in the terminal: ssh name@server_address Alternatively, if the local username is identical to the remote username, you can shorten the command to: ssh server_address The system will then prompt you to enter the password. Type it and press Enter. Note that the terminal will not display the password as you type it. Just like with the ssh-copy-id command, you can explicitly specify the port when connecting to a remote server: ssh client@server_address -p 8129 Once connected, you will have control over the remote machine via the terminal; any command you enter will be executed on the server side. Conclusion Today, SSH is one of the most widely used protocols in development and system administration. Therefore, having a basic understanding of its operation is crucial. This article aimed to provide an overview of SSH connections, briefly explain the encryption algorithms (RSA, DSA, ECDSA, and EdDSA), and demonstrate how public and private key pairs can be used to establish secure connections with a personal server, ensuring that exchanged messages remain inaccessible to third parties. We covered the primary commands for UNIX-like operating systems that allow users to generate key pairs and grant clients SSH access by copying the public key to the server, enabling secure connections.
30 January 2025 · 10 min to read
SSH

How to Use ssh-agent to Manage Private Keys

When connecting to remote servers using SSH, users typically authenticate themselves using a pair of cryptographic keys: a public key and a private key. While storing the private key on your local machine, it’s essential to keep it secure with a passphrase. However, entering the passphrase repeatedly can become tedious. Enter ssh-agent, a tool designed to manage and cache your private keys in memory, making SSH connections more efficient and secure. Why Use ssh-agent for Private Key Management? SSH private keys provide a secure method of authentication. However, without proper management, frequent SSH operations can require multiple passphrase inputs, especially if you use several private keys. ssh-agent simplifies this process by keeping private keys cached in memory. Once a key is loaded into the agent, it can be used for multiple connections without requiring the passphrase each time. This increases productivity without sacrificing security. How Does ssh-agent Work? ssh-agent acts as a background process that stores your private keys and offers them to SSH clients when authentication is required. It caches the passphrase-unlocked private keys in memory so that you don’t have to re-enter the passphrase each time you connect to a cloud server. The agent manages a secure channel between SSH clients and the private keys, ensuring that the private key itself never leaves the local machine. Setting Up ssh-agent on Linux/MacOS Linux and macOS both come with ssh-agent pre-installed, making the setup process relatively simple. Here’s a basic outline to start using ssh-agent: Open a terminal. Verify ssh-agent is installed by running: ssh-agent If ssh-agent is not installed, it can be added through package managers such as apt for Linux distributions or Homebrew for macOS. Starting and Configuring ssh-agent To manually start ssh-agent, use the following command: eval $(ssh-agent) This starts the ssh-agent process and configures your shell environment to communicate with the agent. Adding Private Keys to ssh-agent Once ssh-agent is running, private keys can be added to it using the ssh-add command: ssh-add /path/to/private_key This will prompt you to enter the private key’s passphrase (if it’s protected), and ssh-agent will store the unlocked key in memory for future use. Checking Loaded Keys in ssh-agent To verify which keys are currently loaded into ssh-agent, you can use the following command: ssh-add -l This command will list the fingerprints of all the currently cached private keys. Removing Private Keys from ssh-agent If you want to remove a key from the agent, you can use the -d option with ssh-add: ssh-add -d ~/.ssh/id_rsa To remove all keys at once: ssh-add -D Automating ssh-agent Start with Shell Configuration To make sure ssh-agent starts automatically every time you open a new shell session, you can add the following to your .bashrc file: if ! pgrep -u "$USER" ssh-agent > /dev/null; then   eval $(ssh-agent -s)fi This ensures that ssh-agent is available without requiring manual start every time you open a new terminal. Common ssh-agent Commands and Usage Start ssh-agent: eval $(ssh-agent) Add a key to ssh-agent: ssh-add /path/to/private_key List loaded keys: ssh-add -l Remove a specific key: ssh-add -d /path/to/private_key Remove all keys: ssh-add -D Security Considerations While ssh-agent provides convenience by keeping your private keys in memory, it’s crucial to be aware of potential security risks. The agent keeps keys in volatile memory, which can be exploited if your machine is compromised. Some best practices include: Only keep private keys in memory for the duration they are needed. Use strong passphrases for your private keys. Lock the agent when not in use, especially on shared or insecure systems. Use hardware security modules (HSMs) for storing keys when possible. Conclusion Using ssh-agent to manage SSH private keys significantly enhances convenience and efficiency while maintaining security. By caching your private keys, ssh-agent allows for quick SSH access without repeated passphrase prompts. However, security best practices should always be followed to ensure that sensitive information remains protected during use. Setting up ssh-agent on Linux and macOS is straightforward, and with proper automation, it can become an indispensable tool for anyone managing secure connections across multiple systems.
18 October 2024 · 4 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support