使用WSL在Windows下搭建高效的Linux開發環境


WSL簡介

WSL全稱是Windows Subsystem for Linux。

The Windows Subsystem for Linux lets developers run Linux environments -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the overhead of a virtual machine.

簡單的說 WSL就是在Windows上無需虛擬機就可以搭建一套Linux開發環境。

使用 WSL 的好處是:

  • 快速高效地開啟和運行 Linux 環境,比較而言:虛擬機的方式占用資源多而且打開慢,docker方式在Windows上異常麻煩,遠程到linux環境開發的方式又得依賴成熟的開發工具且操作不夠便捷。
  • 通過 Windows 商店 選擇多種 Linux 發行版,目前支持 Ubuntu、openSUSE Leap 42、 SUSE Linux Enterprise Server。
  • 使用 Linux 命令行工具可以更高端地進行windows文本處理,例如 sed、awk 等。
  • 使用 Linux 內置包管理器安裝一些軟件,例如 git、redis 等,基本上一條命令就能安裝好。

WSL安裝

開啟WSL功能

方法一

控制面板 -> 程序 -> 啟用或關閉 Windows 功能 -> 勾選 適用Linux的Windows子系統 (我是秋季創造者更新版本,我記得之前低版本可能是 beta 版本)

方法二

通過 PowerShell(管理員身份運行) 開啟:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

然后按提示重啟。

安裝 Linux 發行版

打開 window商店,搜索並選擇合適的 Linux 發行版安裝,比如 Ubuntu:

運行

上一步安裝Ubuntu后,可以直接像打開一個windows應用程序一樣運行Ubuntu了,系統首次啟動會設置賬戶和密碼:

然后就進入自由的linux世界了,首選要注意下windows幾個盤符掛載的路徑:

比如可以cd /mnt/e進入Windows E盤文件夾。

搭建環境

設置常用 alias

vim ~/.bashrc
alias cdc='cd /mnt/c/'
alias cdd='cd /mnt/d/'
alias cde='cd /mnt/e/'
alias tailf='tail -f'
source ~/.bashrc

apt設置阿里雲源

備份:

cp /etc/apt/sources.list /etc/apt/sources.list.bak
vim /etc/apt/sources.list 

修改內容:

deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

更新軟件列表:

apt-get update 

安裝常用工具

# git
apt-get install git -y

# redis
apt-get install  redis-server -y
service redis

設置windows右鍵菜單

新建txt文件:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\lxss_shell]
@="WSL Here"
"Icon"="\"%USERPROFILE%\\icon.ico\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\lxss_shell\command]
@="\"c:\\Windows\\System32\\bash.exe\""

保存為.reg文件,雙擊運行即可:

icon文件可以右鍵下載:

開啟SSH密碼登錄

service ssh --full-restart

然后就可以使用xshell等更專業的Terminal玩耍wsl了:

WSL服務自啟動

創建啟動腳本:

進入任意 WSL 發行版中,創建並編輯文件:/etc/init.wsl

#! /bin/sh
/etc/init.d/cron $1
/etc/init.d/ssh $1

里面調用了我們希望啟動的三個服務的啟動腳本,設置權限為可執行,所有者為 root,這時候可以通過:

sudo /etc/init.wsl [start|stop|restart]

來啟停我們需要的服務,在 Windows 中,開始-運行,輸入:

shell:startup

按照你 WSL 使用的 Linux 發行版創建啟動腳本,比如我創建的 Debian.vbs 文件:

Set ws = CreateObject("Wscript.Shell")
ws.run "wsl -d debian -u root /etc/init.wsl start", vbhide

這個腳本就會在你登陸的時候自動在名字為 "debian" 的 wsl 發行版中執行 /etc/init.wsl 啟動我們的服務了,如果你用的是 ubuntu18.04 的發行版,那么修改上面腳本里的 debian 為 ubuntu1804.vbs:

Set ws = CreateObject("Wscript.Shell")
ws.run "wsl -d Ubuntu-18.04 -u root /etc/init.wsl start", vbhide

而如果你不知道自己的 WSL 發行版叫做什么名字,可以用在windows cmd中wsl -l或者在linux系統中cat /etc/os-release查看。

然后,WSL就會隨着Windows系統自啟動了。

WSL 中可以開啟很多有用的服務,你可以按需刪改 /etc/init.wsl ,但沒必要塞很多東西進去影響你的啟動速度,比如 mysql/mongodb 這些重量級服務,需要用的時候再開啟。

一般sshd和crond夠了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM