温故知新,在Windows 11的WSLg(或Multipass)的Ubuntu中安装和运行桌面应用(Snap、Deb)

image

安装和使用Multipass

https://multipass.run

什么是Multipass

Multipass是一个在Linux、macOS和Windows上快速生成云式Ubuntu虚拟机的工具。

它为你提供了一个简单而强大的CLI,使你能够快速访问Ubuntu命令行或创建自己的本地迷你云。

本地开发和测试是一个痛苦的过程,但Multipass通过自动化所有的设置和拆解,使它变得更容易。Multipass有一个不断增长的图像库,使你能够启动特制的虚拟机,或你自己通过其强大的云启动界面配置的自定义虚拟机。

开发人员可以使用Multipass来制作云部署的原型,并在任何机器上创建新鲜、定制的Linux开发环境。Mac和Windows用户可以使用Multipass作为在其系统上获得Ubuntu命令行的最快捷方式。新的Ubuntu用户可以把它作为一个沙盒来尝试新的东西,而不影响他们的主机,也不需要双启动。

获取安装Multipass

安装之后,它会主动写一个PATH值。

检测安装后的版本

multipass version

image

查找镜像

multipass find

image

Image                       Aliases           Version          Description
core                        core16            20200818         Ubuntu Core 16
core18                                        20211124         Ubuntu Core 18
18.04                       bionic            20220411         Ubuntu 18.04 LTS
20.04                       focal,lts         20220411.2       Ubuntu 20.04 LTS
21.10                       impish            20220309         Ubuntu 21.10
appliance:adguard-home                        20200812         Ubuntu AdGuard Home Appliance
appliance:mosquitto                           20200812         Ubuntu Mosquitto Appliance
appliance:nextcloud                           20200812         Ubuntu Nextcloud Appliance
appliance:openhab                             20200812         Ubuntu openHAB Home Appliance
appliance:plexmediaserver                     20200812         Ubuntu Plex Media Server Appliance
anbox-cloud-appliance                         latest           Anbox Cloud Appliance
charm-dev                                     latest           A development and testing environment for charmers
docker                                        latest           A Docker environment with Portainer and related tools
minikube                                      latest           minikube is local Kubernetes

创建并启动新实例

https://discourse.ubuntu.com/t/working-with-multipass-instances/8422

multipass launch

然后可能会成功,并且存在一个输出:

multipass launch
...
Launched: consistent-greyhound

image

这个consistent-greyhound就是新实例的名称,我们也可以看到它跑起来没

multipass list

image

查看指定实例的信息

multipass info $instanceName

image

也可以定制一些参数来创建

multipass launch --name UbuntuImpish --mem 8G --disk 25G --cpus 4 impish

image

其中这个impish指代Ubuntu 21.10这个版本,因为它的代号是Impish Indri

如果安装Ubuntu 20.04,那么命令可以是

multipass launch --name UbuntuFocal --mem 8G --disk 25G --cpus 4 focal

image

如果安装Ubuntu 18.04,那么命令可以是

multipass launch --name UbuntuBionic --mem 8G --disk 25G --cpus 4 bionic

image

管理Ubuntu实例

关闭指定实例

multipass stop $instanceName

image

删除指定实例

multipass delete $instanceName

image

恢复指定实例

multipass recover $instanceName

image

清空所有已删除实例

multipass purge

image

进入到Ubuntu实例

可以单独对实例下发指定命令

multipass exec $instanceName $command

进入指定实例控制台

multipass shell $instanceName

例如:

multipass shell consistent-greyhound

image

设置Root密码并切换到Root

首次设置Root密码

sudo passwd

image

切换到Root账户

su root

image

允许建立本地挂载映射

对了,还需要使用管理员权限打开终端才行。

image

在Windows上默认这个Mount功能是关闭的,需要单独设置打开。

multipass set local.privileged-mounts=true

image

设置和本机的共享目录

首先还是切到根目录创建一个hello文件夹

cd /

image

mkdir hello

image

退出回到外面

exit

image

创建共享,本地目录+虚拟机里面路径

multipass mount /Users/xxxxx/Downloads consistent-greyhound:/hello

image

这时候,你再回到Ubuntu里面就可以访问到这个目录的文件了。

image

安装Ubuntu的桌面环境

https://blog.csdn.net/ppp2006/article/details/47168941

sudo apt-get install xserver-xorg

这个没试过,可以跳过,后面还有个专门的部分

Ubuntu中切换包源

https://blog.csdn.net/qq_36938617/article/details/114887500

获取国内包源

https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

以Ubuntu 22.04为例:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

以Ubuntu 21.10为例:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-proposed main restricted universe multiverse

以Ubuntu 20.04为例:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

以Ubuntu 18.04为例:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

以Ubuntu 16.04为例:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse

备份原有包源

cd /etc/apt/

image

cp /etc/apt/sources.list /etc/apt/sources.list.bak

image

编辑Ubuntu包源配置文件

vim sources.list

image

image

敲下dG清空文本内容。

image

再敲下i进入编辑状态

image

粘贴上面对应Ubuntu版本的国内包源清单,这里版本要对应哈,右键即可粘贴

image

按Esc退出并输入:wq保存。

image

Ubuntu中安装应用

通过DPKG方式安装DEB包

deb包是Debian,Ubuntu等Linux发行版的软件安装包,扩展名为.deb,是类似于rpm的软件包,Debian,Ubuntu系统不推荐使用deb软件包,因为要解决软件包依赖问题,安装也比较麻烦。

sudo dpkg -i xxxxxx.deb

例如:

sudo dpkg -i ./cn.xxxxxxxxxx.xxxx-linux_x64-8.3.16.42-0.deb

image

通过SNAP方式安装SNAP包

Snap是一种全新的软件包管理方式,它类似一个容器拥有一个应用程序所有的文件和库,各个应用程序之间完全独立。. 所以使用snap包的好处就是它解决了应用程序之间的依赖问题,使应用程序之间更容易管理。

sudo snap install xxxxx.2_amd64.snap

例如:

sudo snap install xxxxxxx.3.16.47_amd64.snap --dangerous --devmode

image

image

也可以直接安装指定名字的软件,例如:

sudo snap install libreoffice

image

image

运行已安装的程序

如果你成功安装了,那么最好切换到安装目录看看启动名称是什么。直接输入启动名称就可以了。

$productName

image

Ubuntu中Snap

什么是SNAP包

https://cn.ubuntu.com/blog/what-is-snap-application

Snap包是Ubuntu 16.04 LTS发布时引入的新应用格式包。目前已流行在Ubuntu且在其他如Debian、Arch Linux、Fedora、Kaili Linux、openSUSE、Red Hat等Linux发行版上通过snapd来安装使用snap应用。

image

当你在安装完snap后,你会发现在在根目录下会出现如/dev/loop0的挂载点,这些挂载点正是snap软件包的目录。Snap使用了squashFS文件系统,一种开源的压缩,只读文件系统,基于GPL协议发行。一旦snap被安装后,其就有一个只读的文件系统和一个可写入的区域。应用自身的执行文件、库、依赖包都被放在这个只读目录,意味着该目录不能被随意篡改和写入。

squashFS文件系统的引入,使得snap的安全性要优于传统的Linux软件包。同时,每个snap默认都被严格限制(confined),即限制系统权限和资源访问。但是,可通过授予权限策略来获得对系统资源的访问。这也是安全性更好的表现。

image

Snap可包含一个或多个服务,支持cli(命令行)应用,GUI图形应用以及无单进程限制。因此,你可以单个snap下调用一个或多个服务。对于某些多服务的应用来说,非常方便。前面说到snap间相互隔离,那么怎么交换资源呢?答案是可以通过interface(接口)定义来做资源交换。interface被用于让snap可访问OpenGL加速,声卡播放、录制,网络和HOME目录。Interface由slot和plug组成即提供者和消费者。

预装了Snap的发行版

https://snapcraft.io/docs/installing-snapd

image

Snap除了在Ubuntu 桌面和其他Linux发行版桌面系统上使用外,还能在Ubuntu server和Ubuntu Core上使用且为Ubuntu Core默认应用格式包,Ubuntu Core是迷你,与Ubuntu一致,专为物联网设备、嵌入式平台设计。

目前,Ubuntu的相关产品已以snap包的形式发布,例如Ubuntu MAASJujuMultipassMicroK8sMicroStack等等。借助snap,你可以一键安装专为笔记本工作站打造的Kubernetes和OpenStack,省去了安装等待和繁琐配置过程。对于开发和测试团队来说无疑是一个更高效的方案,将更多的精力和资源投入到关键价值上。

Snap历史演变

Snap包类似于一个容器,完全独立于系统,它包含一个应用程序需要用到的所有文件和库(Snap包含一个私有的root文件系统里面包含依赖的软件包),他们安装在独立的目录中且各个应用之间相互隔离,互不影响,而且安装快捷简单,运行容易管理;主要支持的主流发行版Ubuntu和Debian。

它类似于windows的软件安装包将所需的依赖都统一打包到软件包中部署时只需拷贝snap文件即可,俨然就是一个镜像容器。

Snap前身是由Canonical进行开发和管理的snappy演变而来用的是和Flatpak对抗,在Ubuntu 20.04 TLS的focal的发行版本中默认自带。

core软件包是snap的核心运行时,几乎所有的snap包都依赖core运行时,如果您Ubuntu发行版本低于18.04您可能需要在安装其它的软件包的时候先安装core

Snap优缺点:

优点:

  • 安装简单、稳定、自动升级以及自带配置环境无需自行配置
  • 得益于 Canonical 在中国大陆的运营,下载速度还是可以的

缺点:

  • 配置不灵活
  • 软件包巨大,好像运行一个软件包都需要挂载一个镜像,说是为了解决依赖问题,但……
  • 字体渲染支持很烂
  • 启动速度巨慢,了解后发现这个 Snap 果然还是把软件全部打包并压缩做成镜像,每次都要解压缩再挂载

Snap从Ubuntu 16.04已经开始内置。

SNAP包常用命令

https://snapcraft.io/docs

0. 安装Snap及Snapcraft

原则上高版本Ubuntu都自带了Snap的支持,但是如果想要补充安装或卸载重装,可以基于如下命令:

sudo apt-get install snapd && sudo apt-get install snapcraft

image

1. 搜索指定包

sudo snap find $targetPackage

例如:

sudo snap find vscode

image

2. 安装指定包

sudo snap install $targetPackage

3. 卸载指定包

sudo snap remove $targetPackage

例如我卸载掉Visual Studio Code

sudo snap remove code

image

4. 查看指定包

sudo snap info $targetPackage

例如查看Visual Studio Code

sudo snap info code

image

5. 查看已装包

sudo snap list

image

6. 更新指定包

sudo snap refresh $targetPackage channel=$targetChannel

这里channel的值包括:lateststable

sudo snap refresh code channel=latest

例如Visual Studio Code

sudo snap refresh code

image

7. 还原之前版本

sudo snap revert $targetPackage

例如:

sudo snap revert vscode

8. 下载指定包

sudo snap download $targetPackage

将会下载.assert.snap两类文件,其中:

  • .assert记载了软件包的元数据信息,包括签名和权限控制信息。
  • .snap是实际安装文件包。

image

最后我们看到,以下载code为例,它会得到有个code_94.assertcode_94.snap两个文件。

image

9. 安装离线包

先注册元数据信息

sudo snap ack $targetPackage.assert

再执行包安装

sudo snap install $targetPackage.snap

如果没有.assert,我们也可以通过一个额外参数绕过安全检查,单独安装Snap包,建议是可信任的官网包或者测试包才这样操作。

sudo snap install $targetPackage.snap --dangerous

其中--dangerous意味着软件不是在“沙盒”环境下执行的,运行过程不受控,因为缺少.assert文件所描述的签名信息和权限控制信息。

这里我们拿Visual Studio Code做个案例

先注册.assert

sudo snap ack code_94.assert

image

然后安装.snap

sudo snap install code_94.snap --classic

image

如果带上--dangerous参数,那么它的情况会变成

sudo snap install code_94.snap --classic --dangerous

image

带与不带还是存在细微差别的。

10. 查看Snap版本

snap --version

image

11. Snap安装位置

/snap
cd /snap/$targetPackage && ls -al

image

通常来说,启动入口在

/snap/$targetPackage/x1/usr/bin/$targetPackage

Ubuntu中安装桌面

https://new.qq.com/omn/20211123/20211123A02RBM00.html

安装桌面和RDP软件

在这之前,我们先更新下包索引。

sudo apt-get update

image

接下来开启安装

sudo apt-get install ubuntu-desktop xrdp -y

这将一次性安装ubuntu-desktopxrdp两个软件。

image

稍等片刻之后,安装完毕

image

另外一个方案:

sudo apt-get install ubuntu-desktop unity compizconfig-settings-manager xrdp -y

安装ubuntu-desktopunitycompizconfig-settings-managerxrdp这几个软件。

image

创建桌面登录用户

创建一个登录用户名

sudo adduser $userName

image

并且给这个用户赋予访问权限

sudo usermod -aG sudo $userName

image

获取当前Ubuntu实例IP

ip a

image

从这里面,我姑且看到了一个ip,等下试试:172.26.52.107

当然,其实还有个办法可以看到这个ip,那就是退出Ubuntu,我们从Multipass自带的List也可以看到它的ip

multipass list

image

通过RDP客户端远程Ubuntu

在Windows 11搜索远程,找到远程桌面连接

image

打开它,输入刚才的IP地址

image

点击连接,弹出的对话框选是

image

我们将看到一个登录对话框

image

输入之前设定用户名和密码,并且点击ok

image

太棒了,你现在看到的是Ubuntu的桌面引导,说明我们已经来到桌面了

image

等待下软件更新

image

是的,没错,你现在看到的就是真正的Ubuntu桌面

image

是的,这看起来简直太棒了

image

查看之前安装的桌面客户端,运行良好

image

Ubuntu中安装Mono

https://www.mono-project.com/download/stable/#download-lin

在Ubuntu 20.04中安装Mono

先把Mono的仓库添加到系统中,并且更新索引。

sudo apt install gnupg ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
sudo apt update

image

image

image

image

再安装Mono

sudo apt install mono-devel

image

sudo apt install mono-complete

image

验证已安装的Mono版本

mono --version

image

image

在Multipass里面运行Docker容器

https://multipass.run/docs/docker-tutorial

multipass launch docker

从Windows Terminal中连接Ubuntu

先确保安装sshd软件

sudo apt-get install openssh-server

image

查找它的ip

ip a

image

在Windows Terminal中,输入如下命令

ssh $targetUserName@$targetIp

例如:

ssh taylorshi@192.168.186.128

image

第一次连接会提示你是否允许,我们回答yes即可,输入对应用户的密码,只要授权正常,差不多就可以连接上了。

如果你是虚拟机,注意这里选的网络连接模式。

image

参考

posted @ 2022-03-22 16:07  TaylorShi  阅读(2934)  评论(0编辑  收藏  举报