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 的好处是:
控制面板 -> 程序 -> 启用或关闭 Windows 功能 -> 勾选 适用Linux的Windows子系统 (我是秋季创造者更新版本,我记得之前低版本可能是 beta 版本)
通过 PowerShell(管理员身份运行) 开启:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
然后按提示重启。
打开 window商店,搜索并选择合适的 Linux 发行版安装,比如 Ubuntu:
上一步安装Ubuntu后,可以直接像打开一个windows应用程序一样运行Ubuntu了,系统首次启动会设置账户和密码:
然后就进入自由的linux世界了,首选要注意下windows几个盘符挂载的路径:
比如可以cd /mnt/e
进入Windows E盘文件夹。
vim ~/.bashrc
alias cdc='cd /mnt/c/'
alias cdd='cd /mnt/d/'
alias cde='cd /mnt/e/'
alias tailf='tail -f'
source ~/.bashrc
备份:
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
新建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文件可以右键下载:
service ssh --full-restart
然后就可以使用xshell等更专业的Terminal玩耍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够了。