2020-07-23 Linux 远程连接虚拟桌面

写在前面

  • 我的服务器为Centos7,其他 Linux 发行版可能略有差异。
  • 此方法不需要有物理显示屏。
  • 可以多用户同时登录,同一用户可以开启多个虚拟桌面。

TigerVNC 安装

使用这个软件提供 VNC 服务。
Centos 下安装

sudo yum install -y tigervnc-server

用户模式开启服务

还可以通过系统服务开启,但觉得没必要。根据谁需要谁开启的原则会好一点,安全性也高一点。

  1. 直接在自己的账户下运行 vncserver 。第一次会要求设定密码,后期可以通过 vncpasswd 命令可以修改密码。第一次运行还会初始化,在家家目录下生成 .vnc 文件夹,里边有 config 、localhost.localdomain:1.log localhost.localdomain:1.pid 、passwd 、xstartup 等文件。
$vncserver

You will require a password to access your desktops.

Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used

New 'localhost.localdomain:1 (longfei)' desktop is localhost.localdomain:1

Creating default startup script /home/lo/.vnc/xstartup
Creating default config /home/lo/.vnc/config
Starting applications specified in /home/lo/.vnc/xstartup
Log file is /home/lo/.vnc/localhost.localdomain:1.log
  1. 查看运行状态。如果没有其他问题,查看log文件,显示如下。
$cat localhost.localdomain\:1.log

Xvnc TigerVNC 1.8.0 - built Nov  2 2018 19:05:14
Copyright (C) 1999-2017 TigerVNC Team and many others (see README.txt)
See http://www.tigervnc.org for information on TigerVNC.
Underlying X server release 12001000, The X.Org Foundation

Fri May 22 11:27:09 2020
 vncext:      VNC extension running!
 vncext:      Listening for VNC connections on all interface(s), port 5901
 vncext:      Listening for HTTP connections on all interface(s), port 5801
 vncext:      created VNC server for screen 0

Fri May 22 11:27:13 2020
 ComparingUpdateTracker: 0 pixels in / 0 pixels out
 ComparingUpdateTracker: (1:-nan ratio)
  1. 设置防火墙白名单。从log文件里我们看到 VNC 连接使用的端口是5901,所以要把这个5901端口放行。当然这个端口不是固定的,默认从5901开始往后排,第一个用户开启服务是5901,第二个用户用户开始服务就是5902了。所以管理员可以开通5901-5910端口供用户使用,方法不再赘述。

  2. 关闭服务。localhost.localdomain:1.pid 里边有服务进程号,直接使用 kill 命令结束进程就行。也可以使用 vncserver -kill :1 ,:1 就是刚才运行的第一个服务。

  3. 远程连接。windows 端可以使用 VNC viewer 等软件。直接输入 ip:端口号,然后提示输入密码就行了。

    image

进阶

如果没有任何问题,上面已经可以进入虚拟桌面,使用图形化桌面了。
详细查看官方文档 https://wiki.archlinux.org/index.php/TigerVNC

config

目录下这个文件是配置文件,主要是设置一些默认参数。但是 securitytypes=vncauth,tlsvnc 参数不要随便设置,我copy了官方文档里的设置,结果耗了一天时间没有成功连通,所以不要修改默认就好。具体参数说明参考https://tigervnc.org/doc/vncserver.html 。例如 geometry=2000x1200 设置屏幕大小等。
当然也可以不在 config 文件里写入默认配置,直接在运行 vncserver 时后面加上命令一样的效果。

$cat config
## Supported server options to pass to vncserver upon invocation can be listed
## in this file. See the following manpages for more: vncserver(1) Xvnc(1).
## Several common ones are shown below. Uncomment and modify to your liking.
##
# securitytypes=vncauth,tlsvnc
# desktop=sandbox
# geometry=2000x1200
# localhost
# alwaysshared

xstartup

这个文件主要设置开启的桌面。例如我的服务器上安装了 gnome 桌面,从文档里看我就不需要修改。如果安装的是其他桌面程序,需要看官方文档自行修改。

$cat xstartup
#!/bin/sh

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
/etc/X11/xinit/xinitrc
# Assume either Gnome or KDE will be started by default when installed
# We want to kill the session automatically in this case when user logs out. In case you modify
# /etc/X11/xinit/Xclients or ~/.Xclients yourself to achieve a different result, then you should
# 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值