blog_post/_posts/Win10-Linux-Subsystems.md

121 lines
7.2 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Win10下Linux子系统的启用
tags:
- Win10下linux子系统
categories:
- linux
comments: true
images: >-
http://cdn.7niu.begild.top/windows%E6%94%AF%E6%8C%81%E7%9A%84linux%E5%AD%90%E7%B3%BB%E7%BB%9F.png
abbrlink: 40bf05cc
date: 2018-04-19 12:19:13
---
##### 微软在win10系统上添加了Linux子系统的支持方便我们可以在windows上使用Linux省去了占内存的虚拟机和双系统切换的麻烦😎具体有什么和普通安装的Linux有什么不同抱歉...不知道😬。<!---more--->
## Linux子系统的获取
- 在 '控制面板-->程序和功能(侧边栏)-->启用或关闭 Windows 功能-->勾选适用于 Linux 的 Windows 子系统选项'。这样就成功使能 Linux 子系统的功能。需要重启生效更新请保存你的Word文档☠...)我看到网上的教程说明需要选中 '设置-->更新和安全-->针对开发人员-->开发人员模式' 但我的选项是 '旁加载应用' 也成功操作。
- Linux子系统的获取可以用微软的应用商店进行安装。如下图是支持的子系统种类(2018.04.19)我选择了Ubuntu 要使用其他系统的也看看,以后会支持更多的系统~ ~。
![windows支持的linux子系统](http://cdn.7niu.begild.top/windows%E6%94%AF%E6%8C%81%E7%9A%84linux%E5%AD%90%E7%B3%BB%E7%BB%9F.png)
- 就像装其他软件一样点击安装就会自动下载了。下载挺快的还📢,安装完毕之后就会显示启动这时候系统未被写入到磁盘里的。
![下载ubuntu](http://cdn.7niu.begild.top/%E4%B8%8B%E8%BD%BDubuntu.png)
- 这样获取 Windows 下Linux子系统就完成了。点击启动会打开CMD运行然后将系统写入磁盘所需时间并不长(SSD少于1分钟)长时间卡住尝试回车一下请求响应刷新界面。写入完毕会让你输入一个用户名(自定义哦不必须是 Windows 的用户名)和密码(不可见)。输入完毕就创建了一个用户,之后默认就是这个账户登录进系统。这个用户不具备管理员权限,如果你要默认使用管理员用户,那就直接关闭这个 CMD就跳过这一步了。之后再打开就直接以 root 用户登录。
```bash
Installing, this may take a few minutes...
Installation successful!
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: begild
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Default UNIX user set to: begild
To run a command as administrator (user "root"), use "sudo <command>".
begild@BeGild-PC:~$
```
## Linux 子系统的位置
- Linux子系统位于Users == 用户 👎):
`C:\Users\用户名\AppData\Local\Packages\含有Linux子系统名字的文件夹\LocalState\rootfs`
- 我使用的账户是 Administrator 装的是 Ubuntu 那么位置就是:
`C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs`
- 其他的系统和这个差不多,在 Packages 目录下找一下含有你安装的 Linux 子系统名字的文件夹就能找到。
## Linux子系统位置的迁移
因为linux子系统的位置式位于C盘的文件夹下面的时间长了的话会产生大量的数据C盘本来就装了Windows现在加了一个Linux两个系统的使用会导致C盘加速被塞满。所以把C盘迁移到其他的数据盘是一个比较好的选择。当然你的C盘足够大任性选择也OK。
```bat
robocopy "子系统所有数据所在的位置" "迁移的目的地址" /E /COPYALL /XJ
rmdir "子系统所有数据所在的位置" /S /Q
mklink /J "子系统所有数据所在的位置" "迁移的目的地址"
```
我的子系统所有数据所在的位置是
`C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc`
迁移的目的地址是 `D:\WSL`
所以代码就是
```bat
robocopy "C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc" "D:\WSL" /E /COPYALL /XJ
rmdir "C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc" /S /Q
mklink /J "C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc" "D:\WSL"
```
如果删除子系统数据目录的步骤(rmdir) 遇到不是空目录无法删除重启电脑之后再执行就OK了。
执行上面的东西用 cmder 使用 ls 命令可以看到已经建立一个符号链接到目标目录了。这样就把东西全部迁移到 D:\WSL 里去。
```bash
C:\Users\Administrator\AppData\Local\Packages
ღ ls -l CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
lrwxrwxrwx 1 Administrator 197121 6 4月 22 14:10 CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc -> /d/WSL/
```
## Linux子系统的打开
1. 任意打开一个CMD窗口输入`bash` 启动一个Linux终端
2. 将 Ubuntu 固定到开始菜单磁贴每次点击都能打开一个新的Linux终端窗口。
## Linux子系统的一些信息
1. 使用` lsb_release -a` 可以看到系统的信息安装的是16.04.3长期支持版本
2. `cat /proc/version` 可以看到内核版本 4.4.0-43-Microsoft;编译器GCC版本5.4.0
```bash
root@BeGild-PC:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
root@BeGild-PC:~# uname -r
4.4.0-43-Microsoft
root@BeGild-PC:~# cat /proc/version
Linux version 4.4.0-43-Microsoft (Microsoft@Microsoft.com) (gcc version 5.4.0 (GCC) ) #1-Microsoft Wed Dec 31 14:42:53 PST 2014
```
## 换软件源
emmm,将软件源换为国内源,这样更新和下载软件都会比较快一点。
1. 备份软件源列表:
```bash
# mv /etc/apt/sources.list /etc/apt/sources.list.bak
```
2. 编辑软件源。
```bash
# vi /etc/apt/sources.list
```
3. 替换为国内源(我选[清华大学源](https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/),可以去看看阿里源什么的)。
```bash
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
```
4. 更新软件列表
```bash
# apt-get update
```
5. 更新软件,第一次会下载比较多的更新耐心等待。
```bash
# apt-get upgrade
```