CentOS - How to set up XFCE4 and VNC

Intro

In this tutorial I’ll show you how to configure a remote desktop session, using XFCE 4 and VNC in CentOS 7.x.

Install needed packages

Install XFCE and X Window Manager:

sudo yum groupinstall "X Window system"
sudo yum groupinstall xfce

Install some other handy tools:

sudo yum install nano tmux xarchiver mousepad wget

Install TigerVNC:

sudo yum install tigervnc-server tigervnc-server-minimal

Configuration

Initiate the vnc configuration:

vncserver

List any vnc session:

vncserver -list

If any vnc session is available, close it (using the session number):

vncserver -kill :1

Now backup the default configuration and create a new one using vim.

mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
nano ~/.vnc/xstartup

Paste the configuration below.

#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &

Save and exit.

Next, copy the default ‘Xresources’ configuration in the user home directory.

cp /etc/X11/Xresources ~/.Xresources

And make the ‘xstartup’ script executable by changing its access permissions. Then run the ‘vncserver’ command again.

chmod +x ~/.vnc/xstartup
vncserver

The new vnc session is running with our default desktop XFCE.

In this tutorial, we will run the VNC server as a service. So we need to create new service file for it.

Goto the /etc/systemd/system directory and create a new service file vncserver@.service.

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

Paste the following configuration there (assuming the user is called pier).

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=forking
User=pier
PIDFile=/home/pier/.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

Save the file and exit nano.

Now reload systemd and start the VNC server service.

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

If you don’t get any error, enable the service to launch at system boot and check the service status using systemctl.

sudo systemctl enable --now vncserver@1.service
sudo systemctl status vncserver@1.service

Now, in order to allow a remote access to CentOS, we need to open the Firewall port (in this case port 5901):

sudo firewall-cmd --permanent --zone=public --add-port=5901/tcp
sudo firewall-cmd --reload
comments powered by Disqus