WSL預設安裝目錄

sikewang發表於2024-07-22

我當前在win11下進行以下操作,其它系統版本有問題可以留言

一、安裝WSL

  1. 前提條件:我們需要保證你的作業系統版本滿足 **Windows 10 版本 2004 及更高版本(內部版本 19041 及更高版本)或 Windows 11 ** 才能使用以下命令。
  2. 啟用適用於 Linux 的 Windows 子系統:開啟powershell並輸入
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  3. 啟用虛擬化:以管理員開啟powershell輸入下列命令:
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  4. 【可選/推薦】設定WSL預設版本為wsl2:wsl --set-default-version 2(WSL2.0相比WSL1.0具備完整的Linux核心、託管VM和完全的系統呼叫相容性,所以我們這裡使用WSL2.0)
  5. 直接安裝/指定核心版本安裝
    【直接安裝】WSL的安裝很簡單,可以參考官方手冊:
    可以使用單個命令安裝執行 WSL 所需的一切內容。 在管理員模式下開啟 PowerShell 或 Windows 命令提示符,方法是右鍵單擊並選擇“以管理員身份執行”,輸入wsl --install命令,然後重啟計算機。
    【指定核心安裝】
    當然,可以選擇指定核心的方式來安裝wsl:
  • 若要更改安裝的發行版,請輸入:wsl --install -d <Distribution Name>。 將 <Distribution Name>替換為要安裝的發行版的名稱。
  • 若要檢視可透過線上商店下載的可用 Linux 發行版列表,請輸入:wsl --list --onlinewsl -l -o
  • 若要在初始安裝後安裝其他 Linux 發行版,還可使用命令:wsl --install -d <Distribution Name>

二、WSL修改預設安裝目錄到其他盤

顯然,此時的wsl預設安裝在c盤,隨著系統的使用,會佔用我們C盤的空間,所以我們將其打包放到其它盤去。
1.檢視WSL發行版本
在Windows PowerShell中輸入命令:
wsl -l --all -v
2.匯出分發版為tar檔案到d盤
wsl --export Ubuntu-20.04 d:\wsl-ubuntu20.04.tar(Ubuntu-20.04修改成你現在的發行版名稱)
3.登出當前分發版
wsl --unregister Ubuntu-20.04(Ubuntu-20.04修改成你現在的發行版名稱)
4.重新匯入並安裝WSL在d:\wsl-ubuntu20.04(可以修改成你自己想要的目錄
wsl --import Ubuntu-20.04 d:\wsl-ubuntu20.04 d:\wsl-ubuntu20.04.tar --version 2
5.設定預設登陸使用者為安裝時使用者名稱
ubuntu2004 config --default-user Username
6.刪除tar檔案(可選)
del d:\wsl-ubuntu20.04.tar

經過以上操作後,就將WSL的預設安裝目錄遷移到D:\wsl-ubuntu20.04目錄(可以自己修改自己想要的目錄)下了。此目錄即為WSL的跟檔案系統。

位置在:

C:\Users\THINK\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs

備註:THINK是我的Windows登入使用者名稱,你換成自己的使用者名稱即可,知道了這個資料夾之後就可以方便地進行subsystem的檔案管理了,寫好後的程式也可以轉移到自己的windows系統下,避免了程式丟失的煩惱。

一、WSL簡介

WSL的全稱是Windows Subsystem for Linux。
適用於 Linux 的 Windows 子系統可讓開發人員按原樣執行 GNU/Linux 環境 - 包括大多數命令列工具、實用工具和應用程式 - 且不會產生傳統虛擬機器或雙啟動設定開銷。

二、開啟WSL功能

系統要求:win10/11 專業版,(家庭版無法使用)
開啟控制皮膚 -> 程式 -> 啟用或關閉windows功能
將“適用於Linux的windows子系統”勾選
後續您的windows系統會進行更新,重啟後將會開啟WSL功能。
預設開啟的WSL版本是1.目前最新的版本是2.(但是之後可能會報錯還是要版本2.0的)


三、安裝kali子系統

進入微軟商店搜尋kali,選擇kali進行安裝、沒有Windows Terminal也要裝
安裝完成後,在開始頁面可以看到Kali Linux。
首次完成會要求您完成的第一步是建立賬戶,包括使用者名稱和密碼。(但是不是很順利會報錯)

報錯的解決辦法在下面👇

此使用者名稱和密碼特定於安裝的每個單獨的 Linux 分發版,與 Windows 使用者名稱無關。
建立使用者名稱和密碼後,該帳戶將是分發版的預設使用者,並將在啟動時自動登入。
此帳戶將被視為 Linux 管理員,能夠執行 sudo (Super User Do) 管理命令。
在適用於 Linux 的 Windows 子系統上執行的每個 Linux 分發版都有其自身的 Linux 使用者帳戶和密碼。 每當新增分發版、重新安裝或重置時,都必須配置一個 Linux 使用者帳戶。
此時雖然為管理員賬戶,可以使用sudo命令,但是不能使用root進行操作。
使用sudo passwd root命令,為root修改密碼。

報錯1:8007019e

解決問題:WslRegisterDistribution failed with error: 0x8007019e

然後開啟安裝好的Windows Linux子系統(WSL) 此時一般來說會遇到下面的問題:

 
  1. Installing, this may take a few minutes...
  2. WslRegisterDistribution failed with error: 0x8007019e
  3. The Windows Subsystem for Linux optional component is not enabled. Please enable it and try again.
  4. See https://aka.ms/wslinstall for details.
  5. Press any key to continue...
 

出現這個錯誤的原因是沒有安裝Windows子系統支援(如果前面開啟那個功能過後就重啟就不會出現這個問題)。

解決辦法: 開啟Windows PowerShell(Admin) 輸入: Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux 回車執行 重啟

報錯2:800701bc

問題解決WslRegisterDistribution failed with error: 0x800701bc

造成該問題的原因是WSL版本由原來的WSL1升級到WSL2後,核心沒有升級,前往微軟WSL官網下載安裝適用於 x64 計算機的最新 WSL2 Linux 核心更新包即可。

下載並安裝WSL2更新包

WSL2 Linux kernel update package for x64 machines​wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

將WSL2設定成預設

wsl --set-default-version 2

但是輸入wsl --set-default-version 2失敗,報錯“請啟用虛擬機器平臺 windows 功能並確保在 bios 中啟用虛擬化"

解決辦法:執行這個指令要以管理員身份執行

bcdedit /set hypervisorlaunchtype auto

這個指令代表的意思是:設定Hyper-V虛擬化啟動型別為自動。

報錯3:80370102

問題解決WslRegisterDistribution failed with error: 0x80370102

 
  1. Installing, this may take a few minutes...
  2. WslRegisterDistribution failed with error: 0x80370102
  3. Please enable the Virtual Machine Platform Windows feature and ensure virtualization is enabled in the BIOS.
  4. For information please visit https://aka.ms/enablevirtualization
  5. Press any key to continue..
 

這個只要前面開啟那個虛擬化平臺也不會報錯

開啟虛擬機器特性

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

報錯4:80004005

安裝報錯 WslRegisterDistribution failed with error: 0x80004005

我是從MS Store 安裝的,下載後點選開啟,彈出cmd視窗報錯資訊:

 
  1. Installing, this may take a few minutes...
  2. WslRegisterDistribution failed with error: 0x80004005
  3. Error: 0x80004005 ???????
  4. Press any key to continue...
 

解決方法:
將 LxssManager 服務修改為自動啟動
從服務中修改會提示“拒絕訪問”,所以從登錄檔改
win + R -> regedit -> \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LxssManager ->

右鍵 Start 項 -> 將值修改為 2(預設為 3)

 
  1. 1 自動(延遲啟動)
  2. 2 自動
  3. 3 手動
  4. 4 禁用
 

更新wsl
管理員啟動 powershell:wsl --update

成功安裝
再次點選開啟

 
  1. Installing, this may take a few minutes...
  2. Please create a default UNIX user account. The username does not need to match your Windows username.
  3. For more information visit: https://aka.ms/wslusers
  4. Enter new UNIX username:
 

報錯5:80070005

WslRegisterDistribution failed with error: 0x80070005

解決方法
  1. 解除安裝:
  • Hyper-V
  • 虛擬機器管理程式平臺
  • 容器
  • WSL的
  1. 重新啟動
  2. 重新安裝所有 4 個功能
  3. 重新啟動
  4. 再次呼叫 API。現在它起作用了。

如何刪除它?我在 Microsoft 商店中沒有解除安裝按鈕。

第一個,在“設定”中選中“應用程式”並找到它或在搜尋欄中寫入Windows子系統,然後解除安裝

第二個,在“設定”中選中“應用程式”並找到它或在搜尋欄中寫入kali,然後解除安裝

重新再來一遍就行...

修改預設安裝位置

一般來說都會預設按在 C 盤 ,所以我們可以使用下面的方法進行修改預設安裝位置,同時我們應該也可以使用這個方法進行一個分支儲存

 
  1. # 1. 檢視所有安裝的版本
  2. PS C:\Users\14894> wsl -l --all -v
  3. NAME STATE VERSION
  4. * kali-linux Running 2
  5. # 2. 匯出分發版為 tar檔案到 D盤
  6. PS C:\Users\14894> wsl --export kali-linux D:\wsl-kali-linux.tar
  7. # 3. 登出當前分發版
  8. PS C:\Users\14894> wsl --unregister kali-linux
  9. # 4. 重新匯入並安裝WSL在D盤
  10. PS C:\Users\14894> wsl --import kali-linux D:\kali-linux D:\wsl-kali-linux.tar --version 2
  11. # 5. 設定預設登陸使用者為安裝時使用者名稱
  12. PS C:\Users\14894> kali-linux config --default-user USERNAME
  13. # 6. 刪除 wsl-kali-linux.tar
  14. PS C:\Users\14894> del D:\wsl-kali-linux.tar
 

更新源和升級軟體

su vim /etc/apt/sources.list

# 官方源
# deb http://http.kali.org/kali kali-rolling main non-free contrib
# deb-src http://http.kali.org/kali kali-rolling main non-free contrib
#根據需要自己選一個,中科大的還可以
#中科大
deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
#阿里雲
#deb http://mirrors.aliyun.com/kali kali-rolling main non-free contrib
#deb-src http://mirrors.aliyun.com/kali kali-rolling main non-free contrib

安裝完整版Kali

執行:apt-get install kali-linux-everything
即使更換了更新源也需要經過漫長的等待,全部的安裝內容大約在19G左右。根據C盤容量,量力而行。

0 upgraded, 3891 newly installed, 0 to remove and 0 not upgraded.
Need to get 10.8 GB of archives.
After this operation, 30.8 GB of additional disk space will be used.
Do you want to continue? [Y/n]

0個已升級,3891個新安裝,0個要移除,0個未升級。
需要獲得10.8 GB的檔案。
執行此操作後,將使用30.8 GB的額外磁碟空間。
你想繼續嗎?[是/否]

kali-linux-everything 安裝kali提供的所有工具

kali-linux-large 安裝kali提供的預設工具

kali-linux-top10 安裝最流行的10個工具

"docker-credential-desktop.exe": executable file not found in $PATH 錯誤解決

  • 1. 錯誤資訊和解決方法

1. 錯誤資訊和解決方法

錯誤資訊,

error getting credentials - err: exec: "docker-credential-desktop.exe": executable file not found in $PATH, out: ``
 

解決方法,

Delete the line with credsStore from ~/.docker/config.json.

Or rename credsStore to credStore

完結!

WSL2 預設安裝在 C 盤下,系統盤空間有限,推薦遷移安裝目錄。

1. 預設安裝的 WSL2 目錄

C:\Users\cheng\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx

在這裡插入圖片描述

2. 將安裝的 WSL2 遷移至指定目錄

  1. wsl --list --verbose - wsl -l -v

檢視安裝在 Windows 計算機上的 Linux 發行版列表,其中包括狀態和執行發行版的 WSL 版本 (WSL 1 或 WSL 2)。

  1. wsl --shutdown

立即終止所有正在執行的發行版和 WSL 2 輕量級實用工具虛擬機器。

Microsoft Windows [版本 10.0.22621.1265]
(c) Microsoft Corporation。保留所有權利。

C:\Users\cheng>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2

C:\Users\cheng>
C:\Users\cheng>wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2

C:\Users\cheng>
C:\Users\cheng>wsl --shutdown

C:\Users\cheng>
C:\Users\cheng>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-20.04    Stopped         2

C:\Users\cheng>
  1. wsl --export Ubuntu-20.04 f:\ubuntu20.04.tar

將指定 tar 檔案匯出為新的發行版。--export 選項後面是檢視執行狀態中 Linux 系統的名字。

C:\Users\cheng>wsl --export Ubuntu-20.04 f:\ubuntu2004.tar

C:\Users\cheng>

在這裡插入圖片描述

  1. Unregister or uninstall a Linux distribution (登出或解除安裝 Linux 發行版)

While Linux distributions can be installed through the Microsoft Store, they can’t be uninstalled through the store.
儘管可以透過 Microsoft Store 安裝 Linux 發行版,但無法透過 Store 將其解除安裝。

To unregister and uninstall a WSL distribution

wsl --unregister <DistributionName>

Replacing <DistributionName> with the name of your targeted Linux distribution will unregister that distribution from WSL so it can be reinstalled or cleaned up. Caution: Once unregistered, all data, settings, and software associated with that distribution will be permanently lost. Reinstalling from the store will install a clean copy of the distribution. For example, wsl --unregister Ubuntu would remove Ubuntu from the distributions available in WSL. Running wsl --list will reveal that it is no longer listed.
如果將 <DistributionName> 替換為目標 Linux 發行版的名稱,則將從 WSL 取消註冊該發行版,以便可以重新安裝或清理它。警告:取消註冊後,與該分發版關聯的所有資料、設定和軟體將永久丟失。從 Store 重新安裝會安裝分發版的乾淨副本。 例如:wsl --unregister Ubuntu 將從可用於 WSL 的發行版中刪除 Ubuntu。執行 wsl --list 將會顯示它不再列出。

You can also uninstall the Linux distribution app on your Windows machine just like any other store application. To reinstall, find the distribution in the Microsoft Store and select Launch.
還可以像解除安裝任何其他應用商店應用程式一樣解除安裝 Windows 計算機上的 Linux 發行版應用。若要重新安裝,請在 Microsoft Store 中找到該發行版,然後選擇 Launch

C:\Users\cheng>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-20.04    Stopped         2

C:\Users\cheng>
C:\Users\cheng>wsl --unregister Ubuntu-20.04
正在登出...

C:\Users\cheng>
C:\Users\cheng>wsl --list --verbose
適用於 Linux 的 Windows 子系統沒有已安裝的分發版。
可以透過訪問 Microsoft Store 來安裝分發版:
https://aka.ms/wslstore

C:\Users\cheng>
  1. wsl --import Ubuntu-20.04 f:\ubuntu_20_04 f:\ubuntu2004.tar

將指定 tar 檔案匯入為新的發行版。

wsl --import <Distribution Name> <InstallLocation> <FileName>

C:\Users\cheng>wsl --import Ubuntu-20.04 f:\ubuntu_20_04 f:\ubuntu2004.tar

C:\Users\cheng>

遷移成功之後,可在安裝目錄下檢視 vhdx 檔案。
F:\ubuntu_20_04\ext4.vhdx
在這裡插入圖片描述

  1. ubuntu2004.exe config --default-user yongqiang

設定預設登陸使用者為安裝時的使用者名稱。

C:\Users\cheng>ubuntu2004.exe config --default-user yongqiang

C:\Users\cheng>
  1. 刪除 f:\ubuntu2004.tar 檔案
C:\Users\cheng>del f:\ubuntu2004.tar

C:\Users\cheng>

3. 透過終端開啟 Ubuntu-20.04

  1. 在 Windows 圖示 [開始] 處點選滑鼠右鍵

在這裡插入圖片描述

  1. 終端 / 終端 (管理員)

在這裡插入圖片描述

References

https://yongqiang.blog.csdn.net/

摘抄自網路,便於檢索查詢。

相關文章