VNC server installation on CentOS 6/7

1. Login as root (or any sudoer group user)

2. Uninstall older version of vnc server (if any)

   sudo yum remove vnc vnc-server
   sudo rpm -qa | grep vnc | grep -v gtk-vnc
   sudo rpm -e <output of above command>

3. sudo yum install vnc vnc-server
4. su <user>
5. vncpasswd    [set the password for vnc session]

6. Edit/Create /etc/sysconfig/vncservers

#for multiple user add these lines in end of file:
VNCSERVERS="1:<vncuser1> 2:<vncuser2> 3:<vncuser3>"
VNCSERVERARGS[1]="-geometry 640x480"
VNCSERVERARGS[2]="-geometry 800x600"
VNCSERVERARGS[3]="-geometry 1600x1200"
#for single user:
VNCSERVERS="1:<user name>"
VNCSERVERARGS[1]="-geometry 1600x1200"

 

7. Edit/Create /etc/init.d/vncserver

#!/bin/bash
#
# chkconfig: - 91 35
# description: Starts and stops vncserver. \
# used to provide remote X administration services.

### BEGIN INIT INFO
# Provides: vncserver
# Required-Start: $network $named
# Required-Stop: $network $named
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: start|stop|restart|try-restart|status|force-reload vncserver
# Description: control vncserver which exports your desktop
### END INIT INFO

# Source function library.
. /etc/init.d/functions

[ -r /etc/sysconfig/vncservers ] && . /etc/sysconfig/vncservers

prog=$"VNC server"

RETVAL=0

start() {
 [ "$EUID" != "0" ] && exit 4

# Source networking configuration.
 . /etc/sysconfig/network

# Check that networking is up.
 [ ${NETWORKING} = "no" ] && exit 1

[ -x /usr/bin/vncserver ] || exit 5
 [ -x /usr/bin/Xvnc ] || exit 5

echo -n $"Starting $prog: "
 RETVAL=0
 if [ ! -d /tmp/.X11-unix ]
 then
 mkdir -m 1777 /tmp/.X11-unix || :
 restorecon /tmp/.X11-unix 2>/dev/null || :
 fi

STARTED=0
 for display in ${VNCSERVERS}
 do
 SERVS=1
 echo -n "${display} "
 DISP="${display%%:*}"
 USER="${display##*:}"
 VNCUSERARGS="${VNCSERVERARGS[${DISP}]}"
 runuser -l ${USER} -c "vncserver :${DISP} ${VNCUSERARGS}" < /dev/null
 RETVAL=$?
 [ "$RETVAL" -eq 0 ] && STARTED=`expr $STARTED + 1`
 done
 if [ -z "$SERVS" ]; then
 echo -n "no displays configured "
 failure
 RETVAL=6
 else
 if [ "$STARTED" -gt 0 ]; then
 success $"vncserver startup"
 touch /var/lock/subsys/Xvnc
 else
 failure $"vncserver start"
 fi
 fi
 echo

# As written in https://bugzilla.redhat.com/show_bug.cgi?id=523974 (LSB
# compliance) start of already running service is OK.
 [ "$RETVAL" -eq 98 ] && RETVAL=0

return "$RETVAL"
}

stop() {
 [ "$EUID" != "0" ] && exit 4

echo -n $"Shutting down $prog: "

status Xvnc > /dev/null 2>&1
 RETVAL=$?

STOPPED=0
 # 3 means service is already stopped
 if ! [ "$RETVAL" -eq 3 ]; then
 for display in ${VNCSERVERS}; do
 echo -n "${display} "
 export USER="${display##*:}"
 runuser ${USER} -c "vncserver -kill :${display%%:*}" >/dev/null 2>&1
 RETVAL=$?
 [ "$RETVAL" -eq 0 ] && STOPPED=`expr $STOPPED + 1`
 done
 RETVAL=$?
 else
 let STOPPED=1
 let RETVAL=0
 fi

[ "$STOPPED" -gt 0 ] && success $"vncserver shutdown" || \
 failure $"vncserver shutdown"
 echo
 [ "$STOPPED" -eq 0 ] && rm -f /var/lock/subsys/Xvnc
 return "$RETVAL"
}
# See how we were called.
case "$1" in
 start)
 start
 ;;
 stop)
 stop
 ;;
 restart|force-reload)
 stop
 sleep 3
 start
 ;;
 condrestart)
# https://bugzilla.redhat.com/show_bug.cgi?id=508367
# echo "condrestart is obsolete, use try-restart instead"
 if [ -e /var/lock/subsys/Xvnc ]; then
 stop
 sleep 3
 start
 fi
 ;;
 try-restart)
 if [ -e /var/lock/subsys/Xvnc ]; then
 stop
 sleep 3
 start
 fi
 ;;
 status)
 status Xvnc
 RETVAL=$?
 ;;
 reload)
 exit 3
 ;;
 *)
 echo $"Usage: $0 {start|stop|restart|try-restart|status|force-reload}"
 exit 2
esac

exit "$RETVAL"

8. sudo chmod a+x /etc/init.d/vncserver

9.  sudo service vncserver start
10. sudo service vncserver stop
11. sudo chkconfig vncserver on

12. Edit ~/.vnc/xstartup

Uncomment the following two lines (remove the "#" characters) (if commented out):

unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc

13. sudo service vncserver restart

14.  Start your Vnc client

Login as
<server ip>:5901
Let VNC server choose

 

If you are getting issue during connection firewall settings need to be changed
Sometimes your firewall settings doesn't allow client to connect, 
allow vnc ports using following steps:

Run following commands on vnc server:
iptables -A INPUT -i eth0 -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp -m multiport --dports 5801:5803,5901:5903,6001:6003 -j ACCEPT
service iptables restart or service firewalld restart
service vncserver restart

try to connect now

 

If still you are not able to connect

------------ On VNC server gui ----
system->administration->firewall
Other ports->add
user defined
5801-5803
tcp
[add for 5901-5903 also]
apply

service vncserver restart

try to connect now

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s