利用 WSL 在 Windows下打造高效的 Linux 開發環境


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 的好處是:

  1. 最主要的一點,你可以快速的開啟 Linux 環境,之前我的做法是開啟虛擬器,占用資源多而且打開慢。
  2. 通過 window商店 選擇多種 Linux 發行版,目前支持 Ubuntu(16.04 LTS)、openSUSE Leap 42、 SUSE Linux Enterprise Server。
  3. 使用 Linux 命令行工具處理一些問題,例如 sed, awk 等。
  4. 使用 Linux 內置包管理器安裝一些軟件,例如 git redis 等,基本上一條命令就能安裝好。

安裝 WSL

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

你也可以通過 PowerShell(管理員身份運行) 開啟

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

然后按提示重啟。

  1. 打開 window商店,選擇合適的 Linux 發行版安裝 (這里搜索Linux 根據下面第一個suggestion 進入),這里我選擇了 Ubuntu

  2. 打開 Ubuntu ,第一次需要等待一會,設置賬號密碼,我這里直接設置成了 root

打造環境

設置右鍵菜單

新建 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 文件,運行即可。

PS:圖標為Windows 商店的圖標,我復制了一份放在我的用戶目錄下 。

設置常用 alias

  1. vim 打開.bashrc 文件
 vim ~/.bashrc
  1. 因為在 WSL 中 window 的盤符都是掛載在 /mnt 下的,所以設置 alias 有助於你快速的訪問 window 目錄.
alias cdc='cd /mnt/c/'
alias cdd='cd /mnt/d/'
alias cde='cd /mnt/e/'

我個人常用的 alias

alias tf='tail -500f'
alias hg='history|grep'
alias cls='clear'
alias ssh-a='ssh user@host'
alias untar='sudo tar -xzvf'

apt更換阿里雲源

  1. 備份,養成好習慣,在服務器上操作時記得也要備份哦。
 cp /etc/apt/sources.list /etc/apt/sources.list.bak
  1. vim 編輯 sources.list
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
  1. 更新軟件列表
 apt-get update 

安裝tmux

在日常開發中,我們經常會一邊運行程序 一邊看錯誤日志,總覺得窗口不夠用,每次都打開一個窗口又覺得麻煩,切換也不是很方便,這時候我們就需要 tmux 幫我們打開多個會話,分割多個窗口。
對於 tmux 不熟的同學,推薦花幾分鍾看一下tmux的使用方法和個性化配置 這篇文章,我也是看這篇文章學會的。

tmux 安裝:

apt-get install tmux -y

安裝常用工具

Git

apt-get install git -y

Redis

apt-get install  redis-server -y

service redis

參考

Windows Subsystem for Linux Overview
tmux的使用方法和個性化配置


免責聲明!

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



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