Windows Subsystem for Linux(简称WSL)是一个在Windows 10\11上能够运行原生Linux二进制可执行文件(ELF格式)的兼容层。它是由微软与Canonical公司合作开发,开发人员可以在 Windows计算机上同时访问 Windows和Linux的强大功能。通过适用于Linux的Windows子系统(WSL),开发人员可以安装Linux发行版(例如 Ubuntu、OpenSUSE、Kali、Debian、Arch Linux等),并直接在Windows上使用Linux应用程序、实用程序和Bash命令行工具,不用进行任何修改,也无需承担传统虚拟机或双启动设置的费用。
WSL安装
一、安装
- 安装Terminal(非必须)
- 安装WSL(win10自带)
- 安装虚拟平台(启用WSL功能):wsl –install
二、使用
wsl –hep
- 查看列表:wsl -l -v
- 安装系统
- Mircosoft store模式
- 命令行模式
- 查看支持的系统:wsl –list –online
- 安装指定的系统:wsl –install Ubuntu-24.04
- 进入系统:wsl -d Debian
- 关闭系统:wsl –shutdown
- 设置默认:wsl -s Debian
- 设置代理
- 查看windows地址:ipconfig,得到vEthernet (WSL)的IPv4地址:172.27.144.1
- 进入系统
- export http_proxy=http://172.27.144.1:7890
- export https_proxy=https://172.27.144.1:7890
- 取消代理
- unset http_proxy
- unset https_proxy
- 卸载系统:wsl –unregister Ubuntu-24.04
三、参考
以Ubuntu-24.04为例
wsl –install Ubuntu-24.04
wsl,进入系统
- wsl –user root,以root用户进入系统
设置用户名和密码:liusir/123456
sudo su -
- 123456
- passwd root
- 123456
- ifconfig
- Command ‘ifconfig’ not found, but can be installed with:
- apt install net-tools
apt update
apt upgrade
systemctl start sshd
- Failed to start sshd.service: Unit sshd.service not found.
- apt install openssh-server
- systemctl enable ssh.service
- systemctl list-units –type=service | grep ssh,查看已安装的服务
- vim /etc/ssh/sshd_config
- PermitRootLogin yes
- service sshd restart
- 卸载服务
- apt remove openssh-server
- apt autoremove
usermod -l liuyulong liusir,修改用户名
userdel liuyulong
groupdel liusir
cd /home && rm -rf liusir
useradd liusir
useradd liuyulong
- ssh登录报错
- /home/liusir/.hushlogin file.
- touch: cannot touch ‘/home/liusir/.motd_shown’: No such file or directory
- cd /home
- mkhomedir_helper liusir
- mkhomedir_helper liuyulong
- useradd liusir -m
- useradd liuyulong -m
- usermod -s /bin/bash liusir
- usermod -s /bin/bash liuyulong
- 再次登录,报错:/usr/bin/xauth: file /home/liusir/.Xauthority does not exist
- 在对应主目录下创建:touch .Xauthority
- 卸载Ubuntu-24.04: wsl –unregister Ubuntu-24.04
安装多个相同发行版本的Linux
一、操作
下载:https://github.com/DDoSolitary/LxRunOffline/releases/download/v3.5.0/LxRunOffline-v3.5.0-msvc.zip
解压
执行:LxRunOffline.exe d -n Ubuntu-24.04 -d C:\WSL\Ubuntu-24.04-02 -N Ubuntu-24.04-02
- 报错:
[ERROR] The distro "Ubuntu-24.04-02" already exists.
- 解决:wsl –unregister Ubuntu-24.04-02,卸载后重新复制
- 修改hostname:/etc/hostname
- sudo hostnamectl set-hostname ubuntu-01
- sudo hostnamectl set-hostname ubuntu-02
- 修改hosts:/etc/hosts
- 127.0.1.1 ubuntu-01
- 127.0.1.1 ubuntu-02
- 退出重进
二、问题
复制报错,多半为参数问题
复制后ip/mac什么的都一样
修改IP地址和MAC地址
sudo ifconfig eth0 192.168.1.2 netmask 255.255.255.0
sudo route add default gw 192.168.1.1 dev eth0
sudo ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE
- 报错:SIOCSIFHWADDR: Device or resource busy - you may need to down the interface
- 处理
- sudo ifconfig eth0 down
- sudo ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE
- sudo ifconfig eth0 up
sudo ifconfig eth0 192.168.1.3 netmask 255.255.255.0
sudo route add default gw 192.168.1.1 dev eth0
sudo ifconfig eth0 hw ether 00:A1:B1:C1:D1:E1
- sudo ifconfig eth0 down
- sudo ifconfig eth0 hw ether 00:A1:B1:C1:D1:E1
- sudo ifconfig eth0 up