Walker News

How To Setup Linux VNC Server In 3 Minutes

VNC server works similar to Telnet or SSH daemon, except that it allows VNC clients running on diverse OS platforms to remotely access Linux Desktop Manager of the server itself, rather than the TTY text-based console!

As such, most of Windows-to-Linux users prefer using VNC server than SSH daemon, who are generally favor on the vivid Desktop Manager (either KDE or Gnome) than the cold black-and-white TTY console!
For the newbie (as myself), here is my another 3-minute Linux guide about How to setup VNC server in Red Hat Enterprise Linux?

Though this Linux-VNC guide is tested on Redhat Enterprise Linux 4, it served the general guide of installing VNC in Linux distributions.
  1. Locate the vnc-server-4.0-8.1.rpm package from the Red Hat Linux installation CD or download the latest free version of VNC server from Real VNC.
     
  2. Install the rpm package of VNC server:
    rpm -Uvh vnc-server-4.0-8.1.rpm
     
  3. VNC server is using different set of login authentication than the Linux /etc/passwd. Meaning that, you have to create VNC login password for individual Linux login accounts who are granted remote access with VNC client.
    To create VNC login password, just execute vncpasswd command and enter a valid password twice as usual.
     
  4. Unlike VNC server for Windows, each user account can run more than one VNC servers in Linux.
    To start an instance of VNC server, just execute vncserver command, which will create the $HOME/.vnc directory with VNC default startup scripts inside (if $HOME/.vnc is not currently exists).
     
  5. The vncserver command will startup a VNC server that open 3 next available TCP/IP port, starting from 5801, 5901, and 6001 respectively.
    Port 5801 is for VNC client connection over HTTP protocol, port 5901 is for VNC client connection over RFB protocol, and port 6001 is to allows X applications to connect to the VNC server. The lsof or netstat command will able to show the network port opened by Xvnc server.

    By executing vncserver for the second time or execute the vncserver :2 command, this will startup VNC server that bind and listen to port 5802, 5902, and 6002 respectively.

    To connect to Linux VNC server over HTTP protocol, just type walkernews.net:5801 (replace walkernews.net with your VNC server IP/hostname) at any javascripts-enabled web browser, such as Mozilla Firefox, Opera, or Internet Explorer.

    To connect to Linux VNC server over RFB protocol, just type walkernews.net:5901 at the VNC client.
     
  6. To kill the VNC server in Linux, just execute vncserver -kill :1 will kill the first VNC server that bind and listen to port 5801, 5901, and 6001. The vncserver -kill :2 will kill the second VNC server, and so on.

How to get KDE or Gnome Desktop Manager running in Linux VNC server?

Don’t worry, it’s just a 3-seconds Linux trick! Edit the default VNC server startup scripts $HOME/.vnc/xstartup and un-remark the two “critical” lines, as per the scripts suggestion:
#!/bin/sh

# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
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 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
twm &

Isn’t easy to setup and configure Real VNC in Linux, particularly with Red Hat Linux?

Custom Search

  1. Walker 06-07-07@17:57

    Correction:
    In order to connect VNC server over HTTP protocol, the web browser should be java-enabled and the correct URL should be

    http://walkernews.net:5801 (replace walkernews.net with your VNC server IP/hostname)

    The HTTP:// prefix is a must, or you won’t get the VNC server authentication prompt!

  2. SSH Port Forwarding – Local VS Remote – Walker News 22-07-07@21:00

    [...] your powerful Core 2 Duo home Desktop running Windows Vista Ultimate is not possible to access the Red Hat VNC server over the RFB [...]

  3. Walker 28-07-07@09:31

    If you’re connecting Linux VNC server from Windows VNC Viewer (the client), the working connection string with specific port is

    walkernews.net::5901

    and not

    walkernews.net:5901

    spot the difference

  4. Dave Welsh 14-08-07@21:29

    Excellent How-To! Saved me loads of time faffing about – much appreciated :o))

  5. Configure VNC Server To Auto Start Up In Red Hat Linux – Walker News 20-06-08@17:08

    [...] year, I wrote a post on how to configure VNC server in Red Hat Linux system. But, that 3-minutes setup guide doesn’t mention about how to auto start VNC server when [...]

  6. How To Restart VNC Server After VNC Service Hangs or Stops Unexpectedly? 03-04-09@18:32

    [...] restart that bloody machine. Worst still, simply reboot the machine might not able restart the VNC server. So, what could you do in order to get the VNC server back to work? Precisely, if you can’t [...]

  7. Shyam 08-06-09@22:33

    Thank you very much WALKER……….like u said…..Exactly in 3 minutes……..
    Thanks for saving another one week…………

  8. spongklong 01-07-09@13:36

    it didnt work for me in 3 minutes what the bloody hell?

  9. Paul Peavyhouse 03-09-09@07:04

    Or you could just enable VNC in 3-seconds through “System”->”Preferences”->”Remote Desktop”
    http://www.redhat.com/magazine/006apr05/features/vnc/

    Don’t forget to add port 5900 to your firewall exception (also easy to do via “System”->”Administration”->”Security Level and Firewall”)

    I am running an old RedHat5, so YMMV.

  10. megan 10-09-09@23:03

    Thanks much! Very useful article. Great writing style…was less than 3 mins for me :).

  11. Mithiya 19-10-09@20:01

    Great Doc,
    thanks it works as he said , step by step and inch by inch

    thanks once again.

  12. Scott 21-10-09@11:26

    Excellent tutorial!

    It’s always nice when smart people share

    Thanks.

  13. Peter 12-03-10@07:18

    Cool, worked like a charm… even when the article is sooo 2007!! Thanks.

  14. Tom 10-04-10@09:54

    Worked first go on my beagleboard, w00t! apt-get install vnc-server (for ubuntu)

  15. John 11-05-10@22:03

    I have tried to get this set up on my new redhat server and I want to show the Gnome interface but all I get is the twn screen.

    # Uncomment the following two lines for normal desktop:
     unset SESSION_MANAGER
     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 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
    twm &
    

    any help would be great.

  16. Walker 12-05-10@18:31

    @John, did you restart the vncserver after making changes to xstartup file?

    Try to execute service vncserver restart.

    As vncserver starts up, it will execute the xstartup file of user, e.g. when I start it for root:

    Starting VNC server: 1:root
    New ‘btest:1 (root)’ desktop is btest:1

    Starting applications specified in /root/.vnc/xstartup

  17. marky 04-08-10@09:28

    how to connect with the vncserver with the display port value is :0 or with the default display

    exam:
    connecting from 192.168.0.100:1 to 192.168.0.100:0 or 192.168.0.100

    i want to connect via vnc cliet with the same display as what my server monitor was..can anyone show me and teach me a little trick for this setup?

  18. ravinder singh 08-09-10@00:47

    i want to provide multiple session for user’s for accessing the vncserver from multiple connetcion

  19. Masood 02-11-10@01:59

    Hi,

    I have setup VNC server on a powerful Linux server and am able to initiate a VNC connection but it doesn’t prompt for username but only a password!

    I need 4 users to connect to my VNC server and initiate a GNS3 session but the server doesn’t prompt for username!?

    I have created Linux users account and have done GNS3 installation for each user account.

    Any help that can help me resolve thie issue please?

    Regards,

    Masood

  20. Walker 02-11-10@22:09

    If you have Linux user account A, B and C and would like them to connect the server via VNC client individually, then just repeat the steps for each of those user accounts.

  21. Martin Ooijman 05-12-10@18:05

    John May edit it like this

    #!/bin/sh
    
    # Uncomment the following two lines for normal desktop:
    #unset SESSION_MANAGER
    #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 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
    gnome-session &
    
    #twm &
    

    Edited by Walker:
    So, the differences are

    1) Comment these lines: unset, exec, and twm.
    2) Add gnome-session line.

  22. AKa 08-12-10@05:08

    Thank you soo much…you made my day!

  23. kamran 22-12-10@17:55

    i try to configure vnc at fedora core 8. by yum i install vnc bt but when i enter the commend vncserver :1 -name homeserver -depth 16 -geometry 720×540,it give an error like this

    bash: vncsrever commend not found
    please help me to sort out this problem.

  24. Dileep 19-05-11@21:16

    Hi,

    I am getting an error “SSH Client has been unloaded” while connecting to a unix server from x-windows emulator (ViewNow).

    Can anyone help me in this ?

  25. jxfish2 23-07-11@01:45

    Is there a way to enter a conditional statement inside of the .xstartup file?

    i.e.

    If I open 1 specific VNC session, say on port :2, I want it to open 6 xterm sessions in a pre-defined space.

    On the other hand, if I open any other VNC sessions, on any other ports, I want it to only open 4 xterm sessions, in pre-defined spaces.

    I already have the xterm statements, and geometry’s calculated and working, I just haven’t hit on a way to differentiate between used ports, from inside of the .xstartup file.

    TIA

  26. dhabib77 03-08-11@07:58

    Hi jxfish2,

    You should see your port on the DISPLAY environment variable that was set by the Xvnc process and spawned to your xtartup script.
    Once its readily available, abuse of IF statements at will

    I know it has been a couple of weeks after you’ve asked and yet I still hope i have helped you or anyone in need.

    Sunniest SouthAmerican regards

  27. jxfish2 09-08-11@23:06

    Thanks dhabib.

    I could not find the “$DISPLAY” variable, but I did find the native “$VNCDESKTOP” variable.

    Using this variable, I was able to create a conditional statement to set the number of xterm sessions inside of a VNC session, differently, for different VNC sessions.

    Here’s the conditional statement I used:

    if [[ -n `/bin/echo $VNCDESKTOP | /bin/grep ':2'` ]]

    Now I have one more question, if it’s Ok with you:

    The VNC geometry that I have set inside of the /etc/sysconfig/vncservers is a non-standard entry.

    But, as I have port :2 set for my own personal use, it’s not an issue.

    However, when I open a new session, not previously defined within this config file, my .Xauthority file overrides my preferred VNC geometry, setting it to 1024×768.

    Is there some way for me to set the VNC session geometry locally, outside of the vncserver config file?

    Remember, not the size of the xterm windows, but the actual VNC session geometry.

    In other words, I typically upgrade / work on 6 servers at once, all in parallel.

    I have my default VNC session set to open 6 xterm windows inside of a single VNC session, with each xterm session set to 80×24.

    But, I want to open a new VNC session, with xterm windows that are 80×60, where I can display HOWTO style, cut-n-paste documents. Documents that are prepared in advance of our maintenance windows.

    If nothing more, do you know of a way to reset the default 1024×768 to something different, inside of the local .Xauthority file?

  28. jxfish2 10-08-11@01:40

    Never mind. I figured it out!
    For any of you that might be interested in something similar. I created a separate script to open VNC sessions of 3 different xterm window types:

    • 2 windows, with each xterm window running the length of the VNC session.
    • 4 windows, each of a relatively standard height and width.
    • 6 windows, each of a relatively standard height and width.

    The code will follow in another comment window!

    WINS=$1
    
    if [[ ! -n $1 ]]
    then
    echo “”
    echo “You must enter the number of xterm windows you want to open, as \$1″
    echo “on the command line.”
    echo “”
    echo ” Example:”
    echo “”
    echo ” ./vncserver 4″
    echo “”
    echo “Please try again!”
    exit
    fi
    
    if [[ "$WINS" -eq "2" ]]
    then
    PORT=”20″
    while [[ -n `ps -ef | grep Xvnc | grep "\:$PORT"` ]]
    do
    let PORT=”$PORT + 1″
    done
    /usr/bin/vncserver -geometry 1008×955 :$PORT
    elif [[ "$WINS" -eq "4" ]]
    then
    PORT=”40″
    while [[ -n `ps -ef | grep Xvnc | grep "\:$PORT"` ]]
    do
    let PORT=”$PORT + 1″
    done
    /usr/bin/vncserver -geometry 1008×660 :$PORT
    elif [[ "$WINS" -eq "6" ]]
    then
    PORT=”60″
    while [[ -n `ps -ef | grep Xvnc | grep "\:$PORT"` ]]
    do
    let PORT=”$PORT + 1″
    done
    /usr/bin/vncserver -geometry 1008×990 :$PORT
    fi
    

    To establish a new VNC session, with the pre-configured size, first you need to execute your new script:

    ./myvncserver 2

    ./myvncserver 4

    ./myvncserver 6

    Finally, I need to configure my xstartup script so that it can determine on its own, whether it needs to open 2, 4 or 6 xterm windows.

    xstartup contents to be displayed in the next comments window.

    xsetroot -solid royalblue
    vncconfig -iconic &
    
    if [[ -n `/bin/echo $PORT | /bin/egrep '6[0-9]‘` ]]
    then
    
    # 6 Windows – 1008×990 resolution:
    
    xterm -geometry 80×23+1+0 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    xterm -geometry 80×23+505+0 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    xterm -geometry 80×23+1+330 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    xterm -geometry 80×23+505+330 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    xterm -geometry 80×23+1+660 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    xterm -geometry 80×23+505+660 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    
    elif [[ -n `/bin/echo $PORT | /bin/egrep '4[0-9]‘` ]]
    then
    
    # 4 Windows – 1008×660 resolution:
    
    xterm -geometry 80×23+1+0 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    xterm -geometry 80×23+505+0 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    xterm -geometry 80×23+1+330 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    xterm -geometry 80×23+505+330 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    
    elif [[ -n `/bin/echo $PORT | /bin/egrep '2[0-9]‘` ]]
    then
    
    # 2 Windows – 1008×955 resolution:
    
    xterm -geometry 80×71+0+0 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    xterm -geometry 80×71+504+0 -bg black -fg green -hc white -ls -title “$VNCDESKTOP Desktop” /bin/bash &
    
    fi
    
    twm &
    

    Hope this was useful to someone other than me!
    Note that the window sizes used in the above code fill my desktop sessions perfectly.
    You may need to tweak the sizes according to your own desktop work space.

  29. Lary 29-08-11@10:45

    I have recently inherited a windows network on a Linux server and have changed the ISP now when i go to connect using the VNC Viewer4 I get a error message stating that the “connection was refused Error code 10061″ and guidance in the right direction would be greatly appreciated.

  30. Jambo13 28-12-11@17:22

    Brilliant, thanks for sharing :-)

    Worked first time for me, nice clear instructions

  31. GAP 28-01-12@01:46

    Wonderful, direct to the point every stupid guide I’ve seen has tons of garbage and unnecessary technical explanations. I did follow your guide and had it enabled very quickly!

    Tks!

  32. Rajkumar.m 01-06-12@14:52

    Excellent. I was searching for the port number information you have explained very clearly. Thank you so much

  33. Sam 06-06-12@23:08

    Thanks bro. Your step by step guide works great. Thanks again for writing this.

  34. Aneel 15-11-12@18:18

    Showing following error when running command ‘vncserver’
    Could not determine your computer’s IP address. The error message was
    “get_primary_ip4 does not exist or is not executable”

  35. Walker 15-11-12@23:03
  36. netfighters 17-11-12@02:24

    hey all
    i think Kde is better than gnome-session
    i tried gnome-session in Centos but it does not work correctly just see black screen
    just setup KDE
    and edit the file to run KDE desktop
    and reboot your server
    and it work correctly if you need help in setup VNC on your server

  37. Prabhat 16-02-13@05:04

    gnome-session is working perfectly. You need to uncheck the first box.

  38. Dinesh Rao 03-04-13@19:16

    Hi,

    I have setup VNC server on a powerful Linux server and am able to initiate a VNC connection but it doesn’t prompt for username but only a password!

    I need 4 users to connect to my VNC server and initiate a GNS3 session but the server doesn’t prompt for username!?

    I have created Linux users account and have done GNS3 installation for each user account.

    Any help that can help me resolve thie issue please?

    Regards,
    Dinesh Rao

  39. Walker 04-04-13@01:26

    You should configure the Linux vnc server to run under respective user accounts, each using different listening port number. E.g.:

    user A runs vncserver :1 (i.e. port 5901)
    user B runs vncserver :2 (i.e. port 5902)

    In this case, when user A connecting to port 5902, he/she have to provide vnc password set by user B.

    Thus, there is no need to prompt for user ID, just password will do.

2014  •  Privacy Policy