Win10 Windows Subsystem for Linux

1. Win10 Windows Subsystem for Linux

1.1. 启用 WIN10 WSL

  1. 设置 -> 更新和安全 -> 针对开发人员 -> 选中 “开发人员模式”
  2. 启用 “适用于 Linux 的 Windows 子系统”
  3. 打开控制面板 -> 程序 -> 启用或关闭 Windows 功能, 选中勾选 “适用于 Linux 的 Windows 子系统”, 保存, 此时提示需要重启系统
  4. 打开 cmd, 输入 bash 回车, 提示访问一个网址(https://aka.ms/wslstore), 使用浏览器访问, 重定向到应用商店, 然后可以获取需要的 Linux 子系统, 比如 Debian
Windows Subsystem for Linux has no installed distributions.
Distributions can be installed by visiting the Windows Store:
https://aka.ms/wslstore
Press any key to continue...

1.1.1. 删除 WIN10 WSL

uninstall using:

lxrun /uninstall /full

reboot

then:

lxrun /install /y

reboot

This gets you back to the initial installation: at least I can get at root now.

1.2. 打开 WIN10 WSL

  1. 方法 1

cmd -> 输入 bash or debian

  1. 方法 2

使用 Pin Start

  1. 使用 Microsoft Store

点击 Windows 按钮, 直接输入 store 即可

1.2.1. 指定用户登录 WSL

1.2.1.1. 方法 1

In Windows admin command prompt (Super+X, A) change the default user to root:

lxrun /setdefaultuser root

Now Bash on Ubuntu on Windows logs you in as root without asking password

Use passwd command in Bash to change the user password:

passwd your_username

Change the default user back to your normal user in Windows command prompt

lxrun /setdefaultuser your_username
1.2.1.2. 方法 2

In the Command Prompt or PowerShell window (not a Bash shell window):

  • Ubuntu: ubuntu config --default-user username
  • Debian: debian config --default-user username
  • openSUSE Leap 42: opensuse-42 --default-user username
  • SUSE Linux Enterprise Server 12: sles-12 --default-user username

1.3. 使用 wslconfig 命令进行管理

  1. 设置默认运行的 Linux 系统
wslconfig /setdefault <DistributionName>

正如上面所说, 如果执行 wslconfig /setdefault ubuntu, 那么执行 bash 命令则会运行 ubuntu

  1. 卸载 Linux 系统
wslconfig /unregister <DistributionName>

当系统出现问题, 我们可以卸载后重新安装。如: wslconfig /unregeister ubuntu

  1. 查看已安装的 Linux 系统
wslconfig /list

1.4. 使用 WIN10 WSL 访问 Windows 磁盘内容

Windows 磁盘在 Linux 子系统下都是有挂载的, 输入 mount 命令可以查看挂载情况, 只要你的操作系统是中文路径就支持中文, 直接可以使用 cd 命令来切换中文目录, 非常神奇。

一般情况下 C 盘、D 盘、E 盘的挂载路径分别是 /mnt/c/mnt/d/mnt/e, 依此类推。

1.5. 重启 WSL 里面的操作系统

1.5.1. 方法 1

Open Settings: Click the Start button, then click Settings.

Open Apps Category and find Ubuntu: Click on Apps button, and then search for Ubuntu (or your distribution name) under Apps & features.

Open Advance Options: Under search results, click on Ubuntu (or your distribution name) and then click on Advance options.

Hit Reset: Click the Reset button.

1.5.2. 方法 2

Since Windows 10 version 1803, closing all WSL terminal windows won’t kill background processes by default, unless the file /var/run/reboot-required is present. This file will be automatically created by apt on Ubuntu when an update requires a reboot, but if you want to manually reboot the subsystem, you can create the file yourself:

sudo touch /var/run/reboot-required

I haven’t tested this on other distributions available in the Microsoft Store. An alternative solution is to kill all processes yourself:

sudo killall -r '.*'

1.5.3. 方法 3

As an Administrator restart the windows Service “LxssManager” on Windows 10. This does a clean boot of the WSL. The services in the Linux Subsystem - for example xrdp - must be restarted if not enabled for autostart.

1.6. WSL 文件的位置

早期版本:

%localappdata%\Lxss\rootfs

现在, 如果安装的是 Debian:

C:\Users\Administrator\AppData\Local\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState\rootfs

1.7. sudo 免密

$ sudo nano /etc/sudoers

## User privilege specification
root    ALL=(ALL:ALL) ALL
user    ALL=(ALL:ALL) NOPASSWD:ALL  // 添加这行

## Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
user    ALL=(ALL:ALL) NOPASSWD:ALL  // 添加这行, 为什么这里也要添加? 因为如果没有添加此行, 上面的 %sudo 会覆盖 NOPASSWD:ALL 导致设置失败。

查看当前用户所在组命令:

$ groups user
user : user adm cdrom sudo dip plugdev

1.8. 使用国内的镜像源

由于众所周知的原因, 如果直接使用这样的 Linux 子系统安装软件的话, 网速非常慢, 需要改成国内的镜像源, 我们这里使用中科大的镜像源。

由于镜像源要支持 https 协议需要安装软件 apt-transport-https(WSL 版本默认未安装), 所以我们先用 http 协议安装 apt-transport-https。

  1. 替换源 http 为中科大镜像的 http
$ sudo sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
$ sudo sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list
  1. 安装 apt-transport-https
$ sudo apt update
$ sudo apt install apt-transport-https -y
  1. 替换中科大镜像的 http 为 https
$ sudo nano /etc/apt/sources.list   加上 s 就可以了

1.9. WIN10 子系统安装图形化界面

参考文章: https://baijiahao.baidu.com/s?id=1596652006568524478&wfr=spider&for=pc

1.9.1. 各桌面系统比较

各桌面性能评价:

桌面环境 / 窗口管理器RAM used% CPU used类型
KDE 4.6363 MB4 %桌面环境
*** Unity271 MB14%桌面环境(shell)
*** GNOME 3193 MB10%桌面环境
GNOME 2.x191 MB1 %桌面环境
XFCE 4.8144 MB10 %桌面环境
LXDE85 MB10 %桌面环境

Debian 9 Stretch Minimal CD 用的是 XFCE, 通过打开终端点击 Help -> About 可以看到用的是什么桌面系统。

1.9.1.1. What is the meaning of: Xorg, display manager, X server ?

X-Windows is a Unix/Linux Client-Server system. Although usually on the same computer, the Client and Server can be on different computers, unlike Microsoft Windows.

X11 is a network protocol. It encodes things such as graphic primitives, images, pointer motion, and key presses.

X clients are graphical applications like Firefox.

Xorg is an X server. It implements X11 and provides an interface to keyboards, mice, and video cards.

Gnome, KDE, LXDE, and Xfce are desktop environments. They provide libraries and specifications that applications use

When running X-Windows, instead of a console login, the Display Manager i.e. Lightdm starts up. The DM starts the X-server and asks for login credentials and then launches the user’s desktop environment.

1.9.1.2. XFCE vs LXDE

Which one is faster?

Obviously LXDE. It is even lighter than XFCE. LXDE uses about 110 mb of ram when idle while XFCE is a little more bloated, taking about 200 mb (based on my experience)

Which is easier to use?

XFCE. It have more GUIs while LXDE requires you to modify configuration files in your own in some cases.

More configurable?

Both are configurable, but it depends on how much you want to play with it. It is less configurable than KDE or GNOME, though.

Can I install them?

Via the software center everything is easier. But I recommend you use lubuntu-desktop for LXDE or xubntu-desktop for XFCE. (Since they’ve prepared most of the stuff for you)

1.10. 问题

Windows Linux 子系统并不是一个完善的系统, 在使用过程中我遇到了一些问题

1.10.1. Error C1083: Cannot open source file: "stdafx.cpp’: No such file or directory

For the record, I had this problem when cloned a repo using git from the Ubuntu shell in Windows 10.

After I cloned the repo to a different location usin git from a Windows shell, the problem went away and the solution builds fine.

The issue was that the build scripts were looking for ‘stdafx.cpp’ in a folder with uppercase name (C:\GIT\LIBRARYX\SRC\) and this path did not work if the solution was cloned using git from Ubuntu bash.

原因找到了, 是 Windows 驱动器挂载引起的不兼容导致的, 目前比较靠谱的做法是先 git clone 到 Linux 子系统的存储区, 然后再压缩成 .tar.gz 包, 将这个包移动到 Windows 驱动器 (挂载在 /mnt 下) 中, 然后在 Windows 下用 Windows 压缩软件解压缩。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云满笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值