前置条件:

已开启Linux (Beta)
可能需要开启梯子,本教程不考虑一切网络问题

如果你没有梯子,在某些步骤需要换成国内源

教程所使用的系统版本:

Version 80.0.3987.137 (Official Build) (64-bit)

步骤

进入 termina

使用Ctrl+alt+t开启系统自带的命令行
输入vmc start termina进入vm:

crosh> vmc start termina
(termina) chronos@localhost ~ $ 

使用 lxc 创造并进入容器

输入lxc launch ubuntu:18.04 desktop 创建容器
输入lxc exec desktop bash 进入容器
输入su ubuntu 登入ubuntu用户
输入cd ~ 进入用户目录

(termina) chronos@localhost ~ $ lxc launch ubuntu:18.04 desktop
Creating desktop
Starting desktop
(termina) chronos@localhost ~ $ lxc exec desktop bash
root@desktop:~# su ubuntu
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@desktop:/root$ cd ~
ubuntu@desktop:~$ 

注意:如果你清楚你正在做什么,可以尝试以下操作:

  • 输入lxc remote list查看镜像资源并访问相应网站以更改在这一步建立的镜像
  • 输入lxc remote add以增加新的镜像资源
  • 输入lxc image list以查看或管理本地镜像
  • 修改desktop为其他名字对容器进行命名。
  • 使用root或其他用户进行后续操作

安装相关依赖

依次输入以下命令:
更新源:

sudo apt update

更新软件:

sudo apt upgrade

安装桌面环境(这一步所需的时间比较长,可能需要几十分钟):

sudo apt install ubuntu-gnome-desktop
注意:如果你清楚自己正在做什么,可以替换为其他桌面环境(如kde,xfce等)。
但是可能面临无法连接,无法启动,登录后无内容等信息。本文中所使用的配置与命令是我亲测可用的。

安装VNC相关程序:

sudo apt install xserver-xorg-core tigervnc-standalone-server tigervnc-xorg-extension tigervnc-viewer

配置VNC程序

输入vncserver启动vnc服务器,由于是首次启动,你将看到以下信息:

You will require a password to access your desktops.

Password:
Verify:
Would you like to enter a view-only password (y/n)? n
/usr/bin/xauth:  file /home/ubuntu/.Xauthority does not exist

New 'BaseBionicDesktop.lxd:1 (ubuntu)' desktop at :1 on machine BaseBionicDesktop.lxd

Starting applications specified in /etc/X11/Xvnc-session
Log file is /home/ubuntu/.vnc/BaseBionicDesktop.lxd:1.log

Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/ubuntu/.vnc/passwd :1 to connect to the VNC server.

键入两次访问密码,之后键入n(这一步是询问是否增加一个只读密码,由于我们是在本机使用,不需要这个功能)

输入vncserver -kill :*关闭vnc服务器,你将看到以下信息:

Killing Xtigervnc process ID 1080... success!

输入vi ~/.vnc/xstartup(其中vi可以替换成nano)修改xstartup文件,替换成以下信息并保存退出:

#!/bin/sh
# Start Gnome 3 Desktop 
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
vncconfig -iconic &
dbus-launch --exit-with-session gnome-session &

依次输入以下两个命令开启桌面服务:

sudo systemctl start gdm
sudo systemctl enable gdm

在输入第二个命令的时候可能出现以下信息:

The unit files have no installation config (WantedBy, RequiredBy, Also, Alias
settings in the [Install] section, and DefaultInstance for template units).
This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
   .wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
   a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
   D-Bus, udev, scripted systemctl call, ...).
4) In case of template units, the unit is meant to be enabled with some
   instance name specified.

如果出现此类信息,输入sudo vi /lib/systemd/system/gdm.service修改配置文件,并将以下内容添加到文件末尾:

[Install]
WantedBy=multi-user.target

再输入一次sudo systemctl enable gdm,出现以下信息则说明开启启动设置成功

Created symlink /etc/systemd/system/multi-user.target.wants/gdm.service → /lib/systemd/system/gdm.service.

启动vnc服务器并连接

输入以下命令启动vnc服务器

tigervncserver -localhost no -geometry 1200x900 -depth 24
使用1200x900分辨率是因为pixelbook默认的虚拟分辨率是1200x900,你可以根据需要替换成其它值
进入应用商店下载VNC® Viewer for Google Chrome™插件

新启动一个termina页面或返回至termina页,输入lxc list,可以看到类似以下的信息:

+-------------------+---------+-----------------------+------+------------+-----------+
|       NAME        |  STATE  |         IPV4          | IPV6 |    TYPE    | SNAPSHOTS |
+-------------------+---------+-----------------------+------+------------+-----------+
| desktop           | RUNNING | 100.115.92.197 (eth0) |      | PERSISTENT | 0         |
+-------------------+---------+-----------------------+------+------------+-----------+
| penguin           | STOPPED |                       |      | PERSISTENT | 1         |
+-------------------+---------+-----------------------+------+------------+-----------+

desktop后面的ip地址即为该linux容器的ip地址,打开VNC插件后输入

100.115.92.197::5901

进行连接即可(ip请替换为你自己界面中的ip)

其他可选项

参考网络上的资料,有使用系统服务来在容器启动时自动开启vnc服务的方法,但是我尝试了好久,以服务方式启动连接上之后无法显示桌面环境,暂未找到解决方法。

使用别名来令启动更加简单:
在系统内输入vi ~/.bash_aliases
加入以下内容:

alias runvnc='tigervncserver -kill :* && tigervncserver -localhost no -geometry 1200x800 -depth 24'
alias stopvnc='tigervncserver -kill :*'

之后即可使用runvnc来启动vnc服务,使用stopvnc来关闭vnc服务。

20/05/10 更新:在最近的某次更新之后,ubuntu源( https://cloud-images.ubuntu.com/releases )的镜像都不能使用了,建议改成images源( https://images.linuxcontainers.org )

参考资料:

Install VNC Server with Gnome display on Ubuntu 18.04

How to debug a failed systemctl service (code=exited, status=217/USER)?

linux中如何让命令的别名重启后还生效

标签: none

已有 5 条评论

  1. 谢谢分享!

  2. 我的一直处于连接中,防火墙端口已经打开了还是不行,是什么原因呢?

    1. 你现在使用的梯子是什么,如果是开了全局的梯子的话(比如ssr什么的),有可能会让你的vnc无法连接到本地。(这是我的猜想,按理说如果设备都一样的话我能按照这个步骤完成的你应该也能)

    2. 已经能连接上了,但是是处于黑屏状态,不知道哪里出了问题。

      1. 搜索了一下,发现这不是我能解决的问题,感谢分享经验。

添加新评论