1.概念
VNC是一个远程连接工具
VNC is used to display an X windows session running on another computer. Unlike a remote X connection, the xserver is running on the remote computer, not on your local workstation. Your workstation ( Linux or Windows ) is only displaying a copy of the display ( real or virtual ) that is running on the remote machine.
There are several ways to configure the vnc server. This HOWTO shows you how to configure VNC using the 'vncserver' service as supplied by CentOS.
2.安装
2.1客户端:
yum install vnc
2.2 服务端:
1) 保证已安装windows桌面:yum groupinstall "GNOME Desktop Environment"
或者最小化安装:yum install xterm
2) 安装vnc-server:
yum install vnc-server
3.设置并运行
3.1 创建一个用于vnc连接的用户user01,并切换到user01登陆,当然,你也可以直接用root用户或其他已存在的普通用户:
[clouder@ana53 soft]$ su root
密码:
[root@ana53 soft]# useradd user01
[root@ana53 soft]# passwd user01
更改用户 user01 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@ana53 soft]# su - user01
[user01@ana53 ~]$ cd ~
[user01@ana53 ~]$ ll .vnc
ls: cannot access .vnc: No such file or directory
3.2 执行vncserver ,第一次执行会要求设置用于客户端通过vnc连接该用户的密码,以后想更改可以通过vncpasswd更改该连接密码。
注意最后问是否设置一个只读密码要选否,否则客户端登陆只会看到一个黑屏:
[user01@ana53 ~]$ vncserver
You will require a password to access your desktops.
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
xauth: file /home/user01/.Xauthority does not exist
New 'ana53:3 (user01)' desktop is ana53:3
Creating default startup script /home/user01/.vnc/xstartup
Creating default config /home/user01/.vnc/config
Starting applications specified in /home/user01/.vnc/xstartup
Log file is /home/user01/.vnc/ana53:3.log
默认是从1开始,但我当前已经有2个vncserver运行,所以现在是3了,第一次执行vncserver后,会在该用户的家目录下创建一个.vnc目录
[user01@ana53 ~]$ ll .vnc/
total 40
-rw-rw-r-- 1 user01 user01 23646 Aug 14 09:56 ana53:3.log
-rw-rw-r-- 1 user01 user01 5 Aug 14 09:55 ana53:3.pid
-rw-r--r-- 1 user01 user01 332 Aug 14 09:55 config
-rw------- 1 user01 user01 8 Aug 14 09:55 passwd
-rwxr-xr-x 1 user01 user01 92 Aug 14 09:55 xstartup
3.3 vncserver -list 查看当前用户开了多少个vncserver,ps -ef |grep vnc看到我有其他2个用户也起了vncserver,这个可以忽略哈:
[user01@ana53 ~]$ vncserver -list
TigerVNC server sessions:
X DISPLAY # PROCESS ID
:3 2338[user01@ana53 ~]$ ps -ef|grep vnc
user1 1750 1 0 09:54 pts/4 00:00:00 /usr/bin/Xvnc :2 -auth /home/user1/.Xauthority -desktop ana53:2 (user1) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /home/user1/.vnc/passwd -rfbport 5902 -rfbwait 30000
user01 2338 1 1 09:55 pts/2 00:00:00 /bin/Xvnc :3 -auth /home/user01/.Xauthority -desktop ana53:3 (user01) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /home/user01/.vnc/passwd -rfbport 5903 -rfbwait 30000
user01 3215 2268 0 09:56 pts/2 00:00:00 grep --color=auto vnc
root 31155 1 0 09:34 ? 00:00:01 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop ana53:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /root/.vnc/passwd -rfbport 5901 -rfbwait 30000
3.4 客户端连接服务端:
本机也可以作为客户端vnc连接到自己,有条件的再找一个机器作为客户端连接比较符合实际应用场景,我使用本机作为客户端通过vnc连接自己,
我通过这个网址下载vncviewer 客户端工具:
https://www.techspot.com/downloads/downloadnow/5760/?evp=bec5c83724706eae8692a26f014582ed&file=4
输入127.0.0.1:5903 ,输入我设置的vnc密码,连接成功。但出现花屏,原因是分辨率设得太低,于是vnc连接成功后:
鼠标放到顶部,点击"Properties"图标--Options--General--Picture quality:设置为Medium,High,或Automatic,点击右下方ok确认
4.其他问题
4.1 第一次执行vncserver,里面好像有句错误提示bad display name in ……,原因是解析不了这个hostname,通过在/etc/hosts添加ip 和hostname映射解决:
[root@ana53 .vnc]# vncserver
xauth: (stdin):1: bad display name "ana53:2" in "add" command New 'ana53:2 (clouder)' desktop is ana53:2 Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/ana53:2.
解决方法:vi /etc/hosts:
192.168.1.253 ana53
4.2 vncviewer连接,始终黑屏:
1) 可能你在第一次给用户设置vnc密码时,最后Would you like to enter a view-only password (y/n)? 回答了y,所以,你应该登陆ssh那个用户,运行vncpasswd,重新设置密码,不要再回y,要回复n;
2) 可能是~/.vnc/xstartup 配置文件问题,贴出我2个用户的配置,root用户的是我修改的,user01是默认的,但是2个都能正常运行。
[root@ana53 .vnc]# cat xstartup
#!/bin/sh
#unset SESSION_MANAGER
#unset DBUS_SESSION_BUS_ADDRESS
#exec /etc/X11/xinit/xinitrc
# 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 &
[user01@ana53 .vnc]$ cat xstartup
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec /etc/X11/xinit/xinitrc
4.3把vncserver做成系统服务,这样比较方便,固定端口号n,而且可以设置开机自启,要有sudo权限才能关闭。
注意,复制vncserver@.service的时候,必须改为vncserver@:n.service,n表示第几个vncserver,默认从1开始,记得必须要冒号,否则会服务是起不来的:
1) 复制系统服务,并重命名
如此案例中我的user01用户开的vncserver 的display窗口是第3号,所以我要把这个vncserver做成系统服务,就复制一个vncserver@:3.service到/etc/systemd/system 目录下,这个根据每个人的具体情况而定,当然,可以运行vncserver :n 来指定要起第几号窗口:
[root@ana53 .vnc]# cp /usr/lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:3.service
2)修改复制后的系统服务里面的<USER>
为你想要通过vnc登陆的用户,注意要把尖括号去掉了
注意PIDFile=/home/<USER>
这里,如果是root用户,应该把/home去掉,因为root用户的home目录不是在/home下,而是在/根目录下。
[root@ana53 .vnc]# vim /etc/systemd/system/vncserver@:4.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 %i > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'[Install]
WantedBy=multi-user.target
修改后:
[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 %i > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l user01 -c "/usr/bin/vncserver %i"
PIDFile=/home/user01/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'[Install]
WantedBy=multi-user.target
3) 重载系统服务,修改了服务文件,必须重载才能生效:
[root@ana53 .vnc]# systemctl daemon-reload
4) 启动该vncserver,并设为开机自启:
[root@ana53 .vnc]# systemctl status vncserver@:4.service
● vncserver@:4.service - Remote desktop service (VNC)Loaded: loaded (/etc/systemd/system/vncserver@:4.service; disabled; vendor preset: disabled)Active: active (running) since 二 2018-08-14 10:27:21 CST; 9min agoMain PID: 9930 (Xvnc)CGroup: /system.slice/system-vncserver.slice/vncserver@:4.service‣ 9930 /usr/bin/Xvnc :4 -auth /home/user01/.Xauthority -desktop ana53:4 (user01) -fp catalogue:/etc/X11/fontpath.d -geometry 102...8月 14 10:27:17 ana53 systemd[1]: Starting Remote desktop service (VNC)...
8月 14 10:27:21 ana53 systemd[1]: Started Remote desktop service (VNC).[root@ana53 .vnc]# systemctl enable vncserver@:4.service
Created symlink from /etc/systemd/system/multi-user.target.wants/vncserver@:4.service to /etc/systemd/system/vncserver@:4.service.
5) 关闭vncserver
方法一:使用vncserver -kill :n 命令,注意,kill后面有一个空格。这个命令只能kill 掉自己的vncserver,因为他是去/home/$user/.vnc/去找pid的:
[user01@ana53 .vnc]$ ps -ef|grep vnc
user1 1750 1 0 09:54 pts/4 00:00:00 /usr/bin/Xvnc :2 -auth /home/user1/.Xauthority -desktop ana53:2 (user1) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /home/user1/.vnc/passwd -rfbport 5902 -rfbwait 30000
root 3623 1 0 10:00 ? 00:00:00 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop ana53:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /root/.vnc/passwd -rfbport 5901 -rfbwait 30000
user01 18521 1 1 11:02 ? 00:00:00 /usr/bin/Xvnc :4 -auth /home/user01/.Xauthority -desktop ana53:4 (user01) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /home/user01/.vnc/passwd -rfbport 5904 -rfbwait 30000
user01 19146 2268 0 11:03 pts/2 00:00:00 grep --color=auto vnc
[user01@ana53 .vnc]$ vncserver -kill :2Can't find file /home/user01/.vnc/ana53:2.pid
You'll have to kill the Xvnc process manually[user01@ana53 .vnc]$ vncserver -kill :4
Killing Xvnc process ID 18521
[user01@ana53 .vnc]$ ps -ef|grep vnc
user1 1750 1 0 09:54 pts/4 00:00:00 /usr/bin/Xvnc :2 -auth /home/user1/.Xauthority -desktop ana53:2 (user1) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /home/user1/.vnc/passwd -rfbport 5902 -rfbwait 30000
root 3623 1 0 10:00 ? 00:00:00 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop ana53:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /root/.vnc/passwd -rfbport 5901 -rfbwait 30000
user01 19302 2268 0 11:03 pts/2 00:00:00 grep --color=auto vnc
方法二:使用systemctl 命令,前提user01有sudo 权限:
[user01@ana53 .vnc]$ sudo systemctl stop vncserver@:2.service
[sudo] password for user01:
[user01@ana53 .vnc]$ sudo systemctl stop vncserver@:4.service
[user01@ana53 .vnc]$ ps -ef|grep vnc
root 3623 1 0 10:00 ? 00:00:00 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop ana53:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /root/.vnc/passwd -rfbport 5901 -rfbwait 30000
user01 20152 2268 0 11:06 pts/2 00:00:00 grep --color=auto vnc
之前一直起不来服务,原来写错了,少了个冒号:
[root@ana53 system]# systemctl status vncserver@:1.service
● vncserver@:1.service - Remote desktop service (VNC)Loaded: loaded (/etc/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled)Active: active (running) since 二 2018-08-14 09:26:00 CST; 2s agoProcess: 28940 ExecStart=/usr/sbin/runuser -l root -c /usr/bin/vncserver %i (code=exited, status=0/SUCCESS)Process: 28938 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)Main PID: 28972 (Xvnc)CGroup: /system.slice/system-vncserver.slice/vncserver@:1.service‣ 28972 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop ana53:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -p...8月 14 09:25:57 ana53 systemd[1]: Starting Remote desktop service (VNC)...
8月 14 09:26:00 ana53 systemd[1]: Started Remote desktop service (VNC).
[root@ana53 system]# systemctl status vncserver@1.service
● vncserver@1.service - Remote desktop service (VNC)Loaded: loaded (/usr/lib/systemd/system/vncserver@.service; disabled; vendor preset: disabled)Active: failed (Result: exit-code) since 二 2018-08-14 09:24:20 CST; 2min 1s agoProcess: 28767 ExecStart=/usr/sbin/runuser -l <USER> -c /usr/bin/vncserver %i (code=exited, status=1/FAILURE)Process: 28763 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)8月 14 09:24:20 ana53 systemd[1]: Starting Remote desktop service (VNC)...
8月 14 09:24:20 ana53 runuser[28767]: runuser: user <USER> does not exist
8月 14 09:24:20 ana53 systemd[1]: vncserver@1.service: control process exited, code=exited status=1
8月 14 09:24:20 ana53 systemd[1]: Failed to start Remote desktop service (VNC).
8月 14 09:24:20 ana53 systemd[1]: Unit vncserver@1.service entered failed state.
8月 14 09:24:20 ana53 systemd[1]: vncserver@1.service failed.