1. 程式人生 > 其它 >我應該是最後一個知道 WSL 的吧!

我應該是最後一個知道 WSL 的吧!

前言

前段時間,我買了塊固態硬碟給我的膝上型電腦裝上(因為它太慢了,影響我遊戲學習)。同時也重灌了系統,以前一直在用 Win8.1,在上面構建了我的開發環境:在 Win8.1 上安裝了 VMvare,在 VMware 上安裝了 Centos,在 Centos 上安裝了 Docker,然後在 Win8.1 上通過 Xshell 來連線虛擬機器 Centos 控制 Docker 進行開發學習。

這次安裝的是 Win10,真相,win10 支援了 WSL,直接可以在 Win10 上安裝 Docker、Linux系統等,因此重新搭建了我的開發環境,首先把 Git 配置好。

什麼是 WSL

WSL 是 Windows Subsystem for Linux 的縮寫,意思是 linux 版的 window 子系統。

引用自:微軟官網 https://docs.microsoft.com/zh-cn/windows/wsl/about
The Windows Subsystem for Linux lets developers run a GNU/Linux environment -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the overhead of a virtual machine.
You can:

  • Choose your favorite GNU/Linux distributions from the Microsoft Store.
  • Run common command-line tools such as grep, sed, awk, or other ELF-64 binaries.
  • Run Bash shell scripts and GNU/Linux command-line applications including:
  • Install additional software using own GNU/Linux distribution package manager.
  • Invoke Windows applications using a Unix-like command-line shell.
  • Invoke GNU/Linux applications on Windows.

簡單的說就是,Linux 的 Windows 子系統讓開發人員無需虛擬機器就可以直接在 Windows 上執行 Linux 環境,包括大多數命令列工具、程式和應用。
使用 WSL 的好處是:

  1. 與在虛擬機器下使用 Linux 相比,WSL 佔用資源更少,更加流暢;
  2. WSL 可以對 Windows 檔案系統下的檔案直接進行讀寫,檔案傳輸更方便;
  3. 剪貼簿互通,可以直接在 Windows 下其它地方複製文字內容,貼上到 WSL;

WSL 安裝 Ubuntu

  1. 通過Microsoft Store下載,自動安裝

  2. 自己去官網下載,防止 C 盤容量太大
    官網

    下載後複製到D盤,將字尾名改成.zip,然後解壓,執行裡面的exe檔案,進行安裝

安裝後在powershell或者windows terminal(Microsoft Store上的另一個寶藏應用,直接去搜掃安裝就行了)
執行命令wsl -l -v 列出所有子系統

PS C:\Users\Administrator> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2

執行命令wslconfig /setdefault Ubuntu-20.04Ubunru-20.04設為預設子系統

PS C:\Users\Administrator> wslconfig /setdefault Ubuntu-20.04

執行命令wsl進入子系統

PS C:\Users\Administrator> wsl
zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ neofetch

配置 zsh

檢視一下

zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen

安裝 zsh

zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ sudo apt-get install zsh
zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen
/bin/zsh
/usr/bin/zsh

將 zsh 設為預設 shell

zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ sudo chsh -s /bin/zsh 

推出再重新進入

zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ exit
logout
PS C:\Users\Administrator> wsl
➜  Administrator neofetch

配置 Git

  1. 安裝 git
➜  Administrator sudo apt-get install git
  1. 配置你的使用者名稱
➜  Administrator git config --global user.name xxx
  1. 配置你的郵箱地址
➜  Administrator git config --global user.email  [email protected]
  1. 生產SSH金鑰
➜  Administrator ssh-keygen -t rsa -C "[email protected]"
➜  Administrator cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC12sKF6l8CKsYjrWCruS3fqPhSHExo6pcvzCRUYMotqY7KJvhIG45xGALedXn8ow8+jBfC6r9CpO6mzCpf18WsGf7omBvoQJa3rQeuO1KvZ7Ctd/EH+jJndLoFisH76vY4gm2OR0UU= [email protected]
  1. 複製公鑰的內容並且開啟你的 github 設定介面,找到 SSHkey 設定,點選右上角的New SSH key

6.測試

➜  Administrator ssh [email protected]
PTY allocation request failed on channel 0
Hi Zioyi! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

總結

本篇對 WSL 進行了簡單介紹,嘗試基於 WSL 安裝了 Ubuntu 系統,並在系統中安裝 zsh 和 git 工具,打造新時代的 Windows 開發環境。
相比於原來 Win8 的開發環境,這套真的好很多,在便捷性、資源使用、效能上都有很大的提升。