Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save littlecxm/6f74a28585afd9ded63306829e9fe7a2 to your computer and use it in GitHub Desktop.
Save littlecxm/6f74a28585afd9ded63306829e9fe7a2 to your computer and use it in GitHub Desktop.
VNC Server setup on Ubuntu 22.04 LTS

Configure

Install TigerVNC

$ sudo apt install tigervnc-standalone-server

Allow VNC port (if ufw enabled)

$ sudo ufw allow 5900
$ sudo ufw allow 5901

Configure VNC Server by running

$ vncserver

This will setup a password to access the server through a remote session.

Create new /etc/vnc directory

$ sudo mkdir /etc/vnc

Create a new file /etc/vnc/xstartup

#!/bin/sh

test x"$SHELL" = x"" && SHELL=/bin/bash
test x"$1"     = x"" && set -- default

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS

vncconfig -iconic &
"$SHELL" -l << EOF
export XDG_SESSION_TYPE=x11
export GNOME_SHELL_SESSION_MODE=ubuntu
dbus-launch --exit-with-session gnome-session --session=ubuntu
EOF
vncserver -kill $DISPLAY

Make the /etc/vnc/xstartup file executable

$ sudo chmod +x /etc/vnc/xstartup

Create/add this file ~/.vnc/xstartup with the following contents

#!/bin/sh
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources

Make the file executable

$ chmod +x ~/.vnc/xstartup

Add system service

Create a new service file to configure VNC Server to startup as a service on boot -

$ sudo vim /etc/systemd/system/vncserver@service

Add the following to the contents of the newly created file

[Unit]
Description=Start TigerVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=someuser
Group=someuser
PAMName=login
WorkingDirectory=/home/someuser

PIDFile=/home/someuser/.vnc/%H:590%i.pid
ExecStartPre=-/bin/sh -c "/usr/bin/vncserver -kill :%i > /dev/null 2>&1"
ExecStart=/usr/bin/vncserver -fg -depth 24 -geometry 1920x1080 -localhost no :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

Update the permissions of the file

$ sudo chmod +x /etc/systemd/system/vncserver@service

Reload the daemons so that it can load the new VNC service

$ sudo systemctl deamon-reload

Kill any existing VNC servers and enable this service

$ vncserver -kill :*
$ sudo systemctl enable --now vncserver@1.service

Check the service status

$ sudo systemctl status vncserver@1

Alternative to using a service

Add a new crontab by running

$ crontab -e

and adding

@reboot /usr/bin/vncserver -fg -depth 24 -geometry 1920x1080 -localhost no :1 &

Test VNC

Test VNC by this command line

/usr/bin/vncserver -fg -depth 24 -geometry 1920x1080 -localhost no :1 &

References

  1. https://www.nodinrogers.com/post/2021-11-15-connecting-to-ubuntu-via-vnc-default-wm/
  2. https://askubuntu.com/questions/1159513/unable-to-connect-through-vnc
  3. https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-22-04
  4. https://acloudguru.com/hands-on-labs/configuring-a-host-firewall-on-ubuntu-using-ufw
  5. https://gist.github.com/indyfromoz/739cd53d47b91ba1d3b540ab53b1f46c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment