ref: https://blog.csdn.net/weixin_45883933/article/details/106085184
安装前配置
ref: 适用于 Linux 的 Windows 子系统安装指南 (Windows 10)
1. 启用 Windows 功能
搜索并打开“启用或关闭 Windows 功能”,然后选择“适用于Linux的Windows子系统”复选框。
在windows功能中重新勾选hyper-v
然后开启hyper-v模式
在管理员powershell中执行
bcdedit /set hypervisorlaunchtype auto
如果禁用了组策略里面的Device Guard虚拟化安全设置,
需要打开组策略管理,
本地计算机策略 > 计算机配置 > 管理模板>系统 > Device Guard
打开 基于虚拟化的安全设置为“已开启”或者“未设置”
随后重新开启wsl2,若不行,重启计算机。
启用虚拟机平台可选组件
在 powerShell 中以管理员身份运行下面命令
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
运行完成之后,请重启电脑完成安装.
设置WSL发行版
如果想要将默认的WSL发行版设置成 WSL 2,在 powerShell 中使用下面命令
wsl --set-default-version 2
如果想要设置某一个发行版为WSL2,在 powerShell 中使用下面命令,将 换成你想要设置的发行版即可,例如 Ubuntu-18.04
wsl --set-version <Distro> 2
wsl --set-version Ubuntu-20.04 2
验证使用的WSL版本
wsl -l -v
下载安装 Ubuntu-20.04 (Windows 应用商店里)
更新包目录,并使用分发版的首选包管理器升级已安装的包
sudo apt update && sudo apt upgrade
Windows不会自动更新或升级Linux发行版:Linux用户经常意外自行控制此任务。
ubuntu@kylin:〜$ wslfetch
./+o+- Windows 10 Linux Subsystem
yyyyy. 'yyyyyy+ root@kylin
.;//+/h yyyyyyo BUILD: 19624
.++ .:/++++++/-.`sss/` BRANCH: rs_prerelease
.:++o: `\++++++++/:---:/- RELEASE: Ubuntu 20.04 LTS
o:+o+:++. `````'-/ooo+++++\ KERNEL: Linux 4.19.104-microsoft-standard
.:+o:+o/. `+sssooo+\ UPTIME: 0d 0h 2m
.++/+ +oo+o:` \sssooo;
/+++//+: oo+o
\+/+o+++ o++o ydddhh+
.++.o+ +oo+:` /dddhhh;
.+.o+oo:. oddhhhh+
\+.++o+o` -,,,,.:ohdhhhhh+
`:o+++ ohhhhhhhhyo++os:
.o: .syhhhhhhh'.oo++o.
/osyyyyyyy.oooo+++\
`````+oo+++o:/
`oo++'`
root@kylin:~
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04 LTS
Release: 20.04
Codename: focal
ubuntu @ kylin:/ $ sudo apt install lolcat
sudo apt install lolcat
lsb_release -a | lolcat
wsl ubuntu config
1 修改 默认的源 (更换国内源)
cp /etc/apt/sources.list /etc/apt/sourses.list.bak
更换默认源为阿里源, 使用 sudo vim /etc/apt/sources.list 命令编辑,删除原来的内容,添加下面的阿里源信息
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
更换源之后,使用下面的命令更新一下
sudo apt-get update
sudo apt-get upgrade
2.ssh 连接 配置
在WSL Ubuntu系统中安装ssh server
当对Linux实现文件操作时,使用WinScp更为方便。因此需要使用ssh远程登陆
安装ssh server
sudo apt-get install openssh-server
配置ssh
使用 cp 命令将 SSH 相关配置备份
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
使用 vim 编辑 sshd_config 文件
sudo vim /etc/ssh/sshd_config
调整一下设置:
Port 22
ListenAddress 0.0.0.0
PermitRootLogin yes
StrictModes yes
PasswordAuthentication yes
root@summer:/# service ssh status
* sshd is not running
root@summer:/# service ssh start
* Starting OpenBSD Secure Shell server sshd
sshd: no hostkeys available -- exiting.
root@summer:/# sshd -T
sshd: no hostkeys available -- exiting.
root@summer:/# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
root@summer:/# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
root@summer:/etc/ssh# sshd -T
root@summer:/etc/ssh# service ssh start
* Starting OpenBSD Secure Shell server sshd [ OK ]
#Ubuntu的防火墙状态检测,防火墙可能限制SSH端口22
root@summer:~# service ufw status
* Firewall is not running... [fail]
root@kylin:~#
重启ssh service
sudo service ssh restart
sshd: no hostkeys available — exiting
在开启SSHD服务时报错.
sshd re-exec requires execution with an absolute path
用绝对路径启动,也报错如下:
Could not load host key: /etc/ssh/ssh_host_key
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_dsa_key
Disabling protocol version 1. Could not load host key
Disabling protocol version 2. Could not load host key
sshd: no hostkeys available — exiting
解决过程:
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
/usr/sbin/sshd
如果上述两个文件存在,仍然出现这个错误,那么试试 chmod 600 上述两个文件。之后应该可以解决。
ssh login
登陆 SSH
使用 SSH 指令登陆
ssh root@127.0.0.1 -p 22
运行/停止Ubuntu子系统
wsl -l
列出了系统中安装的子系统名称,可以是一个或多个,本文中的子系统名称是Ubuntu-18.04-20190707,接下来针对这个默认子系统进行操作:
运行子系统
wsl --distribution Ubuntu-18.04-20190707
或者
wsl -d Ubuntu-18.04-20190707
查看运行中的子系统
适用于 Linux 的 Windows 子系统:
Ubuntu-18.04-20190707 (默认)
停止子系统
wsl -t Ubuntu-18.04-20190707
或者
wsl --terminate Ubuntu-18.04-20190707
备份/删除/还原子系统
备份子系统
非常简单,但一定要先停止子系统之后再备份
wsl --export Ubuntu-18.04-20190707 c:\temp\Ubuntu-18.04-20190707.tar
等待完成即可。
备份成功后,子系统会被打包成命令中指定的tar文件。
删除子系统
也是一个命令即可:
wsl --unregister Ubuntu-18.04-20190707
这样WSL子系统就从Windows中删除的干干净净了。
还原子系统
删除了没关系,刚才做了备份,也是一个命令还原:
wsl --import Ubuntu-18.04-20190707 c:\WSL c:\temp\Ubuntu-18.04-20190707.tar
这里注意指定还原的路径。成功后,子系统又回来了,可以用wsl -l确认一下。
install docker-engines under ubuntu.
refer to: https://docs.docker.com/engine/install/ubuntu/
#启动docker sudo service docker start
service --status-all
sudo service docker start #WSL2下能使用 systemctl, 参考:https://www.cnblogs.com/a5idc/p/13752839.html
sudo usermod -aG docker {$USER},$user是linux os你创建的用户,参考:https://docs.docker.com/engine/install/linux-postinstall/
docker run hello-world #检查是否安装成功
install docker-compose under ubuntu.
参考: https://docs.docker.com/compose/install/
sudo chmod +x /usr/local/bin/docker-compose
在ubuntu下git checkout docker-compose目录,运行mysql等服务。在windows下,可直接使用localhost连接
docker-compose up #启动并运行
docker-compose up -d #在后台运行
docker-compose down
apt-get install telnet
apt-get -y install netcat-traditional
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)