安装WSL
#列出已安装的 Linux 发行版
wsl -l -v
#您必须运行 Windows 10 版本 2004 及更高版本(内部版本 19041 及更高版本)或 Windows 11。
wsl --install
#重启Linux 子系统 以管理员权限运行cmd命令即可
#停止LxssManager服务
net stop LxssManager
#启动LxssManager服务
net start LxssManager
2.查看可用 Linux 发行版列表
wsl --list --online或wsl -l -o
##以下是可安装的有效分发的列表。
请使用“wsl --install -d <分发>”安装。
NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
openSUSE-42 openSUSE Leap 42
SLES-12 SUSE Linux Enterprise Server v12
Ubuntu-16.04 Ubuntu 16.04 LTS
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
3.安装
wsl --install -d Ubuntu-18.04
wsl2出现“参考的对象类型不支持尝试的操作"
reg 脚本解决
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WinSock2\Parameters\AppId_Catalog\0408F7A3]
"AppFullPath"="C:\\Windows\\System32\\wsl.exe"
"PermittedLspCategories"=dword:80000000
4. wsl 修改Linux 用户名和密码
C:\Users\SayHello>wsl -d Ubuntu-18.04 -u root # 进入根目录
root@Simle:/mnt/c/Users/SayHello# passwd root # 修改 root 账户密码
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@Simle:/mnt/c/Users/SayHello# exit
logout
4.1 配置apt镜像源
#备份
cp /etc/apt/sources.list /etc/apt/sources.list.bak
#编辑
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
vim操作:
全选(高亮显示):按esc后,然后ggvG或者ggVG
全部复制:按esc后,然后ggyG
全部删除:按esc后,然后dG
解析:
gg:是让光标移到首行,在vim才有效,vi中无效
v : 是进入Visual(可视)模式
G :光标移到最后一行
5. 启用SSH
# 移除默认ssh
apt remove openssh-server
# 重装
apt install openssh-server
#5.1 输入命令修改ssh_config文件:
sudo vim /etc/ssh/sshd_config
# 主要修改以下配置
Port 22 #默认即可,如果有端口占用可以自己修改
PermitRootLogin yes #允许root远程登录
PasswordAuthentication yes # 允许用户名密码方式登录
RSAAuthentication yes #秘钥认证
PubkeyAuthentication yes
#5.2 启动ssh服务
sudo /etc/init.d/ssh start
#5.3 可能会出现Could not load host key: /etc/ssh/ssh_host_rsa_key
/etc/ssh路径下执行 ssh-keygen -A 解决。
# 5.4 验证端口启动
root@Simle:/etc/ssh# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 66 root 3u IPv4 17516 0t0 TCP *:ssh (LISTEN)
sshd 66 root 4u IPv6 17518 0t0 TCP *:ssh (LISTEN)
查询ssh服务状态 sudo service ssh status
单次启动ssh服务 sudo service ssh start
单次停止ssh sudo service ssh stop
开启ssh开机自动启动 sudo systemctl enable ssh
关闭ssh开机自动启动 sudo systemctl disable ssh
6.设置静态IP
#在Ubuntu中添加一个IP地址,192.168.50.16,命名为eth0:1
wsl -d Ubuntu-18.04 -u root ip addr add 192.168.50.16/24 broadcast 192.168.50.255 dev eth0 label eth0:1
#在Win10中添加IP地址,192.168.50.88
netsh interface ip add address "vEthernet (WSL)" 192.168.50.88 255.255.255.0
#访问Ubuntu时使用 192.168.50.16 ,访问Win10时使用 192.168.50.88
#可以将以上两行命令保存为.bat文件,然后放入引导区,每次都自动执行。
wsl 启动的时候执行脚本
WSL 开机自启动项配置
WSL 的Linux每次重启的时候,很多服务都无法自启动通过systemctl 命令提示
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable docker
cd /etc/init.d # 存放服务的地方 创建脚本文件 startup.sh
ip addr add 192.168.50.16/24 broadcast 192.168.50.255 dev eth0 label eth0:1
/etc/init.d/ssh start
更改权限
chmod u+x startup.sh
Window cmd 命令
::管理员运行
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
::配置 wsl 的静态ip
wsl -d Ubuntu-18.04 -u root /etc/init.d/startup.sh
netsh interface ip add address "vEthernet (WSL)" 192.168.50.88 255.255.255.0
参考列表:
安装WSL|微软文档
搭建WSL开发环境|微软文档
APT镜像源
配置SSH登录
wsl 子系统 设置静态IP
wsl开机服务自启