溫故知新,在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

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM