Skip to content

Instantly share code, notes, and snippets.

@brccabral
Last active March 26, 2024 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brccabral/c660c1f5ae5ee4930f6cd70b4315e988 to your computer and use it in GitHub Desktop.
Save brccabral/c660c1f5ae5ee4930f6cd70b4315e988 to your computer and use it in GitHub Desktop.
VNC server on Ubuntu Desktop

VNC server on Ubuntu Desktop

Followed these initial instructions
https://www.youtube.com/watch?v=3K1hUwxxYek

The video asks to install lightdm, but there is no need, we can use gdm3.
Install x11vnc.

sudo apt update
sudo apt install x11vnc

Create password for the VNC session (not your login password)

sudo mkdir /etc/x11vnc
sudo touch /etc/x11vnc/passwd
x11vnc -storepasswd /etc/x11vnc/passwd

Create service if you need to start automatically. If not, check the .desktop file below.

sudo nano /lib/systemd/system/x11vnc.service

Copy these lines into service

[Unit]
Description=x11vnc service
After=display-manager.service network.target syslog.target

[Service]
Type=simple
ExecStart=/usr/bin/x11vnc -forever -display :0 -auth guess -rfbauth /etc/x11vnc/passwd
ExecStop=/usr/bin/killall x11vnc
Restart=on-failure

[Install]
WantedBy=multi-user.target

Reload daemon and start service

systemctl daemon-reload
systemctl enable x11vnc.service
systemctl start x11vnc.service
systemctl status x11vnc.service

To view more log lines:

journalctl -n 300 --unit=x11vnc

Forward port 5900 to 59000 locally only, use your <username> here.

ssh -L 192.168.X.Y:59000:192.168.X.Y:5900 -C -N -l <username> 192.168.X.Y -f

Check if computer is listening ports 5900 and 59000

sudo lsof -i -P -n | grep :5900

Open firewall

sudo ufw allow from 192.168.X.0/24 to any port 59000

To signal a stop

x11vnc -R stop

If you want just for your current session:

Create home password

x11vnc -storepasswd $HOME/.vnc/passwd

Create a start script x11vnc-start, make sure it is in your $PATH and with execute permissions.
It uses -auth /home/<username>/.Xauthority to connect to existing gdm3 X session.
Make sure to use your <username> here.

#!/bin/sh
myip=$(hostname -I | awk '{ print $1 }')
ssh -L $myip:59000:$myip:5900 -C -N -l $(whoami) $myip -f
x11vnc -forever -shared -display :1 -rfbauth $HOME/.vnc/passwd -auth $HOME/.Xauthority
chmod +x x11vnc-stop

Create desktop file

nano $HOME/.local/share/applications/x11vnc.desktop

Copy these lines, it will call x11vnc-start in a terminal window because the ssh forwarding needs elevation.

[Desktop Entry]
Name=X11VNC Server
Comment=Share this desktop by VNC
Icon=computer
Type=Application
Terminal=true
Exec=gnome-terminal -e "x11vnc-start"

Create a stop script x11vnc-stop. It signals x11vnc to stop and kills ssh with SIGTERM. Put it in your $PATH and permission to execute.

#!/bin/sh
x11vnc -R stop
ps aux | grep -e "ssh.*5900" | grep -v grep | awk '{ print $2 }' | xargs kill -15
chmod +x x11vnc-stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment