Saturday, February 11, 2012

VNC Server in CentOS



1. Installing the required package

yum install -y vnc-server

2.Create your VNC users (ex. user1 and user2)

useradd user1
passwd user1

useradd user2
passwd user2

3. Set your users' VNC passwords

su user1
vncpasswd
exit

su user2
vncpasswd
exit

4. Edit the server configuration

vi /etc/sysconfig/vncservers
-------------- add ---------------
VNCSERVERS="1:user1 2:user2"
VNCSERVERARGS[1]="-geometry 1024x768"
VNCSERVERARGS[2]="-geometry 1024x768"
----------------------------------

5. Create xstartup scripts
* vncserver 시작하면 ~/.vnc/xstartup 파일이 자동으로 생성된다.

service vncserver start
service vncserver stop

su user1
cd ~/.vnc
vi xstartup
----------------------------------------
#!/bin/sh

# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER  <----------- uncomment
exec /etc/X11/xinit/xinitrc  <----------- uncomment

[ -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 &
----------------------------------------
exit

su user2
cd ~/.vnc
vi xstartup
----------------------------------------
#!/bin/sh

# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER  <----------- uncomment
exec /etc/X11/xinit/xinitrc  <----------- uncomment

[ -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 &
----------------------------------------
exit

6. Start the VNC server

service vncserver start


7. For next boot

chkconfig vncserver on

---------- Note -----------
=> VNC server on display 1 will listen on TCP ports 5801, 5901 and 6001
=> VNC server on display N will listen on TCP ports 580N, 590N and 600N

* 5800+N - Java-based vncviewer;
* 5900+N - VNC Client Port;
* 6000+N - X Server port.

# netstat -ntlp | grep vnc
tcp        0      0 0.0.0.0:5801                0.0.0.0:*                   LISTEN      18228/Xvnc
tcp        0      0 0.0.0.0:5802                0.0.0.0:*                   LISTEN      20007/Xvnc
tcp        0      0 0.0.0.0:5901                0.0.0.0:*                   LISTEN      18228/Xvnc
tcp        0      0 0.0.0.0:5902                0.0.0.0:*                   LISTEN      20007/Xvnc
tcp        0      0 0.0.0.0:6001                0.0.0.0:*                   LISTEN      18228/Xvnc
tcp        0      0 0.0.0.0:6002                0.0.0.0:*                   LISTEN      20007/Xvnc
tcp        0      0 :::6001                     :::*                        LISTEN      18228/Xvnc
tcp        0      0 :::6002                     :::*                        LISTEN      20007/Xvnc
---------------------------

8. iptables

vi /etc/sysconfig/iptables

---------------- add -----------------------
# Open VNC for USER1
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5801 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5901 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6001 -j ACCEPT

# Open VNC for USER1
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5802 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5902 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6002 -j ACCEPT
--------------------------------------------
service iptables restart

9. Test from client

$ vncviewer x.x.x.x:5901
$ firefox http://x.x.x.x:5801/