VNC Server Startup after System Reboot on Ubuntu 16.04



There are dozen of guides how to install VNC server on Ubuntu System. But the main problem is that the VNC server doesn’t startup after reboot. And in this video we gonna try to fix that problem. In our case it’s a Ubuntu 16.04.3 Desktop.

 
First of all we need to install desktop environment packages the VNC will be using for the remote connections:   
$ sudo apt-get install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal

Then lets install TightVNC server:
   
$ sudo apt-get install tightvncserver

To complete the initial setup of the VNC server after installation, we should use the vncserver command to set a secure password and create the initial configuration files:

$ vncserver

We will be prompted to enter and confirm a password for remote access to the system.

After confirming the password, you can create a password for viewing only. Users logged in with a view-only password will not be able to control the VNC server instance using the mouse or keyboard. This is a useful feature if you need to demonstrate something using the VNC server, but it is not necessary.

The process then creates the necessary default configuration files and connection data for the server:

Would you like to enter a view-only password (y/n)? n 
xauth: file /home/sammy/.Xauthority does not exist

New 'X' desktop is your_hostname:1

Creating default startup script /home/exsentis/.vnc/xstartup
Starting applications specified in /home/exsentis/.vnc/xstartup Log file is /home/exsentis/.vnc/your_hostname:1.log



Now configure the VNC server :

The VNC server must know what commands to execute at startup. In particular, VNC needs to know which graphical desktop to connect to.

These commands are located in the xstartup configuration file in the .vnc folder in the home directory. The startup script was created when starting vncserver in the previous step, however we will create our own script to start the Gnome desktop.

When VNC is initially configured, a default server instance is started on port 5901. This port is called the display port and is considered by VNC as : 1. You can run multiple instances of VNC on other display ports, including : 2,: 3, etc.
Since we are changing the configuration of the VNC server, we first need to stop the VNC server instance running on port 5901 using the following command:

$ vncserver -kill :1 

The result should look like this, although you will see a different PID:  
Killing Xtightvnc process ID 17648

Before modifying the xstartup file, we should back up the source file:

$ mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

Now lets create a new xstartup file and open it in a text editor:

$ nano ~/.vnc/xstartup

Commands from this file are automatically executed when starting or restarting the VNC server. The VNC server should start our desktop environment if it is not already running. Add the following commands to the file:

#!/bin/sh

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup

[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources

xsetroot -solid grey

vncconfig -iconic &

x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &

x-window-manager &

gnome-panel &

gnome-settings-daemon &

metacity &

nautilus &


In order for the VNC server to use the new startup file, we need to make it executable.

$ sudo chmod +x ~/.vnc/xstartup


Let’s restart the VNC server:

$ vncserver

Now we can test the remote connection to the system using any VNC client and it should be ok. 

Next, we will configure the VNC server as a system service that we can start, stop and restart like any other service. It will also ensure that VNC starts when you reboot your server.
Create a new file named /etc/systemd/system/vncserver@.service
The @ symbol will allow us to pass an argument that we can use to configure the service. We will use it to specify the VNC display port that we want to use when managing the service.
Add the following lines to the file. Be sure to change the values of the User, Group, WorkingDirectory and username parameters to the PIDFILE values corresponding to your username:

$ sudo nano /etc/systemd/system/vncserver@.service

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

[Service] 
Type=forking 
User=exsentis 
Group=exsentis 
WorkingDirectory=/home/exsentis  
PIDFile=/home/exsentis/.vnc/%H:%i.pid

ExecStartPre=/usr/bin/vncserver -kill :%i > /dev/null 2>&1

ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i

ExecStop=/usr/bin/vncserver -kill :%i
 

[Install] 
WantedBy=multi-user.target

The ExecStartPre command stops the VNC server if it is already running. The ExecStart command starts VNC and sets a 24-bit color depth with a resolution of 1280x800. You can change these startup options as needed.
Then inform the system about the new file:

$ sudo systemctl daemon-reload

Then we need to enable the service:

$ sudo systemctl enable vncserver@1.service

The number 1 after the @ symbol indicates on which display the service should appear. In this case, this is the default value : 1.

Stop the current VNC server instance if it is still running:

$ vncserver -kill :1

Then we gonna start the service as any other service:

$ sudo systemctl start vncserver@1
Till that moment VNC service is operatinal and we are able to connect to the remote machine. But if we restart the VNC server machine VNC service won't startup.

Job for vncserver@1.service failed because the control process exited with error code. See "systemctl status vncserver@1.service" and "journalctl -xe" for details.

Let's check what problem causes this error:

$ journalctl -xe 

Can't find file /home/exsentis/.vnc/ubuntu:1.pid

In /home/exsentis/.vnc/ directory the file ubuntu:1.pid is missing. It disappears on each system reboot.

To fix the problem let’s configure the script which will create the file ubuntu:1.pid in /home/exsentis/.vnc/ directory, on every reboot, where ubuntu is hostname:

$ sudo nano /usr/bin/vnc_script

#!/bin/bash 
touch /home/exsentis/.vnc/ubuntu:1.pid

Let's make the script executable:

$ sudo chmod a +x /usr/bin/vnc_script
We should refer to this script in /etc/crontab at the bottom of the file where "exsentis" is a username:

@reboot      exsentis             /usr/bin/vnc_script

On every system reboot that script will create the file ubuntu:1.pid in /home/exsentis/.vnc/ directory.


Now after VNC server machine reboot  VNC service will startup and remote connection to the system will be successful.














Comments

Post a Comment

Popular posts from this blog

Slackware Routing: configuring network interfaces, adding persistent routes, routing for multiple uplinks, policy based routing