Configuring the VNC server/viewer in Linux.

Overview of VNC

Virtual Networked Computing, is a way of controlling a remote computer just as though you are sitting in front of it. In the Windows world it is also known as remote desktop but it’s normally referred to as VNC in the Linux world. All that happens is that you connect using a VNC client to a remote computer running the VNC server, then an image of the remote desktop is transmitted to your local computer and you can see and control the desktop just as though you are there since all keyboard and mouse commands are sent from your client machine to the server.

vncserver and vncviewer

First check if you already have them installed on your system, open a terminal and type:

$ rpm -qa| grep vnc



Add a user(s)

Next we need to add at least 1 VNC user, open the file /etc/sysconfig/vncservers as root and add the information shown:

#vi /etc/sysconfig/vncservers

# The VNCSERVERS variable is a list of display:user pairs.

# Uncomment the lines below to start a VNC server on display :2 as my ‘myusername’ (adjust this to your own).

#Use “-nolisten tcp” to prevent X connections to your VNC server via TCP.

# Use “-nohttpd” to prevent web-based VNC clients connecting.

# Use “-localhost” to prevent remote VNC clients connecting except when doing so through a secure tunnel.


VNCSERVERARGS[1]=”-geometry 1024×768 -depth 16″

The important part is the VNCSERVERS=”1:alex”, this sets up a users for the vnc server, you can add as many as you like here.

Knowing which port to use

It’s also important to note the session number user as this will tell us which port vncserver will listen on. Remember the Gnome Remote Desktop asked us to use computername:0 as the connection string, the number needs to be added to 5900 to get the listening port. In this case we need to use port 5901 since we are using session 1. In the same way we could use any number, for example:


VNCSERVERARGS[2000]=”-geometry 1024×768 -depth 16″

Setting a password

To add some security we need to add a password that must be given before a connection can be established, open a terminal and type:

$ vncpasswd



This creates a hidden folder called .vnc in your home folder containing the password file.

Starting the server and startup options

To start the server we type the command ‘vncserver’ and the session you wish to start.

$ vncserver :1

Starting VNC server: 1:alex

New ‘linux.alex:1 (alex)’ desktop is linux.alex:1

Starting applications specified in /home/bobuser/.vnc/xstartup

Log file is /home/bobuser/.vnc/linux.alex:1.log

[  OK  ]

Now the server is started and a user could connect, however they will get a plain grey desktop by default as the connection will not cause a new session of X to start by default, to fix this we need to edit the startup script in the .vnc folder in your home directory.

#vi ~/.vnc/xstartup


# Uncomment the following two lines for normal desktop:


exec /etc/X11/xinit/xinitrc

 [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup

[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources

xsetroot -solid grey

vncconfig -iconic &

xterm -geometry 80×24+10+10 -ls -title “$VNCDESKTOP Desktop” &

twm &

Make sure the two lines at the top are uncommented by removing the leading # sign. Next we need to restart vncserver to pick up the changed we just made. To restart the vncserver we need to kill the process and start a new one as root:

#vncserver -kill :1

Killing Xvnc process ID 13728

 $ vncserver :1

Starting VNC server: 1:alex

New ‘linux.alex:1 (alex)’ desktop is linux.alex:1

 Starting applications specified in /home/bobuser/.vnc/xstartup

Log file is /home/bobuser/.vnc/linux.alex:1.log

[  OK  ]

Using vncviewer

To start the viewer type:

$ vncviewer localhost:5901

This open a dialog as shown for us to enter our password we set earlier, enter the password and you should now see a copy of your desktop.

Note that unlike the Gnome Remote Desktop this has started a new session of X so any applications open on the host machine are not visible to the new session, it’s basically a whole new logon running at the same time.

If you just type ‘vncviewer’ at the prompt then you will asked for the host to connect to, then you can type localhost:5901 for example. Remember to use the correct port number when connecting, if you set your VNCSERVERS to be 2000:myname then you would need to connect on localhost:7900.


About Manish Jha

Hi All I am Manish Kumar Jha aka Alex Hunt. I am currently working in VMware Software India Pvt Ltd as Operations System Engineer (vCloud Air Operations). I have around 5 Years of IT experience and have exposure on VMware vSphere, vCloud Director, RHEL and modern data center technologies like Cisco UCS and Cisco Nexus 1000v and NSX. If you find any post informational to you please press like and share it across social media and leave your comments if you want to discuss further on any post. Disclaimer: All the information on this website is published in good faith and for general information purpose only. I don’t make any warranties about the completeness, reliability and accuracy of this information. Any action you take upon the information you find on this blog is strictly at your own risk. The Views and opinions published on this blog are my own and not the opinions of my employer or any of the vendors of the product discussed.
This entry was posted in Server Configuration. Bookmark the permalink.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s