How to Configure VNC Server in Red Hat Enterprise Linux 6.x/7.x

VNC is a fast and secure way to managing Linux systems via GUI remotely. VNC needs proper configuration to work without issue. Configure VNC on Linux systems will be difficult and confusing for beginners. Fortunately, Red Hat provides an application to create VNC configuration as a script automatically.

I’ve published post about Red Hat Access Lab and you can read the post for more information.

You need to have active account on Red Hat Customer Portal for access to Red Hat Access Lab .

If you don’t have account, then you can use the below scripts to configure VNC on your Red Hat Enterprise Linux or all other Red Hat based Linux systems.

Before run the scripts, make sure that required RPMs have been installed on Linux. You can install needed software by run the below command:

# yum install tigervnc-server

The above command will install all necessary packages for run VNC server on RHEL 6.x or 7.x.

You must install at least the below desktops for accessing to server via VNC:

GNOME : Choose this to launch GNOME Desktop Environment as user’s session.Install related packages if they are not installed:

# yum groupinstall "Desktop" "Desktop Platform"

KDE : Choose this to launch KDE Desktop as user’s session.Install related packages if they are not installed:

# yum groupinstall "KDE Desktop"

Minimal GUI session : Choose this to launch minimal GUI environment as user’s session. This will present xterm running within the session with twm window manager.Install related packages if they are not installed:

# yum install xterm xorg-x11-twm

Red Hat Enterprise Linux 6.x VNC Configuration Script

#!/bin/bash
userNum=1
#Preparetion: Stop the VNC server if it has been already started
service vncserver status
if [ $? -eq 0 ]; then
    service vncserver stop
fi
#Step1: Find the available port(s) for VNC service
port=5901
session=1
count=0
ports=()
sessions=()
currentTimestamp=`date +%y-%m-%d-%H:%M:%S`
while [ "$count" -lt "$userNum" ]; do
    netstat -a | grep ":$port\s" >> /dev/null
    if [ $? -ne 0 ]; then
        ports[$count]=$port
        sessions[$count]=$session
        count=`expr $count + 1`
        echo $port" is available for VNC service"
    fi
    session=`expr $session + 1`
    port=`expr $port + 1`
done

#Step2: Write the VNC configuration into the /etc/sysconfig/vncservers
#Backup configuration files
vnc_conf=/etc/sysconfig/vncservers
vnc_conf_backup=/etc/sysconfig/vncservers.vncconfig.$currentTimestamp
if [ -f "$vnc_conf" ]; then
    echo backup $vnc_conf to $vnc_conf_backup
    cp $vnc_conf $vnc_conf_backup
fi

echo '
VNCSERVERS="'${sessions[0]}':user1"

VNCSERVERARGS['${sessions[0]}']="-geometry 1024x768 -nolisten tcp"

'>/etc/sysconfig/vncservers

#Step3 Set up the VNC password for each user

echo "Please set the VNC password for user user1"

su - user1 -c vncpasswd



#Step 4: Set the desktop enviroment

xstartupContent='#!/bin/sh
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
vncconfig -iconic &
dbus-launch --exit-with-session gnome-session &
'

#Backup files
vnc_conf=~user1/.vnc/xstartup
vnc_conf_backup=~user1/.vnc/xstartup.vncconfig.$currentTimestamp
if [ -f "$vnc_conf" ]; then
    echo backup $vnc_conf to $vnc_conf_backup
    cp $vnc_conf $vnc_conf_backup
fi

echo "$xstartupContent" > ~user1/.vnc/xstartup
chmod 755 ~user1/.vnc/xstartup


#Step5:Start the VNC service
#Start the VNC service
service vncserver start
#Set the VNC service start by default
chkconfig vncserver on
#Verify the VNC Service
chkconfig --list vncserver

#Step6: If default firewall is used, we will open the VNC ports


#Step7: Echo the information that VNC client can connect to
red='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${red}Display number for user user1 is ${sessions[0]}${NC}"

Change “user1” with any username that you want to configure VNC for that. Also you must turn the script to a executable script and run it via shell:

# chmod +x vncconfig.sh 
# ./vncconfig.sh


Red Hat Enterprise Linux 7.x VNC Configuration Script

#!/bin/bash
userNum=1

#Step1: Find the available port(s) for VNC service
port=5901
session=1
count=0
ports=()
sessions=()
currentTimestamp=`date +%y-%m-%d-%H:%M:%S`
while [ "$count" -lt "$userNum" ]; do
    netstat -a | grep ":$port\s" >> /dev/null
    if [ $? -ne 0 ]; then
        ports[$count]=$port
        sessions[$count]=$session
        count=`expr $count + 1`
        echo $port" is available for VNC service"
    fi
    session=`expr $session + 1`
    port=`expr $port + 1`
done

#Step2: Set the VNC password

echo "Please set the VNC password for user user1"

su - user1 -c vncpasswd



#Step3: Write the VNC configuration
#Backup configuration file

vnc_conf="/etc/systemd/system/[email protected]:"${sessions[0]}".service"
vnc_conf_backup=$vnc_conf.vncconfig.$currentTimestamp
if [ -f "$vnc_conf" ]; then
    echo backup $vnc_conf to $vnc_conf_backup
    cp $vnc_conf $vnc_conf_backup
fi
echo "
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/sbin/runuser -l user1 -c \"/usr/bin/vncserver %i -extension RANDR -geometry 1024x768\"
PIDFile=~user1/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target
" > $vnc_conf
chmod a+x $vnc_conf


#Step 4: Set the desktop enviroment

xstartupContent='#!/bin/sh
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
vncconfig -iconic &
dbus-launch --exit-with-session gnome-session &
'


#Write configuration to files and backup files

vnc_desktop_conf=~user1/.vnc/xstartup
vnc_desktop_conf_backup=$vnc_desktop_conf.vncconfig.$currentTimestamp
if [ -f "$vnc_desktop_conf" ]; then
    echo backup $vnc_desktop_conf to $vnc_desktop_conf_backup
    cp $vnc_desktop_conf $vnc_desktop_conf_backup
fi
echo "$xstartupContent" > $vnc_desktop_conf
chmod 755 $vnc_desktop_conf



#Step5:Start the VNC service
#Start the VNC service
systemctl daemon-reload

systemctl enable [email protected]:${sessions[0]}.service
systemctl start [email protected]:${sessions[0]}.service


#Step6: If default firewall is used, we will open the VNC ports


#Step7: Echo the information that VNC client can connect to

red='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${red}Display number for user user1 is ${sessions[0]}${NC}"

Do same changes on the above script and then run it.

Note: If you have firewall on Linux, please configure firewall properly. Also you can export same script from VNC Configurator which includes firewall configurations.

See Also

HPE Management Component Pack (MCP)

[Review]: OpenFabrics Enterprise Distribution (OFED)

[Review]: Packet Drop vs Packet Loss – Linux

Red Hat Access Labs – Review

Davoud Teimouri

Davoud Teimouri is as a professional blogger, vExpert 2015/2016/2017/2018, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

Leave a Reply

Your email address will not be published. Required fields are marked *