(Ubuntu Server without Default Desktop/DM) install and configure TightVNC on Ubuntu 18.04

(Ubuntu Server without Default Desktop/DM) install and configure TightVNC on Ubuntu 18.04

Run the following command to install Xfce desktop:

sudo apt-get install xfce4 xfce4-session tightvncserver

When installation completes, start a new VNC session by using the tightvncserver command:

tightvncserver

When tightvncserver command executes for the very first time, you will be prompted to set up a password (we can change the password at a later time with tightvncpasswd command). The command will also create a few configuration files required for the Ubuntu VNC server.

Next, we need to stop the VNC server and edit startup script. First stop Ubuntu VNC server with following command:

tightvncserver -kill :1

Then, open ~/.vnc/xstartup and make sure it is similar to following configuration:

#!/bin/sh

unset SESSION_MANAGER
xrdb $HOME/.Xresources
xsetroot -solid grey
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
startxfce4 &

Save the config file and start a new VNC session:

tightvncserver :1 -geometry 1366x768 -depth 24

We have started a new VNC session with desktop ID 1 and 1366x768 screen resolution.

Start and Stop Ubuntu VNC Server

To start a VNC session with desktop ID 1, run:

tightvncserver :1 -geometry 1366x768 -depth 24
tightvncserver :2 -geometry 1366x768 -depth 24

To manually stop the VNC Server on your Ubuntu 18.04 server, run the command tightvncserver -kill and provide the same ID you used when starting the VNC Server.

tightvncserver -kill :1

 

f:id:johnyuan2000:20190424163605p:plain

vnc_viewer

Automatically Starting the VNC Server on Ubuntu 18.04

Perform the following steps to ensure that the Ubuntu VNC Server will automatically start when your Ubuntu 18.04 server rebooted.

Create a new systemd unit file called vncserver@1.service under the /etc/systemd/system directory.

sudo vim /etc/systemd/system/vncserver@1.service

Insert the following lines:

[Unit]
Description=Start a VNC Session at startup With Desktop ID 1
After=multi-user.target network.target

[Service]
Type=forking
# IMPORTANT!!! Change 'username' to actual user that connect to the session
User=<your_username>
ExecStartPre=/bin/sh -c '/usr/bin/tightvncserver -kill :%i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/tightvncserver :%i -geometry 1366x768 -depth 24
ExecStop=/usr/bin/tightvncserver -kill :%i

[Install]
WantedBy=multi-user.target

Reload the systemd and enable the vncserver@1.service:

sudo systemctl daemon-reload
sudo systemctl enable vncserver@1.service

Disable the service If you don't want to start the VNC session when your Ubuntu 18.04 server rebooted:

sudo systemctl disable vncserver@1.service