How to Use the VNC Remote Desktop Functionality on Linux

Commands, Syntax, and Examples

Two businesswomen looking at a computer
40260/Stockbyte/Getty Images

This article describes how to set up and use remote desktop sessions on Linux using VNC (Virtual Network Computing). VNC is a remote display system that enables you to start a desktop environment on one machine and access it from other computers through an Internet connection. You can set up persistent desktops which will be maintained while you disconnect, so you can continue working exactly where you left off when you re-connect.

This is useful for example when you want to work on the same "desktop" from different locations, and it can be used to run a desktop environment on a server that you don't have physical access to or doesn't have a terminal attached (monitor and keyboard). All you need is a network connection.

So how does it work? You need to install "nvcserver" on the server machine (if not already installed) and "nvcviewer" and client machine (see realVNC for a popular version the VNC software). In order to avoid firewall issues, it is a good idea to use the secure shell ssh to connect from your "viewer" machine to the server on which you want to run the desktop session. The PuTTY package works great for this purpose.

So the first step is to launch an ssh using for example PuTTY. Then you log in to the server and enter:

It will return with with something of the form:

New '" (juser)' desktop is
where '6' is the display number, and "juser" your user name.
A server can run multiple desktops for different users at the same time. The display number (server session number) identifies to the server which terminal a particular desktop is connected to. On Linux systems running X-windows, VNC uses the X-Windows display number as server session number.

Before running "vncserver" you should set up the initialization file "xstartup" in the ".vnc" directory, which should be created in your home directory.

This file contains initialization commands, such as

# Execute common xstartup file 
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup

# Load .Xresources file
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources

# Run the vncconfig helper to enable clipboard transfers and control of the desktop
vncconfig -iconic &

# Launch a GNOME desktop
exec gnome-session &

Now a "desktop" is running on the server waiting to be displayed on your local computer. How do you connect to it? If you installed the realVNC software or downloaded a VNC viewer you run this viewer and enter the server and display number as illustrated in this example:
To avoid mistakes you can copy and paste this string from the system response you received when starting the desktop on the server.

The viewer software will also ask you for password. The first time you use VNC on this server you enter a new password, which will be saved in the .vnc folder. The password is for VNC connections and not related to you user account on the server. After a period of inactivity you may be asked to enter your account password as well to authorize the server access.

Once the password is accepted the desktop window should appear with all the specified graphical user interface elements.

You can disconnect from the desktop by closing the desktop window.

You can terminate the VNC server process (the "desktop") by entering the following command in a shell window on the server:

vncserver -kill :

For example:

vncserver -kill :6 
In order to change the size of the desktop window you can create or edit the file "geometry" in the "vnc" directory in your home directory and insert a line of the form:

export geometry=1920x1058

Where "1920" represents the desired width and "1058" the desired height of the desktop window. It is best to make it match the actual resolution of your screen.

See MobaXterm for an easy to use remote desktop alternative

Was this page helpful?