Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Set Up a VNC Server in Ubuntu: Step-by-Step

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Update your system: Open a terminal and run sudo apt update && sudo apt upgrade -y to ensure all packages are current.
  2. Install TigerVNC server: Run sudo apt install tigervnc-standalone-server tigervnc-common -y. This installs the VNC server and its supporting tools.
  3. 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.
  4. Start the VNC server for the first time: Run vncserver -localhost yes -geometry 1920x1080. The -localhost yes flag binds to localhost (safer) and -geometry sets the screen resolution. You'll see output like New 'ubuntu:1 (username)' desktop is :1. The :1 is the display number.
  5. 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:
    #!/bin/sh
    unset SESSION_MANAGER
    unset DBUS_SESSION_BUS_ADDRESS
    exec /usr/bin/gnome-session --session=ubuntu
    Save and exit (Ctrl+X, then Y). Make it executable: chmod +x ~/.vnc/xstartup.
  6. Restart the VNC server: Run vncserver -localhost yes -geometry 1920x1080 again. Note the display number (e.g., :1).
  7. Connect securely via SSH tunnel (recommended): From your local machine, run ssh -L 5901:localhost:5901 username@ubuntu-server-ip. Replace 5901 with 5900 + display number (e.g., display :1 → port 5901). Keep this SSH session open.
  8. Connect with a VNC client: On your local device, open your VNC client and connect to localhost:5901 (or localhost:1 if your client uses display numbers). Enter the VNC password you set earlier. You should see the Ubuntu desktop.
  9. Stop the VNC server when done: On the Ubuntu machine, run vncserver -kill :1 (replace :1 with your display number).

Common Mistakes

  • Forgetting to bind to localhost: If you skip -localhost yes and 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/xstartup was 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 x11vnc or use xfce4 instead 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 to localhost:5901 instead of localhost:1 in 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.