WSL win10子系统linux-ubuntu 安装 开机启动
0.为什么要用这个WSL子系统呢,因为省内存开销.开虚拟机,实在是低配机子吃不消.开这个子系统,内存就加一丢丢而已.win10系统,还是挺有改革的魄力和实力的.赞一下,开源越来越好
1.启用子系统
控制面板>程序>启用或关闭Windows功能>勾选适用于Linux的Windows子系统



2.在应用商店下载安装ubuntu
Microsoft Store>搜索WSL>找到ubuntu后下载和安装



安装完后会在开始菜单有ubuntu系统的的入口,点开就是terminal界面


3.安装完后会弹出ubuntu系统的terminal界面,要求你创建用户和密码.这个和普通的linux操作一样.
4.修改root账户的密码
sudo passwd root
5.切换到root用户
sudo su
6.查看是否已安装ssh,一般是已安装好的
root@SC-201905211111:~# dpkg -l | grep ssh
ii openssh-client 1:7.6p1-4ubuntu0.3 amd64 secure shell (SSH) client, for secure access to remote machines
ii openssh-server 1:7.6p1-4ubuntu0.3 amd64 secure shell (SSH) server, for secure access from remote machines
ii openssh-sftp-server 1:7.6p1-4ubuntu0.3 amd64 secure shell (SSH) sftp server module, for SFTP access from remote machines
ii ssh-import-id 5.7-0ubuntu1.1 all securely retrieve an SSH public key and install it locally
7.查看ssh服务是否开启,一般没开启,需要手动设置后开启服务
root@SC-201905211111:~# service ssh status
新安装的话,应该显示的状态是未开启的.现在需要去设置ssh的配置文件
#备份原文件
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
#进入配置文件
root@SC-201905211111:~# vim /etc/ssh/sshd_config
#修改端口为23,22有可能已被占用,并去掉之前的#注释符
Port 23
#开放监听IP,将之前的#注释符去掉即可
ListenAddress 0.0.0.0
#开启root用户登录:找下是否有PermitRootLogin no,有的话,将no改为yes.,没有的话,就新增一行
PermitRootLogin yes
#开启账号密码登录PasswordAuthentication no,改为yes
PasswordAuthentication yes
#保存退出(:wq 或 按住shift+zz)
开启ssh服务
service ssh start
#如果提示sshd error: could not load host key,则用下面的命令重新生成
dpkg-reconfigure openssh-server
#执行上述命令后,会有提示选择,直接按确认即可
8.secureCRT连接ubuntu登录
配置端口23
host为127.0.0.1
账号:root
连接后成功后提示输入密码,并保存
登录成功后应该如下:
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.4.0-17763-Microsoft x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Fri Aug 2 09:26:55 DST 2019
System load: 0.52 Memory usage: 76% Processes: 7
Usage of /home: unknown Swap usage: 1% Users logged in: 0
=> There were exceptions while processing one or more plugins. See
/var/log/landscape/sysinfo.log for more information.
0 packages can be updated.
0 updates are security updates.
Last login: Fri Aug 2 08:38:37 2019 from 127.0.0.1
9.设置开机启动ssh服务:
此时还不完善,因为ubuntu是windows子系统,子系统的开机并没有自启动ssh的功能.需要利用脚本去实现.否则你win10开机后,不能直接用secureCRT去连接WSL,而需要登录ubuntu去开启ssh服务再连,很麻烦.
配置ubuntu的脚本
#创建init.wsl文件,保存并保存.注意#! /bin/sh这行不是注释,是sh解释器的指定方式.以下三行:第一行为创建文件,二三两行是编写的内容
vim /etc/init.wsl
#! /bin/sh
/etc/init.d/ssh $1
添加执行权限
chmod +x /etc/init.wsl
编辑sudoers,避免输入密码
root@SC-201905211111:~# vim /etc/sudoers
#添加以下这行到该文件
%sudo ALL=NOPASSWD: /etc/init.wsl
配置windos脚本
先在运行>命令(win+r)中输入shell:startup打开启动文件夹

再在该文件夹中创建文件ubuntu1804.vbs
将以下内容写入该文件并保存
Set ws = CreateObject("Wscript.Shell")
ws.run "ubuntu run sudo /etc/init.wsl start", vbhide
10.重启win10,过一会,就可以利用secureCRT直接连接刚才配置的127.0.0.1的WSL了
11.换华为源
进入华为镜像源官网:https://mirrors.huaweicloud.com/
这里有很多资源,大家以后可以找找自己需要的东东.华为还是很强大的哈.关键的是华为还直接提供操作方法,很方便,很为用户考虑.华为的服务意识真好!哈哈,鼓掌!
而且,华为源的速度很快的.我自己体验下来,华为源的下载速度比阿里源还快
找到操作系统>ubuntu,接着就按官方的提示操作好了



root@SC-201905211111:~# cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
root@SC-201905211111:~# sed -i "s@http://.*archive.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list
root@SC-201905211111:~# sed -i "s@http://.*security.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list
root@SC-201905211111:~# apt-get update
12.WSL子系统ubuntu与windows系统文件交互
以后有需要从win10传文件到ubuntu,就直接访问文件夹就好了
步骤如下:
a.找到win10c盘(系统盘)的文件夹rootfs,这个文件夹是WSL安装的位置
推荐使用Everything这个小软件,很强大的全盘搜索工具.
b.在该文件上右键,添加到快速访问(以后就直接点快速访问进入就可以了)

c.该文件夹右键,获取所有权限(这个功能可能需要安装其他软件支持才有这个功能,我是装了腾讯电脑管家好像才有的,记不清了)

d.尝试通过win10丢文件到rootfs\home路径下,可以在cecureCRT中查看到文件

