ubuntu远程桌面及VNC

一、Ubuntu20.04安装x11VNC远程桌面

1. 安装x11vnc

sudo apt-get install x11vnc

2. 设置密码

sudo  x11vnc -storepasswd

Move密码存储文件到/etc路径,方便配置开机运行服务。

sudo rm /etc/x11vnc.pwd
sudo mv /root/.vnc/passwd /etc/x11vnc.pwd
sudo chown root:root  /etc/x11vnc.pwd

3. 修改配置文件

sudo vim /lib/systemd/system/x11vnc.service
[Unit]
Description=Start x11vnc at startup.
After=multi-user.target

[Service]
Type=simple
#ExecStart=/usr/bin/x11vnc -auth /run/user/1000/gdm/Xauthority -forever -loop -noxdamage -repeat -rfbauth /etc/x11vnc.pass  -rfbport 5909 -shared -capslock -nomodtweak
## 为避免端口5900和系统vino冲突,特将此处端口从5900修改为5911
ExecStart=/usr/bin/x11vnc -auth guess -forever -loop -noxdamage -repeat -rfbauth /etc/x11vnc.pwd -rfbport 5911 -shared -capslock -nomodtweak

[Install]
WantedBy=multi-user.target
  • 4.启动服务
sudo systemctl daemon-reload
sudo systemctl enable x11vnc.service
sudo systemctl start x11vnc.service

日志分析:sudo tail -500f  /var/log/Xorg.0.log

附录:Ubuntu使能桌面共享服务

1. 允许桌面共享

   sudo apt-get install vino 

   sudo vino-preferences

或者直接执行:

sudo gsettings set org.gnome.Vino prompt-enabled true

2 配置远程参数dconf-editor

sudo apt-get install dconf-editor
##启动dconf-editor
dconf-editor

启动dconf-editor,然后打开依次展开org->gnome->desktop->remote-access,将 requre-encryption 设为 False。

二、Windows远程桌面连接ubuntu

remote desktop - Can I access Ubuntu from Windows remotely? - Ask Ubuntu

Yes, you can access Ubuntu from Windows remotely.

Taken from this article.

Follow these steps :

Step 1 – Install xRDP

Open Terminal (Crtl+Alt+T) and execute the following commands:

wget  https://c-nergy.be/downloads/xRDP/xrdp-installer-1.4.6.zip
## 如果有更新,可以访问官网获取最新脚本。
sudo unzip xrdp-installer-1.4.6.zip
sudo chmod +777 xrdp-installer-1.4.6.sh
./xrdp-installer-1.4.6.sh
sudo systemctl restart xrdp

Step 2 – Configure xRDP

sudo vim /etc/xrdp/startwm.sh

Not recommended but you can try to modify /etc/xrdp/startwm.sh as a workaround to run two gnome sessions in parallel for the same user.

sudo vi /etc/xrdp/startwm.sh

unset DBUS_SESSION_BUS_ADDRESS
unset XDG_RUNTIME_DIR

##说明:请放在“test -x /etc/X11/Xsession && exec /etc/X11/Xsession”上面一行。

Step 3 – Restart xRDP

To make all these changes effective, restart xRDP as such:

sudo service xrdp restart

Done.

三、ubuntu虚拟网络控制台VNC之tightvncserver+gnome桌面

tightvncserver: 特别轻量级,类同vnc4server它只能新建桌面会话(即 可以打开若干个桌面供若干用户使用,同一用户也可打开若干桌面),不能查看TTY7/TTY1正在显示的桌面(但x11vnc可以),对比x11vnc安全传输差一些。

All commands must be executed on behalf of the superuser.

1、安装vncserver

sudo apt install tightvncserver

2、安装gnome经典桌面(基于Ubuntu默认自带的Gonme 3.36.4,20.04上成功配置并使用。该桌面是tightvncserver登录的桌面会话中使用的)

sudo  apt install gnome-session-flashback 

The graphical environment is not installed by default on server versions of Ubuntu. Therefore, if we want to connect to a remote desktop, we need to install a graphical shell.

3、启动vnc并设置密码:

sudo vncserver

初次启动vnc执行vncserver命令时会要求设置密码;如果后续修改密码可以使用:

sudo vncpasswd

4、备份并修改/root/.vnc/xstartup文件内容:

# sudo cp  /root/.vnc/xstartup  /root/.vnc/xstartup.org

# sudo vi  /root/.vnc/xstartup 

#!/bin/sh

xrdb $HOME/.Xresources
xsetroot -solid grey
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
# Fix to make GNOME work
export XKL_XMODMAP_DISABLE=1
/etc/X11/Xsession

#### 之上为原始内容,之下为新增配置 ####
## Add for vncserver gnome-session
export XDG_CURRENT_DESKTOP="GNOME-Flashback:GNOME"
export XDG_MENU_PREFIX="gnome-flashback-"
gnome-session --session=gnome-flashback-metacity --disable-acceleration-check &
gnome-terminal &
nautilus &

或者也可以采用如下简洁配置:

#!/bin/sh                                                                       

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
export XKL_XMODMAP_DISABLE=1
export XDG_CURRENT_DESKTOP="GNOME-Flashback:GNOME"
export XDG_MENU_PREFIX="gnome-flashback-"
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
#xsetroot -solid grey    #设置背景色
vncconfig -iconic &    #
#gnome-terminal &    #连接后会直接打开一个terminal窗口
#nautilus &    #连接后会直接打开一个文件窗口
gnome-session --session=gnome-flashback-metacity --disable-acceleration-check &
                           

5、启动vnc

a. 不指定会话号

sudo vncserver -geometry 1920x1080

或者

b. 指定会话号
sudo vncserver -geometry 1920x1080 :2
说明:这里的2指的是端口号,但不是2号端口,需要加上5900,也就是5902 为最终的端口号。

6、增加虚拟控制台桌面软件

默认启动的桌面只有很少的软件,可以继续安装:

sudo apt install gnome-terminal nautilus gnome-settings-daemon gnome-panel metacity 

7、开机自启

Setting up autorun

By default, TightVNC does not have a daemon and does not turn on after a system reboot. To fix this, let's create a new unit in systemd.

sudo  vi /etc/systemd/system/vncserver.service

Insert the following config there:

[Unit]
Description=TightVNC server
After=syslog.target network.target

[Service]
Type=forking
User=root
PAMName=login
PIDFile=/root/.vnc/%H:1.pid
ExecStartPre=-/usr/bin/vncserver -kill :1 > /dev/null 2>&1
ExecStart=/usr/bin/vncserver  -geometry 1920x1080 -depth 24
ExecStop=/usr/bin/vncserver -kill :1

[Install]
WantedBy=multi-user.target

Reload systemd:

sudo systemctl daemon-reload

Enable autorun of the TightVNC server and start it.

sudo systemctl enable --now vncserver

重启服务及查看服务状态:

sudo systemctl restart vncserver.service 

sudo  systemctl status vncserver.service 

8、如果连接失败,需要开启防火墙

If your firewall is enabled, you need to open ports for incoming VNC connections. For one connection, it is enough to open 5901 as shown below. If you need more concurrent connections, open 5902, 5903, etc.

sudo ufw allow 5901/tcp

9、远程VNC连接

Connecting to a remote desktop

Start any VNC client. For Windows, you can use RealVNC or TightVNC Viewer. For Linux - Remmina. Use the IP address of your server with port 5901 (Port号是5900+会话号) as the VNC server.

Enter the password you created earlier and you will see the remote desktop.

To secure the connection - see How to Establish VNC Connection over SSH Tunnel on Ubuntu 20.04.

四、ubuntu远程控制协议对比分析

(一)

可视化界面远程访问Linux服务器(这里以Ubuntu为例),最常见的是通过xrdp和vnc这两种远程桌面协议来进行可视化远程操作。
1.关于xrdp协议和vnc协议之间区别
参考自:http://www.linuxidc.com/Linux/2017-09/147112.htm
1)xrdp协议
  rdp有两种,remote,remotefx
  适用系统:windows、linux
  网络流量:较小,正常使用100-200k左右
  适用场景:由于在色彩、音频、usb及本地磁盘映射方面较好,非常适用于虚拟桌面
2)vnc协议
  适用系统:windows、linux
  网络流量:较小,常用100k左右
  适用场景:主要用于linux的服务器的管理,由于无声音和usb传输,不满足于虚拟桌面的使用
备注:vnc是大部分Linux发行版默认的基于RFB协议的远程桌面程序,但对于普通用户来说,vnc的用户体验并不好,比较慢,还需要安装客户端。个人推荐使用xrdp,并且Windows的远程桌面也是基于RDP协议,兼容性也很好。并且vnc只支持一个桌面,xrdp可以支持多个桌面,所有如果存在多人同时连接Linux的话,xrdp完全是可以支持的,vnc则不行。
2. Windows通过远程桌面访问Ubuntu
参考自:http://www.cnblogs.com/findumars/p/5410914.html
               http://blog.csdn.net/qq_21057881/article/details/71976586

Windows端
Windows端可以通过两种方式访问:
1)Windows自带的远程桌面(Xvnc),这个很简单
说明:
从Ubuntu13.10开始,据说是由于由于xrdp、gnome和unity之间的兼容性问题,在Ubuntu中无法使用xrdp登陆gnome或unity的远程桌面,现象是登录后只有黑白点为背景,无图标也无法操作。
解决办法是:使用xrdp登录xfce的远程桌面,或者也可以使用xrdp登录Mate的远程桌面。

输入在Ubuntu系统的用户名和密码(Ubuntu端系统的用户名和密码)

2)使用window自带的远程桌面,也可以使用VNCViewer,(VNC-any)

这个地方选择Ubuntu端机器的IP,密码则输入在Ubuntu端远程桌面设置时,允许访问的访问密码

1)通过xvnc方式访问远程桌面式时,其实相当于Windows端在Ubuntu机器上新登录一个用户桌面;Ubuntu端是看不到电脑桌面上有什么变化和操作的,而且这种方式,Widows端用户访问速度很快,延迟时间很短,几乎感觉不出来。

2)通过vnc-any方式访问远程桌面时,用户在Windows端的操作同步到ubuntu端,ubuntu端桌面实时看到Windinws端用户的操作。相当于QQ的远程控制桌面。

附录:Ubuntu18.04 VNC Server对比

VNC中比较好用的两个:

tightvncserver: 特别轻量级,它只能新建立桌面(即 可以在一台linux上打开若干个桌面供若干用户使用;同vnc4server),不能查看TTY7/TTY1正在显示的桌面(但x11vnc可以),相比x11vnc安全传输差一些。

x11vnc:安全传输比较好,占用资源比tightvncserver要多。

Ubuntu18.04 LTS 安装 VNC Server[x11vnc,tightvncserver,vnc4server]:

安装组件开启组件监控端口VNC连接情况使用效果
x11vncx11vnc5900可连接,访问正常切换窗口有明显卡顿
tightvncservertightvncserver5901可连接,访问异常只看到了一个灰色屏幕,无法操作
vnc4servervnc4server5901可连接,访问异常只看到了一个灰色屏幕,无法操作
vinosharing5900无法连接无法连接

x11vnc、vino

sharing

5900

无法连接无法连接

tightvncserver、vino

sharing5900无法连接无法连接

vnc4server、vino

sharing5900无法连接无法连接

vino

dconf-editor

sharing

5900

可连接,访问正常使用流畅

x11vnc、vino

dconf-editor

sharing

5900

可连接,访问正常使用流畅,在卸载x11vnc之后,重启电脑,还是可以连接(与VNCServer无关)

tightvncserver、vino

dconf-editor

sharing5900可连接,访问正常使用流畅,在卸载tightvncserver之后,重启电脑,还是可以连接(与VNCServer无关)

vnc4server、vino

dconf-editor

sharing5900可连接,访问正常使用流畅,在卸载vnc4server之后,重启电脑,还是可以连接(与VNCServer无关)

回头来看安装过程(VNC Server不那么重要了,vino,dconf-editor 安装了,配置好就可以使用VNC Client 连接了):

之前需要在Ubuntu18.04 LTS 安装 VNC Server,找了些资料都没能完美解决,后来用了RealVNC,可以参考

Ubuntu18.04 LTS 安装 VNC Server_yidichaxiang的博客-CSDN博客

最近测试XRDP,过程中需要开启VNC支持,找到了一篇不错的文章记录下来:

主要参考:

ubuntu18.04VNC远程配置_gsls200808的博客-CSDN博客_gsls200808 vnc

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行知致简

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值