Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents

Overview

A VNC (Virtual Network Computing) is a graphical desktop sharing system that can remotely control another computer.

On your local computer, please install a supported VNC Client:

Using Windows

If you don’t already have PuTTY installed, please follow the link SSH to Instance using Windows.
Also, download a supported VNC Client, if you haven’t already. In this example, we will use TightVNC.

  1. Connect to your server through PuTTY and open the terminal window.

  2. On your server, update your list of packages.

    Code Block
    sudo apt update
  3. Now, install the Xfce desktop environment and TightVNC server

    Code Block
    sudo apt install xfce4 xfce4-goodies
    sudo apt install tightvncserver
  4. To complete the VNC server configuration, create the initial configuration file and set up a secure password.

    Code Block
    vncserver

    Passwords must be between 6-8 characters. If your password has more than 8 characters, it will be automatically truncated.
    After setting your password, the following should appear with your_hostname = your_instancename:

    Note: if you want to change your password or add a view-only password, you can do so with the following command:

    Code Block
    vncpasswd
  5. Now we configure the VNC server, by first stopping the default VNC server instance.

    Code Block
    vncserver -kill :1
  6. Backup and modify the startup file.

    Code Block
    mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
    nano ~/.vnc/xstartup

    This will create a new xstartup file and open it in the text editor nano. Add the following lines:

    Code Block
    #!/bin/bash
    xrdb $HOME/.Xresources
    startxfce4 &

    To save and exit, press CTRL+X, Y, then ENTER

  7. We make the startup file executable and restart VNC server.

    Code Block
    sudo chmod +x ~/.vnc/xstartup
    vncserver
  8. Now we connect to the VNC Desktop.
    Right-click on the top bar of the terminal window, then select Change Settings.

  9. On the directory on the left-hand side of PuTTY, expand SSH and click on Tunnels.
    In Source Port enter: 59000
    In Destination enter: localhost:5901
    Click Add, then Apply.

  10. Open your VNC viewer client and connect to localhost:59000. You should see something like the following:

Info

The VNC service needs to be manually started after every instance reboot (Steps 5-10) or the service can be automatically started after instance reboot with the following optional setup: Setup VNC as System Service

Also note, the SSH tunnel (Step 9) must be configured after every time the SSH client is started in order to connect to the VNC server. Please visit the following section to save the SSH tunnel information to PuTTY: Setup SSH Tunnel Information in PuTTY

Using Mac or Linux

If you haven’t already, please download a supported VNC Client. In this example, we will use TightVNC.

  1. Connect to your server and open the terminal window.
    Instructions on how to connect to your server through SSH can be found here:
    SSH to Instance using Mac and Linux

  2. On your server, update your list of packages.

    Code Block
    sudo apt update
  3. Now, install the Xfce desktop environment and TightVNC server

    Code Block
    sudo apt install xfce4 xfce4-goodies
    sudo apt install tightvncserver
  4. To complete the VNC server configuration, create the initial configuration file and set up a secure password.

    Code Block
    vncserver

    Passwords must be between 6-8 characters. If your password has more than 8 characters, it will be automatically truncated.
    After setting your password, the following should appear with your_hostname = your_instancename:

    Note: if you want to change your password or add a view-only password, you can do so with the following command:

    Code Block
    vncpasswd
  5. Now we configure the VNC server, by first stopping the default VNC server instance.

    Code Block
    vncserver -kill :1
  6. Backup and modify the startup file.

    Code Block
    mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
    nano ~/.vnc/xstartup

    This will create a new xstartup file and open it in the text editor nano. Add the following lines:

    Code Block
    #!/bin/bash
    xrdb $HOME/.Xresources
    startxfce4 &

    To save and exit, press CTRL+X, Y, then ENTER

  7. We make the startup file executable and restart VNC server.

    Code Block
    sudo chmod +x ~/.vnc/xstartup
    vncserver
  8. Now we connect to the VNC Desktop, by creating an SSH connection.
    In the terminal, input the following:

    Code Block
    ssh -L 59000:127.0.0.1:5901 -C -N -l ubuntu your_server_ip

    Replace ubuntu and your_server_ip with the sudo non-root username and server IP address.
    Note: it may be needed to replace 127.0.0.1 with localhost, depending on the Ubuntu version.

  9. Open your VNC client and connect to localhost:59000.

Info

The VNC service needs to be manually started after every instance reboot (Steps 5-9) or the service can be automatically started after instance reboot with the following optional setup: Setup VNC as System Service

Also note, the SSH tunnel (Step 8) must be configured after every time the client is started in order to connect to the VNC server.

Setup VNC as System Service

The following steps will configure the VNC server to start every time the server boots up.

  1. Create new unit file

    Code Block
    sudo nano /etc/systemd/system/vncserver@.service

    Add the following lines and replace the ubuntu value in User, Group, Working Directory and PIDFILE username as appropriate:

    Code Block
    [Unit]
    Description=Start TightVNC server at startup
    After=syslog.target network.target
    
    [Service]
    Type=forking
    User=ubuntu
    Group=ubuntu
    WorkingDirectory=/home/ubuntu
    
    PIDFile=/home/ubuntu/.vnc/%H:%i.pid
    ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
    ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :%i
    ExecStop=/usr/bin/vncserver -kill :%i
    
    [Install]
    WantedBy=multi-user.target

    Save and close the file.

  2. Enable the new unit file.

    Code Block
    sudo systemctl daemon-reload
    sudo systemctl enable vncserver@1.service
  3. Turn off current VNC server instance.

    Code Block
    vncserver -kill :1
  4. Start systemd service.

    Code Block
    sudo systemctl start vncserver@1

    You can use the following command to verify that it started.

    Code Block
    sudo systemctl status vncserver@1

    The output should say active (running)

  5. To reconnect to the server, start your SSH tunnel (Step 8 in either of instructions above).

Info

With this setup, users only need to create the SSH tunnel after every reboot to connect to the VNC server, starting Steps 8 in Using Windows/ Using Max or Linux instructions, to access the VNC server.

For PuTTY users, this can also be combined with Setup SSH Tunnel Information in PuTTY for even quicker connection.

Setup SSH Tunnel Information in PuTTY

  1. Open a new PuTTY sesssion and enter in the Host Name information, but do not click Open yet.

  2. Under Saved Sessions, label what you want the server and SSH information to be known as. Then click Save.

  3. On the directory on the left-hand side of PuTTY, expand SSH and click on Tunnels.
    In Source Port enter: 59000
    In Destination enter: localhost:5901
    Click Add.

  4. Return back to the default main page, by clicking on Session in the directory.
    Click Save once again.

  5. The next time you reboot the instance and reopen PuTTY, load the saved session. If you click on SSHTunnels, you should find the added SSH port forwarding. Then click Open.