Back to main index

VNC Server for CentOS-7

Read about installing tigervnc-server on CentOS7.

yum install tigervnc-server xorg-x11-fonts-Type1

Copy the VNC Server config file.
Use port :3 which is 5903 instead of :0 or 5900

cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:3.service

Replace <USER> with the user name in the service file.
cat /etc/systemd/system/vncserver@:3.service

Description=Remote desktop service (VNC)


# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l mihai -c "/usr/bin/vncserver %i"
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'


VNC connection is not encrypted. We must use a ssh tunnel.
Open both ports ssh 22 and vnc 5903

iptables -A INPUT -p tcp --dport 22   -j ACCEPT
iptables -A INPUT -p tcp --dport 5903 -j ACCEPT

If you do this on a headless server then install X11, XFCE and lightdm as on CentOS-8.
You have to start XFCE once for the <USER> to create the default desktop.

Set the VNC password for the user.

[mihai@centos: ~]$ vncserver

You will require a password to access your desktops.

Would you like to enter a view-only password (y/n)? n
A view-only password is not used

New 'centos:1 (mihai)' desktop is centos:1

Creating default startup script /home/mihai/.vnc/xstartup
Creating default config /home/mihai/.vnc/config
Starting applications specified in /home/mihai/.vnc/xstartup
Log file is /home/mihai/.vnc/centos:1.log

[mihai@centos: ~]$

By default VNC server will start GNOME Desktop session.
Read here how to switch to XFCE session.

mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
cat > ~/.vnc/xstartup << EOF
xrdb $HOME/.Xresources
startxfce4 &

Grant executable privilages

chmod +x ~/.vnc/xstartup

Enable the VNC service.

systemctl daemon-reload
systemctl start vncserver@:3.service
systemctl enable vncserver@:3.service

It may be needed to reboot the server once.

Download the VNC viewer for Linux from realvnc website and install it to /opt

mkdir -pv /opt/vnc-viewer
mv VNC-Viewer-*-Linux-x64 /opt/vnc-viewer/
cp -v /home/mihai/Code/git-main/script/realvnc/vnc-viewer.png /opt/vnc-viewer 
chown -Rv root:root /opt/vnc-viewer
chcon -Rv -u system_u -t usr_t /opt/vnc-viewer
cat > /usr/share/applications/realvnc.desktop << EOF
[Desktop Entry]
GenericName=VNC Viewer
Comment=Control remote computer

Create the ssh tunnel

ssh -L 5903:$DIGITALOCEAN:5903 -l mihai $DIGITALOCEAN

or connect using a private key

ssh -L 5903:$DIGITALOCEAN:5903 -i ~/.ssh/id_rsa_digitalocean -l mihai $DIGITALOCEAN

Start RealVNC

realvnc shortcut

Add a new connection

realvnc new connection

To connect from a Windows client read here about How to Tunnel VNC over SSH.
You will need PuTTY and RealVNC for this.

See which VNC port is currently open

nmap localhost
22/tcp   open  ssh
25/tcp   open  smtp
5903/tcp open  vnc-3
6003/tcp open  X11:3

Kill a VNC instance

[sammy@centos-server: ~]$ vncserver -kill :1
Killing Xvnc process ID 1323

Troubleshoot a VNC service that refuses to start

systemctl start vncserver@:3.service
Job for vncserver@:3.service failed. See "systemctl status vncserver@:3.service" and "journalctl -xe" for details.

journalctl -xe
May 01 15:32:17 centos-server runuser[9207]: pam_unix(runuser-l:session): session opened for user sammy by (uid=0)
May 01 15:32:17 centos-server runuser[9207]: Warning: centos-server:3 is taken because of /tmp/.X11-unix/X3
May 01 15:32:17 centos-server runuser[9207]: Remove this file if there is no X server centos-server:3
May 01 15:32:17 centos-server runuser[9207]: A VNC server is already running as :3
May 01 15:32:21 centos-server runuser[9207]: New 'centos-server:1 (sammy)' desktop is centos-server:1
May 01 15:32:21 centos-server runuser[9207]: Starting applications specified in /home/sammy/.vnc/xstartup
May 01 15:32:21 centos-server runuser[9207]: Log file is /home/sammy/.vnc/centos-server:1.log
May 01 15:32:21 centos-server runuser[9207]: pam_unix(runuser-l:session): session closed for user sammy
May 01 15:32:21 centos-server systemd[1]: Can't open PID file /home/sammy/.vnc/ (yet?) after start: No such file or directory
May 01 15:32:21 centos-server systemd[1]: Failed to start Remote desktop service (VNC).

Removing temporary (old) directories

cd /tmp/.X11-unix/
rmdir X3
Back to main index