Skip to content

Instantly share code, notes, and snippets.

@indyfromoz
Last active April 10, 2024 13:55
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save indyfromoz/739cd53d47b91ba1d3b540ab53b1f46c to your computer and use it in GitHub Desktop.
Save indyfromoz/739cd53d47b91ba1d3b540ab53b1f46c to your computer and use it in GitHub Desktop.
VNC Server setup on Ubuntu 22.04 LTS

Setup

Install TigerVNC -

$ sudo apt install tigervnc-standalone-server

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/startup with these lines -

#!/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

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

$ sudo vi /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 1280x800 -localhost no :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

Update the permissions of the newly created file with

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

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

$ sudo systemctl deamon-reload

Enable the VNC service so that it loads on boot -

sudo systemctl enable vncserver@1.service

Kill any existing VNC servers and start the service -

$ vncserver -kill :*
$ sudo systemctl start vncserver@1

Check the status of the service with -

$ 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 &

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
@dirky9000
Copy link

In the line you have, "Create a new file /etc/vnc/startup with these lines -" I believe you meant "xstartup". Also when reloading the daemons you put "sudo systemctl deamon-reload" when it should be "daemon"

@denispeyrusaubes
Copy link

For me, I had to rename filename vncserver@.service instead of sudo vi vncserver@service.

BTW, remote connection is OK for one minute, then vncserver is stopped. Syslog says :

vncserver@1.service: start operation timed out. Terminating.

Any idea ?

@saiema
Copy link

saiema commented Sep 25, 2023

When trying to start the service it fails and the report I get says this, I don't see any log to see what happened.

× vncserver@1.service - Start TigerVNC server at startup
     Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2023-09-25 17:12:32 UTC; 5s ago
    Process: 161262 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=1/FAILURE)
    Process: 161267 ExecStart=/usr/bin/vncserver -fg -depth 24 -geometry 1280x800 -localhost no :1 (code=exited, status=255/EXCEPTION)
        CPU: 15ms

Sep 25 17:12:32 ibp-int-full-2 systemd[1]: Starting Start TigerVNC server at startup...
Sep 25 17:12:32 ibp-int-full-2 systemd[161262]: pam_unix(login:session): session opened for user iBP(uid=1001) by (uid=0)
Sep 25 17:12:32 ibp-int-full-2 systemd[161267]: pam_unix(login:session): session opened for user iBP(uid=1001) by (uid=0)
Sep 25 17:12:32 ibp-int-full-2 systemd[1]: vncserver@1.service: Control process exited, code=exited, status=255/EXCEPTION
Sep 25 17:12:32 ibp-int-full-2 systemd[1]: vncserver@1.service: Failed with result 'exit-code'.
Sep 25 17:12:32 ibp-int-full-2 systemd[1]: Failed to start Start TigerVNC server at startup.

@EarningEvolved
Copy link

EarningEvolved commented Dec 8, 2023

Hi! It looks like you have one error in a step of this process.
You said:

Create a new file /etc/vnc/startup with these lines

I think you meant:

Create a new file /etc/vnc/xstartup with these lines

You missed the letter "x" in that step.

@a90091343
Copy link

a90091343 commented Dec 31, 2023

1、If the ubuntu-desktop package is installed, you do not need to create the /etc/vn/xstartup and ~/.vn/xstartup files, just run the following code:
cat < ~/.xsessionrc
export GNOME_SHELL_SESSION_MODE=ubuntu
export XDG_CURRENT_DESKTOP=ubuntu:GNOME
export XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
EOF
It can be used normally.
2, your /etc/systemd/system/vncserver@service need to change to the following:
[Unit]
Description=Start TigerVNC server at startup
After=syslog.target network.target

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

ExecStartPre=-/bin/sh -c "/usr/bin/vncserver -kill :%i > /dev/null 2>&1"
ExecStart=/usr/bin/vncserver -fg -depth 24 -geometry 1280x800 -localhost no :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

"That is, change Type to simple and remove PIDFile"

@alfredojaramillo
Copy link

When trying to start the service it fails and the report I get says this, I don't see any log to see what happened.

× vncserver@1.service - Start TigerVNC server at startup
     Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2023-09-25 17:12:32 UTC; 5s ago
    Process: 161262 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=1/FAILURE)
    Process: 161267 ExecStart=/usr/bin/vncserver -fg -depth 24 -geometry 1280x800 -localhost no :1 (code=exited, status=255/EXCEPTION)
        CPU: 15ms

Sep 25 17:12:32 ibp-int-full-2 systemd[1]: Starting Start TigerVNC server at startup...
Sep 25 17:12:32 ibp-int-full-2 systemd[161262]: pam_unix(login:session): session opened for user iBP(uid=1001) by (uid=0)
Sep 25 17:12:32 ibp-int-full-2 systemd[161267]: pam_unix(login:session): session opened for user iBP(uid=1001) by (uid=0)
Sep 25 17:12:32 ibp-int-full-2 systemd[1]: vncserver@1.service: Control process exited, code=exited, status=255/EXCEPTION
Sep 25 17:12:32 ibp-int-full-2 systemd[1]: vncserver@1.service: Failed with result 'exit-code'.
Sep 25 17:12:32 ibp-int-full-2 systemd[1]: Failed to start Start TigerVNC server at startup.

Hello! Did you get to troubleshoot this issue?

@Ungsin
Copy link

Ungsin commented Mar 26, 2024

When trying to start the service it fails and the report I get says this, I don't see any log to see what happened.

× vncserver@1.service - Start TigerVNC server at startup
     Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2023-09-25 17:12:32 UTC; 5s ago
    Process: 161262 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=1/FAILURE)
    Process: 161267 ExecStart=/usr/bin/vncserver -fg -depth 24 -geometry 1280x800 -localhost no :1 (code=exited, status=255/EXCEPTION)
        CPU: 15ms

Sep 25 17:12:32 ibp-int-full-2 systemd[1]: Starting Start TigerVNC server at startup...
Sep 25 17:12:32 ibp-int-full-2 systemd[161262]: pam_unix(login:session): session opened for user iBP(uid=1001) by (uid=0)
Sep 25 17:12:32 ibp-int-full-2 systemd[161267]: pam_unix(login:session): session opened for user iBP(uid=1001) by (uid=0)
Sep 25 17:12:32 ibp-int-full-2 systemd[1]: vncserver@1.service: Control process exited, code=exited, status=255/EXCEPTION
Sep 25 17:12:32 ibp-int-full-2 systemd[1]: vncserver@1.service: Failed with result 'exit-code'.
Sep 25 17:12:32 ibp-int-full-2 systemd[1]: Failed to start Start TigerVNC server at startup.

Hello! Did you get to troubleshoot this issue?

I just changed the type of service.
[Service]
Type=forking
->
Type=simple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment