如何在CentOS 7安装VNC
<p>VNC虚拟网络计算,是一个图形桌面共享系统,可让您使用键盘和鼠标远程控制另一台计算机。</p>
本教程介绍如何在CentOS 7安装VNC服务器。我们还将向您展示如何创建SSH隧道并安全地连接到VNC服务器。
在继续学习本教程之前,请确保您以root具有sudo权限的用户登录。
通常,Linux服务器都未安装桌面环境。如果您要使用vnc连接没有GUI的计算机,第一步就是是安装桌面环境。
CentOS软件仓库中有几种可用的桌面环境。在本教程中,我们将使用Xfce作为我们选择的桌面环境。
它是一种快速,稳定且轻巧的产品,非常适合在远程服务器使用。Xfce可在EPEL软件仓库中安装。
如果您未在服务器上启用EPEL,请运行命令sudo yum install epel-release
。根据您的网络速度,下载和安装Xfce软件包可能需要一些时间。
sudo yum install epel-release
sudo yum groupinstall xfce
安装VNC服务器
CentOS 软件仓库中还有几种不同的VNC服务器,例如TightVNC ,TigerVNC和x11vnc。每个VNC服务器在速度和安全性方面都有其优点和缺点。
我们将使用TigerVNC,它是积极维护的高性能VNC服务器。
要在您的CentOS服务器安装TigerVNC,请运行命令sudo yum install tigervnc-server
。
当VNC服务器安装成后,请运行vncserver
命令以创建初始配置并设置密码。
sudo yum install tigervnc-server
vncserver
系统将提示您输入并确认密码,以及是否将其设置为仅供查看的密码。Would you like to enter a view-only password (y/n)?n。
如果您选择设置仅查看密码,则用户将无法使用鼠标和键盘与VNC实例进行交互。
首次运行vncserver
命令时,它将创建密码文件并将其存储在~/.vnc
目录中。
You will require a password to access your desktops.
Password:
Verify:
Passwords don't match - try again
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used
xauth: file /root/.Xauthority does not exist
New 'localhost.localdomain:1 (root)' desktop is localhost.localdomain:1
Creating default startup script /root/.vnc/xstartup
Creating default config /root/.vnc/config
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/localhost.localdomain:1.log
请注意上面输出中主机名后的:1
,这是vnc服务器的显示端口好。vnc服务器将会监听TCP端口5901
,即5900 + 1。
如果运行vncserver
命令创建第二个实例,它将在使用下一个显示端口即:2
,这意味着VNC服务器将会监听端口5902
,即5900 + 2。
在继续下一步之前,请先停止VNC实例。在我们的例子中,VNC服务器在端口5901运行,显示端口是:1
。因此停止显示端口:1
的是命令vncserver -kill :1
。
vncserver -kill :1
Killing Xvnc process ID 1865
配置 VNC 服务器
现在在CentOS 7安装Xfce和TigerVNC,我们需要配置TigerVNC以使用Xfce。使用你喜欢的编辑器,编辑文件~/.vnc/xstartup
。
在本教程中,我们将使用vim编辑文件~/.vnc/xstartup
。完成后,保存文件并退出vim。
xstartup
文件是TigerVNC服务器启动时运行的脚本,因此~/.vnc/xstartup
文件还需要具有执行权限。运行chmod
命令。
vim ~/.vnc/xstartup
chmod u+x ~/.vnc/xstartup
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
/etc/X11/xinit/xinitrc
1. Assume either Gnome or KDE will be started by default when installed
1. We want to kill the session automatically in this case when user logs out. In case you modify
1. /etc/X11/xinit/Xclients or ~/.Xclients yourself to achieve a different result, then you should
1. be responsible to modify below code to avoid that your session will be automatically killed
if [ -e /usr/bin/gnome-session -o -e /usr/bin/startkde ]; then
vncserver -kill $DISPLAY
fi
如果需要更改VNC服务器启动参数,则可以使用vim创建~/.vnc/config
的文件,并在每行添加一个选项。
geometry=1920x1084
dpi=96
配置防火墙
如果你CentOS 8正在运行防火墙,并且使用firewalld作为防火墙管理工具。则需要打开端口5901的连接。
如果你显示端口是:2
。则需要打开端口5902的连接,以此类推,请随时添加你需要允许的端口。
运行命令sudo firewall-cmd --add-port=5901/tcp --permanent
。
sudo firewall-cmd --add-port=5901/tcp
启动 VNC 服务器
现在我们完成VNC服务器的安装和配置,在CentOS 7 VNC服务器并没有作为Systemd的服务在后台运行。
因此,VNC服务器的启动关闭都是使用vncserver
命令。要启动VNC服务器非常简单运行命令vncserver
即可。
如果仅仅运行vncserver
命令不够用时。你可能需要添加更多选项,运行vncserver --hel
p命令查看更多选项。
vncserver
连接 VNC服务器
要连接到远程服务器,请打开Vncviewer,然后在VNC Server字段输入server_ip:5901
。
您现在可以使用键盘和鼠标从本地计算机开始在远程桌面上工作。如果你的Debian 9未安装tigervnc-viewer。
你可以简单运行命令sudo yum install tigervnc
安装它。如果你的客户端计算机运行的是Windows系统,请点击此处下载tigervnc客户端。
sudo yum install tigervnc
Linux macOS SSH隧道
VNC不是加密协议,可能会受到数据包嗅探。推荐的方法是创建SSH tunnel隧道,使用加密的数据连接到远程服务器。
如果您在计算机正在运行Linux,macOS或其他基于Unix的操作系统,则可以运行ssh
命令轻松创建SSH隧道。
系统将提示您输入用户密码。不要忘记用您的用户名和服务器的IP地址替换username
和server_ip_address
。
ssh -L 5901:127.0.0.1:5901 -N -f -l username remote_server_ip
Windows SSH隧道
Windows用户可以使用PuTTY 。设置SSH tunnel隧道。打开Putty,然后在Host name or IP address
字段中输入您的服务器IP地址。
在Connection
菜单下,展开SSH
并选择Tunnels
。在Source Port
字段中输入VNC服务器端口5901
。
在Destination
字段中输入server_ip_address:5901
,然后单击Add
按钮。
返回Session
页面以保存设置,保存后您无需每次都输入它们。要登录到远程服务器,请选择保存的会话,然后单击Open
按钮。
结论
现在,您应该已经启动并运行VNC服务器,并且可以使用图形界面从本地计算机轻松管理CentOS 7服务器。如有任何疑问,请随时发表评论。