--- tags: 環境設置 --- # **Windows Subsystem for Linux (WSL2) 環境設定** ## 0. 目錄 第一篇:[WSL 原理介紹](https://hackmd.io/s/Bkh8oAmGX) 第二篇:本篇 第三篇:[WSL 補充內容](https://hackmd.io/s/SyL3pzzQm) 第四篇:[WSL 搭配 VSC](https://hackmd.io/s/S1LFy-BzQ) :::info 附上近期[投影片教學 (wsl 2版)](https://drive.google.com/file/d/1peY5H2DLpj0KCqFDyyvmEkBRU15s4s9J/view?usp=sharing),未來會在 notion 上重寫 ::: ## 1. 安裝 Linux ### 1.1. 事前須知 首先,請檢查一下手邊的機器有無達到以下條件: WSL2 * 處理器 * x64 架構的處理器 * ARM64 架構的處理器 * 具有以上任一種且具備第二層位址轉譯 (SLAT) 的 64 位元處理器 * 作業系統 * Windows 10 家用版,版本 1903 或更高版本,含 組建 18362 或更高組建。 (x64 的處理器) * Windows 10,版本 2004 或更高版本,含 組建 19041 或更高組建。 (ARM64 的處理器) :::info Windows 10 S版不支援 ::: * 記憶體 * 至少有 4 GB 的記憶體空間。越多記憶體越好。 * 在 BIOS 或 UEFI 中開啟虛擬化支援: * 硬體協助虛擬化。具有 Intel 虛擬化技術的處理器 (Intel VT) 或 AMD Virtualization (AMD) 技術。 * 啟用硬體強制的資料執行防止 (DEP)。對於 Intel 系統而言,這是 (執行停用位) 的 XD 位。 對於 AMD 系統而言,這是 NX 位 (沒有任何執行位) 。 最後,您本人有無達到以下條件 * 了解基本的 Linux 命令列語法 * 勇於嘗試並尋找方法 **本教學適用 Ubuntu 20.04 LTS 以及 Ubuntu 21.10 版** 歡迎搭配以下兩篇一起服用~ [Windows Subsystem for Linux 環境配置 ( 1709 版)](https://medium.com/hungys-blog/windows-subsystem-for-linux-configuration-caf2f47d0dfb) [Windows 10 上適用於 Linux 的 Windows 子系統安裝指南](https://docs.microsoft.com/zh-tw/windows/wsl/install-win10) WSL 更新資訊: * [WSL 官方 github](https://github.com/Microsoft/WSL) * [Release Notes](https://docs.microsoft.com/zh-tw/windows/wsl/release-notes?f=255&MSPPError=-2147217396) * [2004 版及以後的版本](https://devblogs.microsoft.com/commandline/install-wsl-with-a-single-command-now-available-in-windows-10-version-2004-and-higher/) ### 1.2. 啟用 Windows 設定 #### 方法 1: 使用**系統管理員**權限開啟 **Power Shell**,輸入下述指令: ``` DISM /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart DISM /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart ``` <img src="https://i.imgur.com/tPuntRd.png" width=100%> 重新啟動電腦以完成安裝。 #### 方法 2: 到「控制台 > 程式集 > 程式與功能」,點擊「**開啟或關閉 Windows 功能**」。 <img src="https://i.imgur.com/gdkAqch.png" width=100%> 從開啟的小視窗中點選啟用「**適用於 Linux 的 Windows 子系統**」和「**虛擬機器平台**」,確定後重新啟動電腦。 <img src="https://i.imgur.com/nSbzAw6.png" width=60%> ### 1.3. 設定 Focal Fossa Ubuntu 20.04 LTS 可以從 [Windows 商店](https://www.microsoft.com/store/apps/9n6svws3rx71) 取得,沒有標註的預設會安裝**穩定版 LTS** (目前是 20.04),**App 更新不影響版本**,筆者這裡使用最新 20.04 版來說明。 <img src="https://i.imgur.com/EtJaKoj.png" width=100%> > Ubuntu 提供 18.04 與 20.04 特定 LTS 選擇,不同 App 視為不同的 distro。 安裝好後請打開 App,第一次開啟會需要等他安裝組建,完成後需要新增帳號密碼來建立使用者,密碼主要用於 `sudo`。 <img src="https://i.imgur.com/zCI5NyG.png" width=100%> ### 1.4 (選用)安裝 Windows 終端機 **Windows 終端機(Windows Terminal)** 是 Microsoft 公司在 2019 年的 Microsoft Build 大會上宣佈推出一款全新的 Windows 終端機,能夠取代 PowerShell 的命令提示字元。 Windows 終端機可啟用多個索引標籤 (在多個 Linux 命令列、Windows 命令提示字元、PowerShell、Azure CLI 等之間快速切換)、建立自訂按鍵繫結 (開啟或關閉索引標籤、複製+貼上等的快速鍵)、使用搜尋功能及自訂佈景主題 (色彩配置、字型樣式和大小、背景影像/柔邊/透明度)。[官方的介紹短片](https://www.youtube.com/watch?v=8gw0rXPMMPE) <img src="https://i.imgur.com/eihGctQ.png" width=100%> Windows 終端機可以從 [Windows 商店](https://aka.ms/terminal) 取得 <img src="https://i.imgur.com/4R1x7Ok.png" width=100%> 如果在傳統應用程式介面覺得字型大小不好看的話,可以右鍵開啟 App 內容,在裡面調整顯示設定,關於 TrueType 字型在 zsh 會有詳細說明。 <img src="https://i.imgur.com/dJdKDjQ.png" width=80%> > 1803 版之前 App 的內容配置,每次重開似乎都會自動變回新細明體。 設定成功後就會進到此畫面,前面是你的帳號,@後面是你的電腦名稱。 <img src="https://i.imgur.com/XqnT0xM.png" width=90%> 除了透過 App,我們還可以透過**命令提示字元 (cmd) **或是**執行** (`Win`+`R`) 並輸入以下任一種來啟動 WSL: * `bash`:進入 **預設** Linux distro 的 **bash shell** * `wsl`:進入 **預設** Linux distro 的 **default shell** * `ubuntu` 或 `ubuntu2004`:執行特定 **App .exe** 前兩項會根據目前 WSL 預設的 distro 執行,因為目前只安裝 Ubuntu,所以都可以視為相同。 `/mnt/c` 也就是 mount C,是用來支援原生 Windows 檔案系統,詳情可以查看上一篇。 需要注意**使用者名稱**和**根目錄**並非同一個位置,前者存取的是 Windows 下的 Users,後者為專屬 WSL 的檔案系統。 <img src="https://i.imgur.com/MA5lFEC.png" width=80%> > 1703 版前舊版 WSL 的路徑有些不同。 由於 WSL 支援多種 Linux distro,所以我們可以在 cmd 透過 `wslconfig` 來管理,下圖可以看到目前的預設的 distro (App 名稱)。 <img src="https://i.imgur.com/zjoNi9Z.png" width=80%> > 1703 版前舊版 WSL 要透過 `lxrun` 管理。 上面都沒問題的話就更新一下 Ubuntu 的資料庫,最好定期執行。 由於 WSL 內建的 apt 伺服器使用的是位於歐洲(英國)的 http://archive.ubuntu.com/ubuntu ,下載軟體的時間會因各 ISP 的因素導致下載速度緩慢,因此建議修改成離自己地區最近的鏡像站,可以從 [這裡](https://launchpad.net/ubuntu/+archivemirrors) 查詢各地區的鏡像站。 以下使用台灣的 國家高速網路與計算中心(NCHC) 進行修改,提供的頻寬有 20 Gbps 不怕你下載跑不滿。 使用 sed 的指令將 /etc/apt/sources.list 裡面的 archive.ubuntu.com 和 security.ubuntu.com 替換成 free.nchc.org.tw ``` $ sudo sed -i 's/archive.ubuntu.com/free.nchc.org.tw/g' /etc/apt/sources.list $ sudo sed -i 's/security.ubuntu.com/free.nchc.org.tw/g' /etc/apt/sources.list ``` 之後執行以下動作 ``` $ sudo apt clean all # 清除伺服器先前已取得的檔案清單 $ sudo apt update # 更新伺服器的檔案清單 $ sudo apt upgrade # 更新套件 ``` > 有安裝後續 zsh 的 debian 就可以直接用 `ad` 和 `adg`。 > ~~目前 18.04 版在更新套件時會看到 ebtables 的 error,不過這可以先忽略。~~ #### 如要更新 Focal Fossa: * 在 Ubuntu 執行 `do-release-upgrade` (目前測試是沒有辦法找到新 release) * 重新安裝 App #### 如要刪除 Focal Fossa: * 直接解除安裝 App * 透過 `wsl --unregister` 取消註冊 參考資料:[Manage and configure WSL](https://docs.microsoft.com/en-us/windows/wsl/wsl-config) ## 2. 設定 Z shell (zsh) ### 2.1. 特色 一言以蔽之就是實用,像是命令列補全、可載入模組等等,主要都會選擇搭配下面提到的 [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh?source=c) 一起使用。 參考資料:[zsh wiki](http://zshwiki.org/home/) ### 2.2. 安裝 zsh 1. 下載 zsh。 ``` $ sudo apt-get install zsh ``` 2. 檢查一下 zsh 版本。 ``` $ zsh --version zsh 5.8 (x86_64-ubuntu-linux-gnu) ``` 3. 查看已安裝的 shell。 ``` $ cat /etc/shells # /etc/shells: valid login shells /bin/sh /bin/bash /bin/rbash /bin/dash /usr/bin/tmux /usr/bin/screen /bin/zsh /usr/bin/zsh ``` 4. 將 zsh 設為預設 shell。 ``` $ chsh -s $(which zsh) ``` 5. 先關掉 App 再重開。 6. 第一次設定完後會看到下面畫面,請**直接按 0** 建立空白含註解的 `.zshrc`,只要沒有此檔案都會一直回到這個畫面。 <img src="https://i.imgur.com/TLowA79.png" width=90%> 7. 檢查 zsh 是否變為預設,以及版本是否一致。 ``` $ echo $SHELL /usr/bin/zsh $ $SHELL --version zsh 5.4.2 (x86_64-ubuntu-linux-gnu) ``` 8. 安裝 [oh-my-zsh](https://ohmyz.sh/),強大的 zsh 框架,下面擇一種方式安裝即可。 ``` $ sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)" ``` ``` $ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" ``` ### 2.3. 安裝 Powerline 字型 由於部分主題像是 [agnoster](https://github.com/agnoster/agnoster-zsh-theme) 系列,都會用到特殊符號,這種時候就要安裝[ Powerline ](https://github.com/powerline/fonts)字型來解決,一部分原因也是因為 Windows 內建字型較少等寬 (monospace)。 無論是 App 或 VSC 來執行 WSL,字型都是從 Windows 安裝的來選擇,**安裝在 WSL 並沒有意義**。 這邊介紹一款好用的字體:[更紗黑體](https://github.com/be5invis/Sarasa-Gothic) <img src="https://i.imgur.com/40aYOM1.jpeg" width=100%> 由於這個字體更新的頻率比其他字體還要高,您也可以從 [Microsoft 商店](https://www.microsoft.com/store/productId/9MW0M424NCZ7)下載 在 Windows 終端機上面套用的方式也很簡單 1. 打開 Windows 終端機,在新分頁右邊的「下拉式按鈕」,選擇「設定」 <img src="https://i.imgur.com/5KIHNyN.png" width=100%> 2. 在右邊的設定檔選擇 Ubuntu-20.04,標籤切換到外觀,在字體的地方選擇「等距更紗黑體」 ![reference link](https://i.imgur.com/YpPalz7.png) ![reference link](https://i.imgur.com/NshQaZy.png) 3. 字體選擇完畢以後,文字上方可以即時顯示套用的字體 4. 在終端機設定做任何變更請記得在右下角按「儲存」 ### 2.4. 設定主題 先輸入以下指令開啟設定檔,由於我們初始設定選擇 0,所以會看到滿滿註解的 `.zshrc`,同時也可以看到 oh-my-zsh 的安裝路徑。 ``` $ vim ~/.zshrc ``` <img src="https://i.imgur.com/A8iVUIp.png" width=90%> > 使用 App 在 `.zshrc` 暫時沒有高亮,筆者是在 VSCode 下使用。 內建的 [Themes](https://github.com/robbyrussell/oh-my-zsh/wiki/Themes) 會位於 `~/.oh-my-zsh/themes` ,可以直接在上圖 `.zshrc` 中的 `ZSH_THEME` 替換想要的主題,並套用變更。 <img src="https://i.imgur.com/J6ycRQ1.png" width=100%> 其他需要下載的主題可以從[ awesome-zsh-plugins ](https://github.com/unixorn/awesome-zsh-plugins#themes)查看,目前筆者使用的是 [bullet-train](https://github.com/caiogondim/bullet-train.zsh),以下教學都可以類推適用。 先切換目錄到 `$ZSH_CUSTOM/themes` 底下,再使用 `wget` 下載**該主題的 raw 檔**。 ``` $ cd $ZSH_CUSTOM/themes $ wget https://raw.githubusercontent.com/caiogondim/bullet-train-oh-my-zsh-theme/master/bullet-train.zsh-theme ``` 確認下載至對應路徑後,回到 `~/.zshrc` 更改主題並套用,結果如下,雖然有受到 VSC 佈景影響,但整體效果仍相當不錯。 <img src="https://i.imgur.com/wcdeXZN.png" width=90%> 需要注意的是主題和套件都會影響 zsh 的**啟動速度**,請自行衡量。 啟用終端機的 256 色模式。 ``` $ echo 'export TERM="xterm-256color"' >> ~/.zshrc ``` 如果是在 VSC 裡執行 WSL,內部顏色會**被 VSC 的佈景顏色影響**,App 則不會有太大影響,以下是 [ 24 bits color ](https://github.com/gnachman/iTerm2/blob/master/tests/24-bit-color.sh)測試結果: <img src="https://i.imgur.com/MgluEAy.png" width=100%> <img src="https://i.imgur.com/xGXTCEw.png" width=100%> > 如果是像 [powerlevel9k](https://github.com/bhilburn/powerlevel9k) 這類 agnnoster 主題,尾端或是游標的顯示在 App 或 VSC 有可能跑掉,這必須自己測試主題了。 > 目前 VSC 的終端機顏色只能透過佈景主題去調整,詳見 [Color theme for VS Code](https://stackoverflow.com/questions/42307949/color-theme-for-vs-code-integrated-terminal)。 ### 2.5. 安裝套件 各種支援套件都可以在此找到:[plugins 列表](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins)、[plugins 分類](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins-Overview),內建套件會位於 `~/.oh-my-zsh/plugins`。 如同上面提到,[awesome-zsh-plugins](https://github.com/unixorn/awesome-zsh-plugins) 詳列許多 zsh 框架,以及各式套件與主題,想簡化安裝步驟的可以使用[ Antigen ](https://github.com/zsh-users/antigen)等套件管理工具。 #### 需要另外下載的套件: 安裝於 `$ZSH_CUSTOM/plugins` 底下。 * [zsh-autosuggestions](https://github.com/zsh-users/zsh-autosuggestions) ``` $ git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions ``` * [zsh-completions](https://github.com/zsh-users/zsh-completions) ``` $ git clone https://github.com/zsh-users/zsh-completions $ZSH_CUSTOM/plugins/zsh-completions ``` * [zsh-syntax-highlighting](https://github.com/zsh-users/zsh-syntax-highlighting) ``` $ git clone https://github.com/zsh-users/zsh-syntax-highlighting $ZSH_CUSTOM/plugins/zsh-syntax-highlighting ``` * [fast-syntax-highlighting](https://github.com/zdharma-continuum/fast-syntax-highlighting):優化上面套件 ``` $ git clone https://github.com/zdharma-continuum/fast-syntax-highlighting $ZSH_CUSTOM/plugins/fast-syntax-highlighting ``` * [autoupdate-zsh-plugin](https://github.com/TamCore/autoupdate-oh-my-zsh-plugins):自動更新位於 `$ZSH_CUSTOM` 的 git repos ``` $ git clone https://github.com/TamCore/autoupdate-oh-my-zsh-plugins $ZSH_CUSTOM/plugins/autoupdate ``` * [zsh-navigation-tools](https://github.com/psprint/zsh-navigation-tools):提供 UI 來篩選歷史紀錄、資料夾、刪除工作等等,以下指令會自動設定至 `.zshrc`,所以不用特別啟用,但務必確保設定在檔案最底端。 ``` $ sh -c "$(curl -fsSL https://gist.githubusercontent.com/jsdryan/1af3a7fab6b7497ae5a99640c501363f/raw/1628761c581fe90cc3d616985ddcdefbd5bce8c4/install.sh)" ``` | 指令 | 說明 | | --- | --- | | n-aliases | browses aliases, relegates editing to vared | | n-cd | browses dirstack and bookmarked directories, allows to enter selected directory | | n-functions | browses functions, relegates editing to zed or vared | | n-history | browses history, allows to edit and run commands from it | | n-kill | browses processes list, allows to send signal to selected process | | n-env | browses environment, relegates editing to vared | | n-options | browses options, allows to toggle their state | | n-panelize | loads output of given command into the list for browsing | > 可以不用加 `-`,`Ctrl`+`R` 可以快速使用 `nhistory`。 #### 內建好但需要關聯的: * [extract](https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/extract):`x` 等同 `extract`,只要加上要解壓縮的檔名,剩下就會自動處理,需要先安裝 unzip ``` $ sudo apt-get install unzip ``` * [fasd](https://github.com/clvv/fasd):提供一些快捷去搜尋和切換最近使用的檔案或目錄,提供互動式的選擇,指令也能互相搭配使用,需要先安裝 fasd **目前 18.04 無法使用 ppa** ``` $ cd ~ $ git clone https://github.com/clvv/fasd.git $ cd fasd $ sudo make install # 可能會需要安裝 make $ echo 'eval "$(fasd --init auto)"' >> ~/.zshrc ``` 除了直接呼叫 `fasd` 加上參數, 一般會使用內建的縮寫: | 指令 | 說明 | | --- | --- | | a | any | | s | show / search / select | | d | directory | | f | file | | sd | interactive directory selection | | sf | interactive file selection | | z | cd, same functionality as j in autojump | | zz | cd with interactive selection | * [debian](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#debian):提供一些管理 package 的快捷,需要先安裝 aptitude ``` $ sudo apt-get install aptitude ``` | 指令 | 說明 | | --- | --- | | as [search term] | Search for packages | | ad | Update packages | | adg | Updgrade packages | | ai [packages to install] | Install package(s) | | ar [package to remove] | Remove package | | app | Apt policy | | acs [search term] | Search packages in cache | | ac | Clean apt cache | | age [parameters] | apt-get [parameters] | | api [parameters] | aptitude [parameters] | | aps [search term] | Search with aptitude | | afs [search term] | Search files in packages | | asrc [parameters] | apt-get source [parameters] | | aac | Autoclean | | abd | Build dependencies | | adu | Update & (safe)upgrade | | afu | Update & full-upgrade | | ag | Upgrade (safe) | | ail [list of packages] | Install all packages in list | | ap | Purge | | ads | Dselect upgrade | | dia | Install all ./*.deb | | di [glob] | Install all glob | | kclean | Remove unused kernel images | | allpkgs | Print all installed packages | | mydeb | Create .deb package | | apt-list-packages | List packages by size | | kerndeb | Kernel-package building shortcut | | apt-copy | Creates script to install all current packages on other machine | | apt-history | Prints apt history | #### 可以直接啟用的: * [last-working-dir](https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/last-working-dir):每次開啟終端機會自動回到上次路徑 * [colored-man-pages](https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/colored-man-pages):改善 manual 的可讀性 * [command-not-found](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#command-not-found):找不到 command 時會提供相關建議 * [common-aliases](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#common-aliases):提供一些常用指令的快捷,主要有各式 `ls` 的搭配 * [sudo](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#sudo):按 `Esc` 兩次,可以直接在 command 前加上 `sudo` #### 最後步驟: 在 `~/.zshrc` 這地方依序打上需要啟用的套件 (資料夾名稱) 並套用變更。 ``` plugins=( git zsh-autosuggestions zsh-completions zsh-syntax-highlighting fast-syntax-highlighting autoupdate extract fasd debian last-working-dir colored-man-pages command-not-found common-aliases sudo ) ``` > 可以發現 plugins 後面才 `source oh-my-zsh.sh`。 ``` source ~/.zshrc ``` ### 2.6. 注意事項 * 在 1703 版後,用戶的 Windows 環境變數會自動加到 WSL 裡,所以可以很容易地透過 WSL 開啟 Winodws 的程式,所以在 WSL 執行 `code` 會直接開啟 VSC。 <img src="https://i.imgur.com/aB8MH4X.png" width=100%> * 1805 新版的 oh-my-zsh,啟用 completion 相關的套件可能顯示如下警告,詳見 [Folder permission issue](https://github.com/robbyrussell/oh-my-zsh/issues/6835#issuecomment-390216875)。 <img src="https://i.imgur.com/WIEqv5o.png" width=100%> 請直接在 `~/.zshrc` 前面加上下面指令,務必要**在載入 oh-my-zsh 之前**。 ``` ZSH_DISABLE_COMPFIX=true ``` * 更新 oh-my-zsh: ``` $ upgrade_oh_my_zsh ``` * 刪除 oh-my-zsh: ``` $ uninstall_oh_my_zsh ``` * `~/.profile` 和 `~/.bashrc` 由於我們已更改預設 shell,所以不會被 zsh 載入,一般前者為 **login** 載入,後者為 **non-login** 載入。 * `zsh` 有專屬的 `~/.zprofile` 和 `~/.zshrc`,一般只會修改後者。 ## 3. 加碼說明 ### 1. 非正規備份還原 在預設的情況下,WSL2 的虛擬磁碟檔案(ext4.vhdx)會放在 C 槽的位置。但有時候我們可能有一些原因(例如:C 槽容量不足、系統常常重灌、想隨身攜帶)會想要放到其他地方而官方預設又不讓你轉移存放,這邊找到一個變通的辦法可以讓您暢行無阻 #### 1-1. 備份 1. 找到原本 WSL2 放在 C 槽的地方 預設位置: ``` C:\Users\[使用者名稱]\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState) ``` 2. 將 ext4.vhdx 複製出來放到你自己知道的地方 #### 1-2. 還原 關於還原的部分,由於同一個名稱只認一個安裝位置, 假如你新的電腦已經安裝了 WSL2 的 Ubuntu-20.04 你就必須要先將其解除安裝或是重新命名,否則接下來的還原會發生失敗,接下來就以新的電腦有安裝過 Ubuntu-20.04 做示範 1. 開啟終端機,先解除系統已註冊的 Ubuntu-20.04 ``` wsl --unregister Ubuntu-20.04 ``` 2. [手動下載 Ubuntu-20.04](https://aka.ms/wslubuntu2004),其他的平台 [您也可以從微軟提供的網站下載](https://docs.microsoft.com/zh-tw/windows/wsl/install-manual) 3. 將下載回來的 Appx 檔案使用壓縮軟體進行解壓縮 4. 執行 Ubuntu-20.04.exe,等安裝完成出現要您輸入使用者名稱之後將視窗關閉 (這麼做的目的是為了讓系統記住您 Ubuntu-20.04 的安裝位置) :::warning 在 Ubuntu-20.04 尚未註冊到系統之前,不要提前將您自己的 ext4.vhdx 丟到解壓縮的資料夾內 否則會還原會失敗 ::: 5. 執行 WSL 強制關閉 (不這麼做在稍後的還原會因為系統依然佔用著檔案而發生失敗) ``` wsl --shutdown ``` 6. 將您的 ext4.vhdx 放到 Ubuntu-20.04.exe 同一個資料夾裡面,覆蓋原本產生的 ext4.vhdx 7. 重新執行 Ubuntu-20.04.exe,就會發現開起來就是您原本備份的資料了 8. Enjoy~ ###### 備註: 如果有遇到還原之後預設登入帳號為 root ,而非使用者帳號時,可參考以下解決辦法 root 帳號下執行此指令 ``` # id -u <username> ``` (理論上應該回傳 1000) 修改登錄檔 ``` HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\{MY-UUID} ``` 修改 `DefaultUid` 登錄檔的十進制為 1000 PowerShell: ``` PS > wsl —shutdown PS > wsl ``` 這時登入帳號應該就會是 username 了! [解決辦法原文](https://github.com/microsoft/WSL/issues/4276) ### 2. 將 WSL 的介面變成中文顯示 原本早期的版本 Ubuntu 的指令介面還是具備中文顯示的,但後期到現在的版本全部改成英文 這邊提供各位還原的辦法 1. 安裝繁體中文套件包 ``` sudo apt install language-pack-zh-hant ``` 2. 重新套件設定語言 ``` sudo dpkg-reconfigure locales ``` 3. 用鍵盤 Page Down 的方式按到最下面,按空白鍵選擇 `zh-TW.UTF-8`,然後按 `Tab` 鍵切換到 OK,然後按 `Enter` 下一步 4. 語言選擇 `zh-TW.UTF-8`,然後按 `Enter` 下一步 5. 關閉 WSL 的視窗,然後重新開啟 WSL 6. 用 `locale` 進行確認,語系會全部變成 `zh-TW.UTF-8` ### 3. 將 WSL Ubuntu 20.04 LTS 升級至 Ubuntu 21.10 Microsoft 提供的 WSL Ubuntu 發行版為 LTS 的分支。 為因應部分特殊需求,Microsoft 仍然保留修改發行版更新通道的設定檔。 這邊提供升級的方法: 1. 完成更新目前所有尚未更新的套件 ``` sudo apt update && sudo apt-get full-upgrade -y ``` 2. 修改更新通道 ``` sudo vim /etc/update-manager/release-upgrades ``` 將 `Prompt=lts` 更改成 `Prompt=normal` <img src="https://i.imgur.com/cVqyUb8.png" width=100%> 3. 更新至 21.04 >> 21.10 ``` sudo do-release-upgrade ``` <img src="https://i.imgur.com/iFH0ZMN.png" width=100%> ### 4. 安裝多個 WSL 同版本個體 基本上,你一次只能從 Microsoft Store 或是手動安裝一種 WSL Distro 版本,如果想準備多個不同的開發環境,但都使用同一個 WSL Distro 怎麼辦呢? 其實很簡單,你先找到一個執行個體,並且把基本設定都搞定,當成 WSL Distro 範本,接著先匯出,再匯入多個版本即可! 底下這個例子(PowerShell),就是將現有的執行個體先匯出,然後匯入成兩個不同的 WSL Distro 名稱 (指定使用 WSL 2 版本): ``` PS > cd $env:USERPROFILE PS > wsl --export Ubuntu-21.10 Ubuntu-2110_backup.tar PS > wsl --import Ubuntu-21.10 K:\WSL2\Ubuntu21.10 Ubuntu-2110_backup.tar --version 2 PS > wsl --import Ubuntu-21.10-Dev K:\WSL2\Ubuntu21.10-Dev Ubuntu-2110_backup.tar --version 2 ``` 如果要指定啟動特定 WSL Distro 的話,只要加上 -d <Distro> 參數即可: `wsl -d Ubuntu-21.10-Dev` 如果要指定關閉特定 WSL Distro 的話,只要加上 -t <Distro> 參數即可: `wsl -t Ubuntu-21.10-Dev` 處理辦法原文:[使用 WSL 2 打造優質的多重 Linux 開發環境](https://blog.miniasp.com/post/2020/07/26/Multiple-Linux-Dev-Environment-build-on-WSL-2) 參考資料: [鳥哥](http://linux.vbird.org/linux_basic/0320bash.php#settings_bashrc) [Zsh not hitting ~/.profile](https://superuser.com/questions/187639/zsh-not-hitting-profile) [Ubuntu 如何快速替換軟體來源的鏡像站(Mirror)?](https://magiclen.org/ubuntu-exchange-source/)