Set Up VNC Server on Linux

Foreword

This tutorial goes over installing and configuring Tiger VNC Server on:


Prerequisites

You will need:

  • One of the aforementioned Linux operating systems
  • Sudo or root access

Steps – CentOS 7 (GNOME)

Install VNC Server

1. Install VNC Server:
# yum install tigervnc-server

Create Systemd Configuration File for VNC Server

2. Create a VNC Server configuration file for systemd:
# cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

3. Edit the configuration file:
# vim /etc/systemd/system/vncserver@:1.service
(replace <USER> with your username – mine is jeff)

Add VNC Server Ports to Firewalld

4. Open port 5901 on the active firewalld zone:
# firewall-cmd --zone=public --add-service=vnc-server --permanent
# firewall-cmd --zone=public --add-port=5901/tcp --permanent
# firewall-cmd --reload

Create a Password for VNC Connections

5. Switch back to the user you want to use with VNC:
# su - <user>
(my user is jeff)

6. Create a password for VNC connections:
$ vncserver
(type in a password you wish to use to connect)

7. Kill the running VNC Server instance:
$ vncserver -kill :1

Enable VNC Server to Start on Boot

8. Reload the systemctl daemon, enable VNC Server on boot, and start VNC Server now:
$ sudo su -
# systemctl daemon-reload
# systemctl enable vncserver@:1.service
# systemctl start vncserver@:1.service

Test the VNC Connection

9. That’s it! Now connect to the VNC server by using any VNC client pointed to <user>@<ip_address>:5901 (ex. jeff@192.168.1.100:5901)


Steps – CentOS 6.9 (GNOME)

Disable NetworkManager and Enable network with a Static IP

1. Disable NetworkManager:
# chkconfig NetworkManager off
# service NetworkManager stop

2. Give a static IP to eth0 (or desired interface):
# vim /etc/sysconfig/network-scripts/ifcfg-eth0

Example values:
DEVICE=eth0
BOOTPROTO=none
IPADDR=192.168.1.220
NETMASK=255.255.255.0
NETWORK=192.168.1.0
PEERDNS=no
DNS1=8.8.8.8
DNS2=8.8.4.4
ONBOOT=yes
NAME=eth0
TYPE=Ethernet
HWADDR=90:e6:ba:c2:61:d1
GATEWAY=192.168.1.1
NM_CONTROLLED=no
IPV6INIT=no
USERCTL=no

3. Enable network and start network:
# chkconfig network on
# service network start

Install VNC Server

4. Install VNC Server:
# yum install tigervnc-server

Enable VNC Server to Start on Boot

5. Enable VNC Server to start on boot:
# chkconfig vncserver on

Add VNC Server Ports to iptables

6. Edit iptables to allow VNC connections:
$ su –
# vim /etc/sysconfig/iptables

Add the following line:
-A INPUT -m state –state NEW -m tcp -p tcp –dport 5901 -j ACCEPT

7. Restart iptables:
# service iptables restart

Create a Password for VNC Connections

8. Switch back to the user you want to use with VNC:
# su - <user>
(my user is jeff)

9. Create a password for VNC connections:
$ vncpasswd
(type in a password you wish to use to connect)

Create a sysconfig File for VNC Server

10. Create a sysconfig file for VNC server:
# vim /etc/sysconfig/vncservers

Example values:
VNCSERVERS="1:jeff"
VNCSERVERARGS[1]="-geometry 1280×900"

Test the VNC Connection

11. That’s it! Now connect to the VNC server by using any VNC client pointed to <user>@<ip_address>:5901 (ex. jeff@192.168.1.220:5901)


Steps – Ubuntu 16.04 (Unity)

Note: Unlike most VNC Server tutorials I found online, this one actually configures VNC Server to run with the Unity desktop rather than XFCE.

Install VNC Server

1. Download the latest release of Tiger VNC:
# wget https://bintray.com/tigervnc/stable/download_file?file_path=tigervnc-1.8.0.x86_64.tar.gz

Alternatively, go to https://github.com/TigerVNC/tigervnc/releases and find the binary release download.

2. Extract the .tar.gz file:
# tar -zxvf tigervnc-1.8.0.x86_64.tar.gz

3. Copy the extracted contents to the /usr directory:
# cd tigervnc-1.8.0.x86_64/usr
# cp -R * /usr

Create a Password for VNC Connections

4. Switch back to the user you want to use with VNC:
# su - <user>
(my user is jeff)

5. Create a password for VNC connections:
$ vncserver
(type in a password you wish to use to connect)

6. Kill the running VNC Server instance:
$ vncserver -kill :1

Create an xstartup and .xsession File to Use Unity

7. Make an xstartup file:
$ vim ~/.vnc/xstartup

Place the following within the file:
#!/bin/sh

export XKL_XMODMAP_DISABLE=1
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
OS=`uname -s`
if [ $OS = 'Linux' ]; then
case "$WINDOWMANAGER" in
*gnome*)
if [ -e /etc/SuSE-release ]; then
PATH=$PATH:/opt/gnome/bin
export PATH
fi
;;
esac
fi
if [ -x /etc/X11/xinit/xinitrc ]; then
exec /etc/X11/xinit/xinitrc
fi
if [ -f /etc/X11/xinit/xinitrc ]; then
exec sh /etc/X11/xinit/xinitrc
fi
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources

xsetroot -solid grey
xterm -geometry 1280x960 -ls -title "$VNCDESKTOP Desktop" &
vncconfig -iconic &
unity &

8. Create an .xsession file:
$ vim ~/.xsession

Place the following within the file:
/usr/lib/gnome-session/gnome-session-binary --session=ubuntu &
/usr/lib/x86_64-linux-gnu/unity/unity-panel-service &
/usr/lib/unity-settings-daemon/unity-settings-daemon &

for indicator in /usr/lib/x86_64-linux-gnu/indicator-*; do
basename=`basename ${indicator}`
dirname=`dirname ${indicator}`
service=${dirname}/${basename}/${basename}-service
${service} &
done

unity

Enable VNC Server to Start on Boot

9. Create a systemd startup script for VNC Server:
# vim /etc/systemd/system/vncserver.service

Example file (note my username is jeff and the hostname is mg-rex-ubuntu1604):

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

[Service]
Type=fork
User=jeff
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x960 :1
PIDFile=/home/jeff/.vnc/mg-rex-ubuntu1604:1.pid
ExecStop=/usr/bin/vncserver -kill :1

[Install]
WantedBy=multi-user.target

10. Enable the startup script:
# systemctl daemon-reload
# systemctl enable vncserver.service

Test the VNC Connection

11. That’s it! Now connect to the VNC server by using any VNC client pointed to <user>@<ip_address>:5901 (ex. jeff@192.168.1.225:5901)

NOTE: Alternative to step 9 and 10, you can also create a .profile file for just the user you want to run under:

9-alt. Create a .profile file to enable VNC Server to start automatically when logged in:
$ vim ~/.profile

Example contents:
#!/bin/bash
# starts vnc server
echo -e "starting vnc server @ $(date)...\n" >> /home/jeff/vnc-log.txt
/usr/bin/vncserver >> /home/jeff/vnc-log.txt 2>&1


Steps – Linux Mint (Cinnamon)

Install VNC Server

1. Download the latest release of Tiger VNC:
# wget https://bintray.com/tigervnc/stable/download_file?file_path=tigervnc-1.8.0.x86_64.tar.gz

Alternatively, go to https://github.com/TigerVNC/tigervnc/releases and find the binary release download.

2. Extract the .tar.gz file:
# tar -zxvf tigervnc-1.8.0.x86_64.tar.gz

3. Copy the extracted contents to the /usr directory:
# cd tigervnc-1.8.0.x86_64/usr
# cp -R * /usr

Create a Password for VNC Connections

4. Switch back to the user you want to use with VNC:
# su - <user>
(my user is jeff)

5. Create a password for VNC connections:
$ vncserver
(type in a password you wish to use to connect)

6. Kill the running VNC Server instance:
$ vncserver -kill :1

Enable VNC Server to Start on Boot

7. Create a systemd startup script for VNC Server:
# vim /etc/systemd/system/vncserver.service

Example file (note my username is jeff and the hostname is mg-rex-ubuntu1604):

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

[Service]
Type=fork
User=jeff
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x960 :1
PIDFile=/home/jeff/.vnc/mg-rex-mint:1.pid
ExecStop=/usr/bin/vncserver -kill :1

[Install]
WantedBy=multi-user.target

8. Enable the startup script:
# systemctl daemon-reload
# systemctl enable vncserver.service

Test the VNC Connection

9. That’s it! Now connect to the VNC server by using any VNC client pointed to <user>@<ip_address>:5901 (ex. jeff@192.168.1.100:5901)