WSL與Windows互動實踐

來源:互聯網
上載者:User

標籤:register   etcd   執行檔案   linu   /usr   port   同事   linux環境   lse   

  • 1. WSL是什麼
  • 2. WSL新特性
  • 3. WSL管理配置
  • 4. WSL互動
  • 5. 解決方案
    ?* 5.1 使用別名
    ?* 5.2 多複製一份
    ?* 5.3 重新導向
    ?* 5.4 symlink
  • 6. 其他
    ?* 6.1 閑聊
    ?* 6.2 參考
1. WSL是什麼

? WSL 是Windows Subsystem for Linux 的簡稱,主要是為了在Windows 10上原生運行Linux二進位可執行檔(ELF格式),而提供的相容層。 通俗來講是在Windows10 嵌入了個Linux子系統(預設是ubuntu),方便運行大部分 Linux 命令及軟體,比如grep MySQL Apache。這很大方便了使用Windows做開發的同學,不需要雙系統或虛擬機器了。

在Windows功能中啟用```適用於Linux的Windows子系統```,然後在Windows CMD中直接輸入```bash```,即可進入Linux環境,執行命令:

2. WSL新特性

從Windows10 1709版本時開始,可以直接輸入wsl進入互動環境, bash方式會逐漸廢棄掉。

以前的 bash -c [command]直接用 wsl [command]來替代。

另一個特性是:Windows 10商店裡,可以下載安裝其他Linux發行版。這樣就可以自由選擇,不用限制到Ubuntu。

然後可以在程式列表中直接開啟Ubuntu進入,或在CMD或Powershell中直接輸入ubuntu進入:

PS D:\> ubuntu[email protected] ~ % lsgo  mush  test[email protected] ~ % pwd/home/mush[email protected] ~ %

後面都基於wslUbuntupowershell來介紹和示範。

3. WSL管理配置

Windows10內建了wslconfig,去管理多個安裝的發行版,比如卸載某個發行版,設定預設啟動的髮型版。

在PowerShell中輸入wslconfig /?, 可以看到:

PS D:\> wslconfig /?在 Linux Windows 子系統上執行管理操作用法:    /l, /list [/all] - 列出登入的分發內容。        /all - 有選擇地列出所有分發內容,包括目前               正安裝或未安裝的分發內容。    /s, /setdefault <DistributionName> - 將指定的分發內容設定為預設值。    /u, /unregister <DistributionName> - 登出分發內容。

切換預設發行版:

PS D:\> wslconfig /l# 適用於 Linux 的 Windows 子系統:Legacy (預設)UbuntuPS D:\> wslconfig /s UbuntuPS D:\> wslconfig /l# 適用於 Linux 的 Windows 子系統:Ubuntu (預設)Legacy

在Windows 1803 後,還支援更多配置。比如網路,root目錄等。進入發行版後, 可以在/etc/wsl.conf中配置。 如果沒有該檔案,可以手動建立一個配置:

[automount]enabled = true  # 自動掛載 c:/ 等到 /mntroot = /windir/options = "metadata,umask=22,fmask=11"mountFsTab = false[network]generateHosts = truegenerateResolvConf = true
4. WSL互動

也是從1709開始,WSL支援在Windows 10上直接使用 Linux命令:

PS D:\test>  wsl ls -latotal 5836drwxrwxrwx 1 root root    4096 Jan 25 13:20 .drwxrwxrwx 1 root root    4096 Apr 20 16:25 ..-rwxrwxrwx 1 root root     105 Oct 14  2017 03-build.ps1

同樣在 WSL 內也可以使用Windows應用程式,比如notepad,docker:

[email protected]:/mnt/d/go/src/code.teambition.com/soa/webhooks# docker.exe psCONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                                                                                        NAMES63698edb01a8        quay.io/coreos/etcd:latest   "/usr/local/bin/etcd"    2 days ago          Up 27 hours         0.0.0.0:2379->2379/tcp, 2380/tcp                                                             etcd

這是個非常贊的特性,極大方便了開發人員。但在使用過程中發現,有個體驗非常不好的地方,必須帶.exe尾碼才行,不然會提示找不到命令 :

[email protected]:/mnt/d/go/src/code.teambition.com/soa/webhooks# dockerThe program 'docker' is currently not installed. You can install it by typing:apt-get install docker

比如同事在mac上寫了個docker build的指令碼,放到Windows上後 想使用WSL去執行,發現必須加尾碼才行,這樣指令碼就沒辦法統一了

5. 解決方案

當然也可以在中裝個docker,而不是使用宿主機上的docker。但這樣會很冗餘,而且效能不好。經過一番折騰找到幾種解決方案:

5.1 使用別名

在WSL 中.bashrc設定別名,去掉尾碼:

alias docker=docker.exealias docker-compose=docker-compose.exe

這樣就可以正確運行命令了, 但別名只在互動環境有效,指令碼執行壞境不行。

5.2 多複製一份

在宿主機上找到 docker.exe,然後複製一份重新命名為 docker 放到同級目錄,這樣在wsl中也是可以執行的,有點蠢萌黑魔法的感覺。

5.3 重新導向

思路是定義command_not_found_handle函數(bash 4.0+ 支援),當任何命令找不到時,都會調用調用它。 然後在該函數中嘗試調用宿主機上cmd.exe,由它來來執行命令,並返回結果。

在.bashrc中添加:

command_not_found_handle() {    if cmd.exe /c "(where $1 || (help $1 |findstr /V Try)) >nul 2>nul && ($* || exit 0)"; then        return $?    else        if [ -x /usr/lib/command-not-found ]; then           /usr/lib/command-not-found -- "$1"           return $?        elif [ -x /usr/share/command-not-found/command-not-found ]; then           /usr/share/command-not-found/command-not-found -- "$1"           return $?        else           printf "%s: command not found\n" "$1" >&2           return 127        fi    fi}

或在.zshrc中添加:

command_not_found_handler() {    if cmd.exe /c "(where $1 || (help $1 |findstr /V Try)) >nul 2>nul && ($* || exit 0)"; then        return $?    else        [[ -x /usr/lib/command-not-found ]] || return 1        /usr/lib/command-not-found --no-failure-msg -- ${1+"$1"} && :    fi}
5.4 symlink

使用符號串連,講宿主機上的docker.exe 映射到 WSL中:

ln -sf /mnt/c/Program\ Files/Docker/Docker/resources/bin/docker.exe /usr/bin/docker
6. 其他6.1 閑聊

差不多有2年左右,沒寫部落格了。主要是因為從C#/Net,轉向Golang相關的技術棧了,需要重新積累和學習下。前期寫了段時間c++,然後寫Golang,發現Golang寫著舒服多了。當然跟有了女朋友後,變懶也有很大關係。

這篇是開頭,希望能繼續堅持分享,也有利於自己成長。新部落格會同步到github一份,方便備份修改。

6.2 參考

https://docs.microsoft.com/en-us/windows/wsl/interop

https://docs.microsoft.com/en-us/windows/wsl/wsl-config

https://github.com/Microsoft/WSL/issues/2003

WSL與Windows互動實踐

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.