Centos7安装VNCserver,并设置为开机⾃启动服务的⽅法
1.概念
VNC是⼀个远程连接⼯具
centos vim命令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 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 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 客户端连接服务端:
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 ago
Main 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 -
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 an
a53:2 (user1) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /home/user1/.vnc/passwd -rfbport 5902 -rfbwait 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 user01 19146 2268 0 11:03 pts/2 00:00:00 grep --color=auto vnc
[user01@ana53 .vnc]$ vncserver -kill :2
Can'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 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 ago
Process: 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 ago
Process: 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.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论