Windows中安装linux系统

Windows中安装linux系统

通过WSL命令安装

C:\Users\Administrator>wsl --list --online
以下是可安装的有效分发的列表。
使默认分发用 “*” 表示。
使用 '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

C:\Users\Administrator>wsl --install --distribution Debian
正在安装: 虚拟机平台
已安装 虚拟机平台。
正在安装: 适用于 Linux 的 Windows 子系统
已安装 适用于 Linux 的 Windows 子系统。
正在下载: WSL 内核
正在安装: WSL 内核
已安装 WSL 内核。
正在下载: Debian GNU/Linux

最后卡了很久没有反应,ctrl+c 结束掉再次安装提示必须要重启电脑,所以,这种方式还是慎用,安装不成功,连重试都重试不了。

这个卡住的问题的解决方法找到了,因为在使用wsl --shutdown 之后也会出现卡住的问题,导致重启失效,原因是LxssManager服务无法关闭,需要手动强制关闭,cmd中执行:

tasklist /svc /fi "imagename eq svchost.exe" | findstr LxssManager

得到一个pid, 打开任务管理 -> "详细信息"标签页 -> 找到pid对应的进程 -> 右键 -> 结束进程树。

查看wsl的版本:

wsl -l -v

通过Microsoft Store进行安装

这个只需要在界面上搜索 Debian 或者 Ubuntu,然后选择安装即可,非常方便。

WSL中的linux使用体验

使用了两周,感觉还是非常方便的,整个Debian系统就和一个应用一样,直接使用就可以了。在Debian里面也可以访问和执行Windows系统中的文件和程序。
在里面安装Docker,运行各种应用消耗的资源也非常少。

国内源

国内通过 apt-get 来安装软件,要从国外服务器下载,速度会比较慢,可以使用国内的镜像。
一般我们修改 /etc/apt/sources.list 文件,将 Debian 的默认源地址改成新的地址即可,我安装的是Debian11,也就是 bullseye, 具体的版本英文代号可以通过 lsb_release -cs查询, 常用安装源站点列表,我这边的网络ping下来,中科大的是最快的,所以,选择的是中科大的镜像:
163镜像站
deb http://mirrors.163.com/debian/ bullseye main non-free contrib
deb http://mirrors.163.com/debian/ bullseye-updates main non-free contrib
deb http://mirrors.163.com/debian/ bullseye-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ bullseye/updates main non-free contrib

deb-src http://mirrors.163.com/debian/ bullseye main non-free contrib
deb-src http://mirrors.163.com/debian/ bullseye-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ bullseye-backports main non-free contrib
deb-src http://mirrors.163.com/debian-security/ bullseye/updates main non-free contrib

华为云镜像站
deb https://mirrors.huaweicloud.com/debian/ bullseye main contrib non-free
deb https://mirrors.huaweicloud.com/debian/ bullseye-updates main contrib non-free
deb https://mirrors.huaweicloud.com/debian/ bullseye-backports main contrib non-free
deb https://mirrors.huaweicloud.com/debian-security/ bullseye/updates main contrib non-free

deb-src https://mirrors.huaweicloud.com/debian/ bullseye main contrib non-free
deb-src https://mirrors.huaweicloud.com/debian/ bullseye-updates main contrib non-free
deb-src https://mirrors.huaweicloud.com/debian/ bullseye-backports main contrib non-free

腾讯云镜像站
deb http://mirrors.cloud.tencent.com/debian/ bullseye main non-free contrib
deb http://mirrors.cloud.tencent.com/debian-security bullseye/updates main
deb http://mirrors.cloud.tencent.com/debian/ bullseye-updates main non-free contrib
deb http://mirrors.cloud.tencent.com/debian/ bullseye-backports main non-free contrib

deb-src http://mirrors.cloud.tencent.com/debian-security bullseye/updates main
deb-src http://mirrors.cloud.tencent.com/debian/ bullseye main non-free contrib
deb-src http://mirrors.cloud.tencent.com/debian/ bullseye-updates main non-free contrib
deb-src http://mirrors.cloud.tencent.com/debian/ bullseye-backports main non-free contrib

中科大镜像站
deb https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.ustc.edu.cn/debian-security/ bullseye/updates main contrib non-free

deb-src https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian-security/ bullseye/updates main contrib non-free

然后,再更新源索引信息:

sudo apt-get update
sudo apt-get upgrade

问题

  • hosts
    Debian每次启动的时候,会自动把Windows里面的hosts复制过来的。后面每次启动,都会直接复制和覆盖,所以,自己添加的一些hosts值在第二次启动的时候,被自动覆盖掉了,并且,覆盖完之后,也没有创建一个备份的hosts文件,得自己手动再恢复一遍。
    可以通过 /etc/wsl.conf 文件里面添加
[network]
generateHosts = false

来关掉复写的功能。

  • 访问本机防火墙
    第一次启动通过Windows的IP地址访问本机,是没有问题的,访问外网也没有问题。重启Debian之后,发现不能访问外网,本机Windows也不能访问。怀疑是防火墙的问题,把Windows的防火墙关掉,再ping,本机就可以ping通了,但是外网还是不能访问。搜索之后,发现要重置网络设置,然后重启。于是按照那个操作之后,重启Windows之后,外网访问正常,关键应该就是重启Windows吧。

但是,重启之后还是不能ping通本机,然后,Debian的宿主机的ip地址是在 /etc/resolv.conf里面的,通过powershell执行下面的命令,添加一条Windows系统的防火墙入站规则对WSL进行放行之后,可以ping通宿主机的地址。

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow

要直接ping Windows的ip地址,还是需要关闭防火墙。就不那么麻烦了,直接在hosts文件里面添加宿主机的ip地址就可以了。

172.xxx.xxx.xxx localwindows

当然,这个可以使用bash脚本来自动把 /etc/resolv.conf中的宿主机的IP地址提取出来,然后,写入到hosts中:

su
echo "$(cat /etc/resolv.conf | awk '/nameserver/ {print $2}') localwindows" >> /etc/hosts

或者创建一个 add_windows_host.sh 的bash脚本:

echo "$(cat /etc/resolv.conf | awk '/nameserver/ {print $2}') localwindows" >> /etc/hosts

然后,在命令行执行:

sudo bash add_windows_host.sh
  • 复制粘贴问题
    在terminal中进行复制粘贴,可以在配置属性中把 ctrl+shift+cctrl+shift+v 的快捷键开启,然后,用快捷键进行复制粘贴,也可以启动鼠标右键进行复制和粘贴。
    但是在Vim里面,只有粘贴可以使用,复制用不了。于是,对Vim进行了增强,通过Windows的clip.exe来接收复制的内容,但这个只能按照行来进行复制,不能严格按照选择的内容进行复制。
    /etc/vim/vimrc.local文件中:
map <C-S-c> :w !/mnt/c/Windows/System32/clip.exe
map! <C-S-c> :w !/mnt/c/Windows/System32/clip.exe
  • VIM中文显示问题
    vim中的中文显示问题,在 /etc/vim/vimrc.local文件中:
" encoding
set encoding=utf-8 
set fileencoding=utf-8
set termencoding=utf-8
  • terminal中文显示问题
    /etc/default/locale中添加
LANG=zh_CN.utf-8

这样,ls 查看目录中的中文文件就可以正常显示了,但是在terminal中用输入法输入的中文无法正常显示,目前没有找到解决方案,好在目前也不需要在terminal中输入中文。

  • 界面不刷新
    有时会出现界面的光标不闪动了,界面不刷新,什么都不显示了,一般,把窗口缩小再最大化,然后,按ctrl+l clear一下屏幕,就可以恢复。当然,有时这样操作之后,也不能恢复光标闪动,但命令行一般还是可以显示出来的。

  • MobaxTerm
    上面提到的terminal和界面显示相关的问题,使用MobaxTerm这个免费的terminal,这些问题都不会出现,使用体验非常好。

  • 自启动服务或脚本
    试过各种配置都不生效,包括/etc/rd.d/目录下面的配置也不生效,有一次发现/etc/profile.d/目录下面的sh是会在启动时就执行的,那就可以在这个目录下面去配置需要执行的任务,比如在/etc/profile.d/colorls.sh中添加 alias l='ls -la --color=auto'/etc/profile/etc/bashrc都是分别设置全局环境变量和bash相关的方法和别名的启动文件,但它们两个文件都建议不要直接修改这两个文件,而是在/etc/profile.d文件夹下面添加新的sh脚本。
    那就可以在其中添加一个/etc/profile.d/init.sh来设置自己想要配置的启动脚本。

  • 重启电脑后,WSL里面的linux无法上网,出现域名无法解析的问题
    在cmd中:

wsl --shutdown
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns

之后还要重启,挺无语的。

参考

locale详解
Windows中使用wsl之后文件名大小写不敏感导致的问题

posted @ 2022-12-02 16:23  yangwen0228  阅读(721)  评论(0编辑  收藏  举报