Set Up a VNC Server in Ubuntu: Step-by-Step
In this article
Yes, you can set up a VNC (Virtual Network Computing) server on Ubuntu to remotely access the desktop from another device. This guide walks you through installing and configuring a VNC server using TigerVNC (a common, lightweight option) and connecting securely via SSH. You'll need a Ubuntu machine (20.04 or later), sudo access, and another device with a VNC client (like RealVNC Viewer or Remmina).
What You'll Need
- A Ubuntu computer (desktop or server) with sudo privileges.
- Another device (Windows, macOS, Linux, or mobile) with a VNC client installed.
- Optional but recommended: SSH access to the Ubuntu machine for secure tunneling.
Step-by-Step
- Update your system: Open a terminal and run
sudo apt update && sudo apt upgrade -yto ensure all packages are current. - Install TigerVNC server: Run
sudo apt install tigervnc-standalone-server tigervnc-common -y. This installs the VNC server and its supporting tools. - Set a VNC password: Run
vncpasswd. You'll be prompted to enter a password (at least 6 characters) and optionally a view-only password. This password is used by clients to connect. - Start the VNC server for the first time: Run
vncserver -localhost yes -geometry 1920x1080. The-localhost yesflag binds to localhost (safer) and-geometrysets the screen resolution. You'll see output likeNew 'ubuntu:1 (username)' desktop is :1. The:1is the display number. - Configure the VNC session (optional but recommended): Stop the server with
vncserver -kill :1. Then edit the startup file:nano ~/.vnc/xstartup. Replace the contents with a simple desktop environment launcher. For a standard Ubuntu desktop, use:
Save and exit (Ctrl+X, then Y). Make it executable:#!/bin/sh unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS exec /usr/bin/gnome-session --session=ubuntuchmod +x ~/.vnc/xstartup. - Restart the VNC server: Run
vncserver -localhost yes -geometry 1920x1080again. Note the display number (e.g.,:1). - Connect securely via SSH tunnel (recommended): From your local machine, run
ssh -L 5901:localhost:5901 username@ubuntu-server-ip. Replace5901with5900 + display number(e.g., display :1 → port 5901). Keep this SSH session open. - Connect with a VNC client: On your local device, open your VNC client and connect to
localhost:5901(orlocalhost:1if your client uses display numbers). Enter the VNC password you set earlier. You should see the Ubuntu desktop. - Stop the VNC server when done: On the Ubuntu machine, run
vncserver -kill :1(replace:1with your display number).
Common Mistakes
- Forgetting to bind to localhost: If you skip
-localhost yesand expose VNC directly to the internet, it's a security risk. Always use SSH tunneling. - Wrong port number: Port 5900 is for display :0, 5901 for :1, etc. Mismatching causes connection failures.
- Not making xstartup executable: If you get a blank screen or immediate disconnect, check that
chmod +x ~/.vnc/xstartupwas run. - Using a weak VNC password: VNC passwords are limited to 8 characters; choose a strong one.
Tips
- Use a firewall (like UFW) to allow SSH only, and never open VNC ports (5900-5910) to the internet.
- For a lighter desktop experience on a server, install
x11vncor usexfce4instead of GNOME; adjust the xstartup accordingly. - Set the VNC server to start automatically on boot using systemd, but only if you're comfortable with service management.
- If you're on a headless server, you may need to install a desktop environment first:
sudo apt install ubuntu-desktop(large download).
Troubleshooting
- Cannot connect: Verify the VNC server is running with
vncserver -list. Check that the SSH tunnel is active. Try connecting tolocalhost:5901instead oflocalhost:1in your client. - Blank screen: The xstartup file may be misconfigured. Try a simpler one:
#!/bin/sh xterm &(install xterm if needed) to test if the server works. Then switch to a full desktop. - Authentication failure: Reset the password with
vncpasswd. Ensure you're using the correct display number. - Server won't start: Check for errors with
vncserver -verbose. Ensure no other VNC server is using the same display. Kill any stale processes:pkill -f Xvnc(this will stop all VNC sessions). - Slow performance: Reduce screen resolution or color depth:
vncserver -localhost yes -geometry 1280x720 -depth 24.
Frequently Asked Questions
Can I use VNC without SSH?
Yes, but it's not recommended. You can start the server with vncserver -geometry 1920x1080 (without -localhost yes) and connect directly to port 5901, but this exposes your desktop to the network. Only do this on a trusted LAN.
How do I change the screen resolution?
Kill the server (vncserver -kill :1) and restart with a different -geometry value, e.g., -geometry 1366x768.
Can I run multiple VNC sessions?
Yes, each session gets a new display number (:1, :2, etc.). Start another with vncserver -localhost yes -geometry 1920x1080 and it will use the next available display.
How do I make VNC start automatically?
Create a systemd service file. For example, create /etc/systemd/system/vncserver@.service with appropriate ExecStart and ExecStop commands, then enable it with systemctl enable vncserver@1. This is advanced; ensure you understand systemd syntax.
Why does my VNC session not show the full Ubuntu desktop?
Your xstartup may be missing the desktop environment. Try the GNOME session line provided in Step 5. Also, ensure you have a desktop environment installed (e.g., sudo apt install ubuntu-desktop).
Is TigerVNC the only option?
No, alternatives include x11vnc, Vino (GNOME's built-in), and RealVNC. TigerVNC is lightweight and widely used, but you can install others via sudo apt install x11vnc or sudo apt install vino.