天天看點

Installing Linux Using Windows Subsystem for Linux(WSL)

因為現在拿到的電腦OS版本比較低,隻能支援wsl1,是以該文章隻針對wsl1進行配置和使用。(ps:wsl1沒有完整的linux核心,是以很多功能不支援,在這裡我隻針對我遇到的一些問題提供一些解決方法。也歡迎大家有更好的解決方法可以和我一起分享)

啟用Windows的linux支援項:

打開 控制台-> 程式 ->啟用/關閉Windows功能

Installing Linux Using Windows Subsystem for Linux(WSL)

勾選Windows Subsystem for Linux 和 Virtual Machine Platform

Installing Linux Using Windows Subsystem for Linux(WSL)

然後等待配置完成,重新開機電腦。

下載下傳linux 軟體

打開 Windows 應用商店->輸入linux -> 下載下傳linux 18.08 LTS

Installing Linux Using Windows Subsystem for Linux(WSL)

等下載下傳完成,啟動繼續完成後續安裝。之後會要求你設定一個使用者名和密碼,這個是指登入Linux系統的使用者名和密碼,跟windows系統的使用者無關,自己設定一個就可以。

Installing Linux Using Windows Subsystem for Linux(WSL)

然後我們在shell裡輸入

lsb_release -a

檢視Linux系統資訊。

Installing Linux Using Windows Subsystem for Linux(WSL)

到這裡一個純指令行的Linux環境就好了。現在我們來鼓搗圖形界面。

安裝linux圖形界面xfce4

sudo -i
apt install xfce4 tightvncserver -y// this step to install GUI

#if meet error log:
#dpkg: unrecoverable fatal error, aborting:
#unable to truncate for updated status of 'firefox': Invalid argument
#E: Sub-process /usr/bin/dpkg returned an error code (2)
 
#then run :
dpkg --configure -a
apt --fix-broken install
apt update
apt upgrade -y
           

在Windows上啟動并通路桌面的兩種方式

1.通過xrdp和遠端桌面

apt install xrdp -y
echo "xfce4-session" >~/.xsession
 
vim /etc/xrdp/xrdp.ini // port:3389 -> 3391 (avoid conflict with other service)
service xrdp restart
           

啟動xrdp後,我們可以看到:

Installing Linux Using Windows Subsystem for Linux(WSL)

現在可以用遠端桌面連結linux的GUI了

Installing Linux Using Windows Subsystem for Linux(WSL)
Installing Linux Using Windows Subsystem for Linux(WSL)

2.通過vcxsrv來建構桌面

首先下載下傳vcxsrv windows X server,等安裝完成我們會在桌面看到X-Launch圖示,以預設設定啟動,現在我們來設定linux圖形界面啟動設定。

> echo 'export DISPLAY=:0.0' >> ~/.bashrc
> source ~/.bashrc
> startxfce4
           

然後我們就能看到桌面啟動了。這種方式建構的圖形桌面,桌面視窗和應用視窗分離,我們可以隻保留需要的應用視窗就可以了。桌面可以關掉,我覺得還挺友善的。

在WSL1中安裝并使用Docker

安裝Docker

1.通過指令行安裝(也可以直接根據官方文檔來執行)

sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
           

2.通過.deb包安裝Docker

首先下載下傳包,總共需要下載下傳三個,可以在https://download.docker.com/linux/ubuntu/dists/bionic/pool/stable/amd64/上選擇最新版:

https://download.docker.com/linux/ubuntu/dists/bionic/pool/stable/amd64/containerd.io_1.2.0-1_amd64.deb

https://download.docker.com/linux/ubuntu/dists/bionic/pool/stable/amd64/docker-ce-cli_18.09.03-0ubuntu-bionic_amd64.deb

https://download.docker.com/linux/ubuntu/dists/bionic/pool/stable/amd64/docker-ce_18.09.03-0ubuntu-bionic_amd64.deb

然後按順序安裝:

按順序安裝三個包,注意順序不能錯:

sudo dpkg -i docker-ce-cli_18.09.0~3-0~ubuntu-bionic_amd64.deb
sudo dpkg -i containerd.io_1.2.0-1_amd64.deb
sudo dpkg -i docker-ce_18.09.0~3-0~ubuntu-bionic_amd64.de
           

然後

docker -v

能檢視dockers的版本,但這時你會發現,如果要用

docker pull

的時候,就會報錯:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?. See ‘docker run --help’.

(ps: 基本上是其他的docker指令都會出這個問題,除了檢視版本。)

此時,你可能會嘗試通過執行systemctl start docker指令來啟動Docker服務,因為錯誤資訊告訴我們,Docker的守護程序沒有啟動,可你會發現這樣依然報錯。可是為什麼呢?明明Docker都在WSL裡安裝成功了啊,事實上除了docker -v不需要依賴守護程序,其餘的指令都需要依賴守護程序,而WSL恰恰是不支援docker-engine的,是以,一種曲線救國的思路就是,讓WSL去連接配接主控端上的docker engine。是以確定windows系統開啟Hyper-V,然後安裝Docker for Windows,并打開對主控端Docker的監聽.

接下來,我們給WSL中的Docker設定主控端的位址,在終端中輸入下列指令即可:

echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc
           

這樣我們就可以正常使用docker了。

如果你在使用Docker的過程中,需要處理分區挂載相關的東西,一個比較好的建議是修改WSL的配置檔案(如果不存在需要自行建立):

sudo nano /etc/wsl.conf
[automount]
root = /
options = "metadata"
           

cannot read realtime clock: Invalid argument

如果你在啟動圖形界面的時候遇到這個錯誤,那可能是内部系統時鐘出了問題。

要修複這個問題,可以通過下面的指令來實作:

wget https://launchpad.net/~rafaeldtinoco/+archive/ubuntu/lp1871129/+files/libc6_2.31-0ubuntu8+lp1871129~1_amd64.deb
dpkg --install libc6_2.31-0ubuntu8+lp1871129~1_amd64.deb
apt-mark hold libc6
apt --fix-broken install
apt full-upgrade
           

執行之後,再啟動圖形界面就不會出現這個問題了。

如果還有遇到其他問題,以後再更新~

繼續閱讀