OS:RHEL7.2
step1:安裝vncserver端,這裏使用的是tiger vnc
yum install tigervnc-server
yum install tigervnc
注意:使用vnc之前先得保證能夠開啓xwindows,所以如果你沒有xwindows,需要安裝:yum groupinstall "GNOME Desktop"
關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
關閉selinux
step2:複製並修改配置文件
cp /lib/systemd/system/[email protected] /etc/systemd/system/vncserver@:1.service ==這是給root的配置文件
cp /lib/systemd/system/[email protected] /etc/systemd/system/vncserver@:2.service ==這是給shichunda用戶的配置文件
vim /etc/systemd/system/vncserver@\:1.service ==修改成如下內容,注意紅色字體
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill :1 > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l root -c "/usr/bin/vncserver :1"
PIDFile=/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill :1 > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
vim /etc/systemd/system/vncserver@\:2.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill :2 > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l shichunda -c "/usr/bin/vncserver :2 -geometry 1280*720 -depth 24"
PIDFile=/home/shichunda/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill :2 > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
step3:加載配置文件
systemctl daemon-reload
step4:配置vnc密碼
su - root
vncpasswd
su - shichunda
vncpasswd
注意:也可以在vncserver :1啓動的時候設置vnc密碼
step5:開啓vnc server
方法1:
[root@file system]# vncserver :2
You will require a password to access your desktops.
Password:
Verify:
New 'file:2 (root)' desktop is file:2
Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/file:2.log
方法2:
systemctl start vncserver-root@\:1.service
注意:有的時候用這種方法不能啓動vnc server。建議使用方法1
step6:客戶端連接
客戶端打開vnc viewer,之後輸入192.168.107.105:1,之後輸入root的密碼即可==作爲root登錄
客戶端打開vnc viewer,之後輸入192.168.107.105:2,之後輸入shichunda的密碼==作爲shichnda登錄
查看vncserver
[root@file system]# vncserver -list
TigerVNC server sessions:
X DISPLAY # PROCESS ID
:1 31143
關閉vncserver
[fanguihua@file ~]$ vncserver -kill :3
Killing Xvnc process ID 37239
關於分辨率
默認連接到server上是1024*768分辨率,可以在server的桌面設置分辨率
在vncserver@:2.service配置文件中設置-geometry 1280*720 -depth 24沒有什麼效果
##################################
問題
1 用root作爲:1之後,shichunda用戶就不能用:1了?
是的
2 root只有一個配置文件的話,如果啓動多個的話也是可以的,但只有:1能夠登錄,因爲該用戶只有1個配置文件
vncserver :1
vncserver :2
vncserver :3
[root@file system]# vncserver -list
TigerVNC server sessions:
X DISPLAY # PROCESS ID
:1 31143
:2 33659
:3 34164
此時如果在用shichunda用戶啓動:2的話,會報錯,如下:==因爲root已經佔用了:2了
shichunda的配置文件如下:
[shichunda@file ~]$ more /etc/systemd/system/vncserver@\:2.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill :2 > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l shichunda -c "/usr/bin/vncserver :2 -geometry 1600*900"
PIDFile=/home/shichunda/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill :2 > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
報錯內容如下:
[shichunda@file ~]$ vncserver :2
Warning: file:2 is taken because of /tmp/.X2-lock
Remove this file if there is no X server file:2
A VNC server is already running as :2
把root的:2和:3關掉,用shichunda啓動:2纔可以