Keep Sharp

Set Up VNC on CentOS

install tigervnc

yum groupinstall Desktop
yum install tigervnc-server

modify /etc/sysconfig/vncservers file

VNCSERVERS="1:user1 2:user2"
VNCSERVERARGS[1]="-geometry 1024x768 -localhost"
VNCSERVERARGS[2]="-geometry 1920x1080 -localhost"

-localhost option is to make vnc encrypted through an ssh tunnel

modify /etc/sysconfig/iptables file

add one line:

-A INPUT -m state --state NEW -m tcp -p tcp -m multiport --dports 5901:5903,6001:6003 -j ACCEPT

restart iptables:

service iptables restart

output:

iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]

set vnc passwd for each user

after ssh into my account

vncpasswd

i can set the password corresponding to my account

start vnc server

service vncserver start

output:

Starting VNC server: 1:user1
New 'Lab:1 (user1)' desktop is Lab:1

Starting applications specified in /home/user1/.vnc/xstartup
Log file is /home/user1/.vnc/Lab:1.log

2:user2 
New 'Lab:2 (user2)' desktop is Lab:2

Starting applications specified in /home/user2/.vnc/xstartup
Log file is /home/user2/.vnc/Lab:2.log

                                                           [  OK  ]

try restart or stop also to make it work

if met problems like this:

Starting VNC server: 1:user1 A VNC server is already running as :1
                                                           [FAILED]

it means there are already some process occupy the port 6001, using ps ef | grep vnc does not give me anything, because this port is actually occupied by Xvfb, using lsof -i tcp:6001 will give the pid of the process which occupy the port, after killing that process, vnc server will be able to start normally.

[user1@Lab .vnc]$ ls
passwd  Lab:1.log  Lab:1.pid  xstartup
[user2@Lab .vnc]$ ls
passwd  Lab:2.log  Lab:2.pid  xstartup

usage

after ssh into the server, run vncviewer localhost:1 using corresponding vnc password will show the desktop, can also try vncviewer localhost:2 using vnc password set by others.

Try openning a firefox, close vncviewer, then connect again, firefox is still there.

reference

1. How to install VNC server on CentOS 6

2. VNC CentOS Wiki

3. A VNC server is already running as :10

Comments