0%

WSL

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安装

一、安装

  1. 安装Terminal(非必须)
  2. 安装WSL(win10自带)
  3. 安装虚拟平台(启用WSL功能):wsl –install

二、使用

wsl –hep

  1. 查看列表:wsl -l -v
  2. 安装系统
  • Mircosoft store模式
  • 命令行模式
    • 查看支持的系统:wsl –list –online
    • 安装指定的系统:wsl –install Ubuntu-24.04
  1. 进入系统:wsl -d Debian
  2. 关闭系统:wsl –shutdown
  3. 设置默认:wsl -s Debian
  4. 设置代理
  1. 取消代理
  • unset http_proxy
  • unset https_proxy
  1. 卸载系统:wsl –unregister Ubuntu-24.04

三、参考


以Ubuntu-24.04为例

  1. wsl –install Ubuntu-24.04

  2. wsl,进入系统

  • wsl –user root,以root用户进入系统
  1. 设置用户名和密码:liusir/123456

  2. sudo su -

  • 123456
  1. passwd root
  • 123456
  1. ifconfig
  • Command ‘ifconfig’ not found, but can be installed with:
    • apt install net-tools
  1. apt update

  2. apt upgrade

  3. 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
  1. usermod -l liuyulong liusir,修改用户名

  2. userdel liuyulong

  3. groupdel liusir

  4. cd /home && rm -rf liusir

  5. useradd liusir

  6. 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
  1. 卸载Ubuntu-24.04: wsl –unregister Ubuntu-24.04

安装多个相同发行版本的Linux

一、操作

  1. 下载:https://github.com/DDoSolitary/LxRunOffline/releases/download/v3.5.0/LxRunOffline-v3.5.0-msvc.zip

  2. 解压

  3. 执行: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,卸载后重新复制
  1. 修改hostname:/etc/hostname
  • sudo hostnamectl set-hostname ubuntu-01
  • sudo hostnamectl set-hostname ubuntu-02
  1. 修改hosts:/etc/hosts
  • 127.0.1.1 ubuntu-01
  • 127.0.1.1 ubuntu-02
  1. 退出重进

二、问题

  1. 复制报错,多半为参数问题

  2. 复制后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

三、参考

  1. https://github.com/DDoSolitary/LxRunOffline