基本情况:博主是Ubuntu16.04 系统,远程桌面的服务器系统centos 7.
参考:https://zhuanlan.zhihu.com/p/34940240
有适当修改。
Step 1: 在CentOs7上安装Tigervnc-server
1. 安装Tigervnc-server
Tigervnc-server is a program which executes an
Xvnc server and starts parallel sessions of
Gnome or other Desktop Environment on the VNC desktop.
同个用户可以通过多个客户端使用VNC会话。在CentOS7上安装Tigervnc-server请打开终端,使用root用户权限安装:
sudo yum -y install tigervnc-server
2. 安装完毕后,切换到你想使用Tigervnc-server的用户,然后使用下列命令对VNC设置密码,需要注意的是密码长度必须为6位以上:
3. 接下来,在系统配置文件路径下为你的用户添加一个VNC服务配置文件(daemon configuration file)。需要注意的是拷贝至系统路径需要root权限。
加入当前用户不具有root权限,请切换到root用户(su - root
)或者使用以下命令:
sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
4. 下一步,编辑从系统路径(/etc/systemd/system/)拷贝过来的VNC的模板配置文件。将其中的用户名改为你的用户名。
(注意) 在 @后面的数字1表示的是显示界面的序列号,对应的端口是port 5900+序列号。对于每一个启动的vncserver服务,端口号5900会自增1。
sudo vim /etc/systemd/system/vncserver@\:1.service
添加下列行到覆盖原来的vncserver@:1.service. 注意:下面的两处xxx替换为自己的而用户名
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
User=xxx
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=-/usr/bin/vncserver -kill %i
ExecStart=/usr/bin/vncserver %i
PIDFile=/home/xxx/.vnc/%H%i.pid
ExecStop=-/usr/bin/vncserver -kill %i
[Install]
WantedBy=multi-user.target
5. 添加完毕后,重新运行系统systemd的初始化程序以便使新的配置文件生效,然后重启TigerVNC server
与此同时,检查VNC service的状态同时启用VNC daemon system-wide。
systemctl daemon-reload
systemctl start vncserver@:1
systemctl status vncserver@:1
systemctl enable vncserver@:1
6. 我们可以查看VNC server占用的端口号,使用命令ss
——CentOS 7下用来显示网络sockets 占用的命令。因为我们刚刚打开了一个显示会话,所以目前打开的端口应该是5901/TCP.
同样的,使用ss
命令需要root权限。假如不同用户登录到了本机,对应的端口号就应该是5902,接下来应该是5903 等等,端口6000+是用于X应用连接到VNC server的.
7. 为了从外界访问到CentOs,你还需要确保VNC server对应的端口可以通过防火墙
目前我们只打开了一个端口5901,我们使用以下命令将5901添加到防火墙规则。
firewall-cmd --add-port=5901/tcp
firewall-cmd --add-port=5901/tcp --permanent
Step 2: 使用VNC Client连接到CentOS桌面
8. 连接
Being a platform independent protocol, remote
Graphical User Interface VNC connections can be performed from almost any operating system with a GUI and a specialized VNC client.
A popular VNC client used in
Microsoft based operating systems, fully compatible with
Linux TigerVNC server, is the
RealVNC VNC Viewer.
打开VNC Viewer,在VNC Server一栏输入CentOs的ip地址,需要加上对应的端口号(192.168.1.23:5901),敲击回车。
当连接建立以后,会有warning提醒连接没有加密,点击继续即可。
9. 接下来会弹出密码确认窗口,输入在第二点中设置的密码,敲击ok
即可连接到CentOs7
(注意)第一次连接可能出现显示分辨率失常的情况,断开重新连接以及进入桌面后调整Display的分辨率可以使其显示正常。