1. Introduction to TightVNC
The primary reason for setting up TightVNC Server is to gain seamless access to all the machines available in my house, right from my workspace. This setup allows me to efficiently manage, monitor, and troubleshoot any of these systems remotely without physically being near them, streamlining my workflow.
In this comprehensive guide, we’ll walk you through setting up TightVNC Server on your Ubuntu system, configuring it to start automatically as a service after a reboot, and enabling remote connections from another machine. This setup is particularly useful if you need remote access to an Ubuntu machine with a graphical interface.
TightVNC is a remote desktop protocol that allows you to control your Ubuntu machine remotely using a graphical user interface (GUI). It uses the VNC (Virtual Network Computing) protocol, enabling you to work on your machine as if you were physically present.
TightVNC is lightweight, easy to install, and supports multiple sessions, making it ideal for both personal and professional use. In this guide, we will install TightVNC, configure it, and ensure that it starts automatically on system boot.
2. Installing TightVNC Server on Ubuntu
Step 1: Update Your System
Before we begin, it's essential to ensure your system is up-to-date. Open your terminal and run the following commands:
sudo apt update
sudo apt upgrade -y
Step 2: Install TightVNC Server
To install the TightVNC Server package, run:
sudo apt install tightvncserver -y
Step 3: Install a Desktop Environment (If Needed)
TightVNC works by serving a graphical interface. If you don’t have a desktop environment installed, you’ll need to install one, such as XFCE. XFCE is lightweight and works well with VNC.
To install XFCE, run:
sudo apt install xfce4 xfce4-goodies -y
Alternatively, if you already have GNOME, KDE, or another desktop environment installed, you can use that instead. Usually you will already have a desktop environment installed on Ubuntu Desktop.
Step 4: Start TightVNC for the First Time
Start TightVNC to create a new configuration:
vncserver
You will be prompted to set a password for VNC access. Ensure that it’s strong but easy to remember. This password will be used to connect remotely.
After setting the password, you will see an output similar to:
New 'X' desktop is your-hostname:1
The :1 at the end indicates the display number. You will use this in your VNC client to connect later. You can stop the VNC server with the following command:
-kill :1
3. Configuring TightVNC for Remote Desktop
To customize your VNC server setup, you will need to edit the startup script that VNC uses to start a graphical desktop. The script is located at ~/.vnc/xstartup. You’ll need to modify this file to use XFCE or your preferred desktop environment.
Step 1: Backup the Original xstartup File
mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
The default content looks like this:
#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &
TightVNC, by default, will try to use XFCE.
Step 2: Create a New xstartup Script
nano ~/.vnc/xstartup
Add the following content to use XFCE:
#!/bin/sh
xrdb "$HOME/.Xresources"
xsetroot -solid grey
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
gnome-session &
# Fix to make GNOME work
export XKL_XMODMAP_DISABLE=1
/etc/X11/XsessionIf you're using GNOME or KDE, replace startxfce4 with gnome-session or startkde, respectively. In my case, I want to use the Ubuntu Desktop environment.
Step 3: Make the Script Executable
chmod +x ~/.vnc/xstartup
Step 4: Restart the VNC Server
vncserver :1
4. Setting Up TightVNC as a System Service
To ensure that TightVNC starts automatically after every reboot, we will configure it as a system service using systemd.
Step 1: Create a VNC Service File
Create a new service file for TightVNC at /etc/systemd/system/vncserver@.service:
sudo vim /etc/systemd/system/vncserver@.service
Add the following content to the file:
[Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target
[Service]
Type=forking
User=your-username
PAMName=login
ExecStartPre=/usr/bin/vncserver -kill :%i > /dev/null 2>&1 || true
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=multi-user.target
- Replace
your-usernamewith your actual username. - You can adjust the
-geometry(resolution) and-depth(color depth) to your preference.
Step 2: Enable and Start the VNC Service
Check the service status:
sudo systemctl status vncserver@1.service
Start the VNC service immediately:
sudo systemctl start vncserver@1.service
Enable the service to start on boot:
sudo systemctl enable vncserver@1.service
(The 1 here refers to the display number, :1.)
If the service is running correctly, it will say "active (running)."
5. Configuring TightVNC to Start on Boot
To ensure TightVNC starts automatically after every reboot:
- Now, whenever the system is restarted, the VNC server will automatically start.
Enable the VNC service you created:
sudo systemctl enable vncserver@1.service
6. Connecting to Ubuntu Using TightVNC
Step 1: Install a VNC Client on Your Local Machine
For Windows, you can use:
- TightVNC Viewer: Download TightVNC
- RealVNC Viewer: Download RealVNC
For macOS:
- RealVNC Viewer: Same as above.
For Linux:
- Use any VNC viewer available in the package manager (
vinagre,remmina, etc.).
Step 2: Connect to Your Ubuntu Server
- Open your VNC viewer.
- In the "VNC Server" field, enter the IP address of your Ubuntu machine followed by the display number (e.g.,
your-server-ip:1). - Enter your VNC password when prompted.
You should now be connected to the graphical desktop of your Ubuntu machine.
7. Security Considerations for VNC Connections
VNC itself is not encrypted, so it's important to secure your connection. One way to do this is by tunneling the VNC connection through SSH.
Step 1: Set Up an SSH Tunnel
From your local machine (Windows/macOS/Linux), run the following SSH command to create an encrypted tunnel:
ssh -L 5901:localhost:5901 your-username@your-server-ip
Then, in your VNC viewer, connect to localhost:1 instead of your-server-ip:1. This encrypts the VNC traffic using SSH.
8. Troubleshooting Common Issues
- Black Screen After Connecting: This usually indicates a problem with the desktop environment configuration. Ensure your
xstartupfile is correct and executable.
VNC Server Fails to Start: Check the logs for errors:
cat ~/.vnc/your-server-ip:1.log
Cannot Connect to VNC: Ensure that port 5901 (or your VNC port) is open in your firewall:
sudo ufw allow 5901/tcp
Conclusion
Setting up TightVNC Server on Ubuntu is a simple and powerful way to access your machine remotely with a graphical interface. By configuring it as a service, you ensure that the server starts automatically after reboots, giving you seamless remote access. Additionally, securing the connection with SSH tunneling ensures your data remains private.