Centos7/Redhat7 较之前的6版本变化比较大,相应的VNC的配置也和之前也不一样了。centos6版本VNC的配置可以参看之前的文章--linux下安装配置VNC 。本篇就主要说下7版本下VNC服务的配置。
1、安装软件
在配置前需要先安装桌面环境和VNC-Server软件
# yum groupinstall "GNOME Desktop"
# yum -y install tigervnc-server tigervnc
2、复制配置文件
我们先尝试通过6版本中的方法,查看VNC的配置文件,得到提示信息如下:
# cat /etc/sysconfig/vncservers
# THIS FILE HAS BEEN REPLACED BY /lib/systemd/system/vncserver@.service
在去查看lib下的vncserver文件时,又给出了如下的提示
# cat /lib/systemd/system/vncserver@.service
# The vncserver service unit file
#
# Quick HowTo:
# 1. Copy this file to /etc/systemd/system/vncserver@:.service
# 2. Edit and vncserver parameters appropriately
# ("runuser -l -c /usr/bin/vncserver %i -arg1 -arg2")
# 3. Run `systemctl daemon-reload`
# 4. Run `systemctl enable vncserver@:.service`
所以我们就按他的提示来进行操作
# cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
3、编辑VNC配置文件
这里我以root用户为例,/etc下复制过去的文件内容修改如下:
[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=/sbin/runuser -l root -c "/usr/bin/vncserver :1 -geometry 1280x720 -depth 24" PIDFile=/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill :1 > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
将文件中的用你当前的用户替换,将%i替换为1 。修改完成后,同样根据提示信息,重载配置,并enable服务开机自启动。
# systemctl daemon-reload
# systemctl enable vncserver@:1.service
# systemctl start vncserver@:1.service
虽然服务已经启动,但由于未配置密码项,所以这时修还没法进行连接,需要通过vncpasswd命令或vncserver命令配置密码,如下:
# vncserver
You will require a password to access your desktops.
Password:
Verify:
xauth: file /root/.Xauthority does not exist
……………………省略
如果上面配置密码的步骤没有,直接连接,会发现没法连接,而且会在/var/log/message中发现如下报错:
Sep 10 00:32:38 361way systemd: Starting Remote desktop service (VNC)...
Sep 10 00:32:38 361way systemd: Failed to reset devices.list on /machine.slice: Invalid argument
Sep 10 00:32:38 361way systemd: Failed to reset devices.list on /machine.slice: Invalid argument
Sep 10 00:32:38 361way systemd: Starting Session c3 of user root.
Sep 10 00:32:38 361way systemd: Started Session c3 of user root.
Sep 10 00:32:38 361way systemd: Failed to reset devices.list on /machine.slice: Invalid argument
Sep 10 00:32:38 361way runuser: You will require a password to access your desktops.
Sep 10 00:32:38 361way runuser: getpassword error: Inappropriate ioctl for device
Sep 10 00:32:38 361way runuser: Password:
Sep 10 00:32:38 361way systemd: vncserver@:1.service: control process exited, code=exited status=1
Sep 10 00:32:38 361way systemd: Failed to start Remote desktop service (VNC).
Sep 10 00:32:38 361way systemd: Unit vncserver@:1.service entered failed state.
4、防火墙配置
如果主机上启用了防火墙,还在要其配置中将端口例外,使用的端口一般为 5910 + N ,其中N为冒号后面的桌面号。
iptables防火墙配置为:# iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 5901 -j ACCEPT
# service iptables save
firewalld 防火墙配置为:
# firewall-cmd --permanent --zone=public --add-service vnc-server
# firewall-cmd --reload
在为另外一个用户配置时,需要重复上面的步骤,并将N的值相应增加,如上面1号被占用了,为oracle 用户又新增的VNC配置为vncserver@:2.service 。还需要注意的是普通用户的家目录和root用户是不同的。通过vnc连接工具输入IP:5901连接后,桌面显示如下: