在 Windows 上运行 Linux:WSL2 完整指南(二)

系列文章目录

在 Windows 上运行 Linux:WSL2 完整指南(一)🚪
在 Windows 上运行 Linux:WSL2 完整指南(二)🚪


文章目录


前言

在第一篇文章中,我们介绍了 WSL2 的特点和与传统虚拟机的比较,并讲解了如何在 Windows 上安装和配置 WSL2。在这篇文章中,我们将继续解决一些常见问题,并介绍如何对 WSL2 进行美化,使其使用体验更加友好。


四、常见问题及解决方法

问题二:0x800701bc

描述:在安装完 Linux 分发版之后可能会出现报错。

这个错误可能是因为电脑版本过低,需要下载安装适用于 x64 计算机的最新 WSL2 Linux 内核更新包。

解决方法:

下载链接:WSL2 Linux 内核更新包🚪。下载完更新包之后点击 next -> finish 即可解决问题。

问题三:0x80080005

描述:在输入用户名 huahua 的时候报错WslLaunchInteractive adduser --quiet --gecos '' huahua failed with error: 0x80080005

这个错误通常与COM(组件对象模型)服务有关,可能是由于WSL安装或配置的问题导致的

解决方法:

  1. 尝试重启 WSL 服务。在 PowerShell(以管理员身份运行)中执行以下命令:
powershell 复制代码
net stop LxssManager
net start LxssManager
  1. 如果特定的Linux发行版出现问题,可以尝试重置该发行版。注意,这会清除该发行版中的所有数据。然后重新安装该发行版。
powershell 复制代码
wsl --unregister <发行版名称>
  1. 确保已经启用了WSL和虚拟机平台功能。在PowerShell(以管理员身份运行)中执行以下命令:
powershell 复制代码
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  1. 尝试重装WSL组件。在PowerShell(以管理员身份运行)中执行以下命令:
powershell 复制代码
dism.exe /online /disable-feature /featurename:Microsoft-Windows-Subsystem-Linux
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

五、WSL 美化

5.1 准备工作

确保已经存在 WSL2,并在微软商店下载并安装 Windows Terminal。

5.2 安装 oh-my-zsh 美化包

  1. 安装 zsh:
powershell 复制代码
sudo apt update
sudo apt install zsh -y
  1. 安装oh-my-zsh(代理工具🚪)
powershell 复制代码
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

5.3 安装 Powerline9k 主题

  1. 安装主题
shell 复制代码
git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k
  1. 启用主题:编辑 ~/.zshrc 配置文件,修改 ZSH_THEME 项的内容为:ZSH_THEME="powerlevel9k/powerlevel9k"

5.4 安装字体

访问 nerd-fonts🚪Github release 页,下载某种字体的压缩包(如 JetBrainsMono.zip),解压后在 Windows 系统上安装字体文件(有些时候可能需要使用管理员权限安装)

修改 Windows Terminal 的默认字体:


六、oh-my-zsh插件

6.1 安装插件

  1. 安装 zsh-autosuggestions(自动补全):
powershell 复制代码
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  1. 安装 zsh-syntax-highlighting(高亮显示):
powershell 复制代码
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  1. 启用插件(包含默认插件):修改 ~/.zshrc 配置文件的 plugins 项,内容如下(可按需添加或删减)
powershell 复制代码
plugins=(
  cargo
  command-not-found
  docker
  git
  golang
  npm
  nvm
  rust
  sudo
  systemd
  web-search
  zsh-autosuggestions
  zsh-syntax-highlighting
)

6.2 添加天气和时钟显示

用一个shell脚本显示

root文件夹下新建一个weather-clock.sh文件,代码如下:

powershell 复制代码
#!/bin/bash

# NAME: now
# PATH: $HOME/bin
# DESC: Display current weather, calendar and time
# CALL: Called from terminal or ~/.bashrc
# DATE: Apr 6, 2017. Modified: May 24, 2019.

# UPDT: 2019-05-24 If Weather unavailable nicely formatted error message.

# NOTE: To display all available toilet fonts use this one-liner:
#       for i in ${TOILET_FONT_PATH:=/usr/share/figlet}/*.{t,f}lf; do j=${i##*/}; toilet -d "${i%/*}" -f "$j" "${j%.*}"; done

# Setup for 92 character wide terminal
DateColumn=34 # Default is 27 for 80 character line, 34 for 92 character line
TimeColumn=61 # Default is 49 for   "   "   "   "    61 "   "   "   "

# Replace Edmonton with your city name, GPS, etc. See: curl wttr.in/:help
curl wttr.in/Shanghai?0 --silent --max-time 3 > /tmp/now-weather
# Timeout #. Increase for slow connection---^

readarray aWeather < /tmp/now-weather
rm -f /tmp/now-weather

# Was valid weather report found or an error message?
if [[ "${aWeather[0]}" == "Weather report:"* ]] ; then
    WeatherSuccess=true
    echo "${aWeather[@]}"
else
    WeatherSuccess=false
    echo "+============================+"
    echo "| Weather unavailable now!!! |"
    echo "| Check reason with command: |"
    echo "|                            |"
    echo "| curl wttr.in/Edmonton?0    |" # Replace Edmonton with your city
    echo "|   --silent --max-time 3    |"
    echo "+============================+"
    echo " "
fi
echo " "                # Pad blank lines for calendar & time to fit

#--------- DATE -------------------------------------------------------------

# calendar current month with today highlighted.
# colors 00=bright white, 31=red, 32=green, 33=yellow, 34=blue, 35=purple,
#        36=cyan, 37=white

tput sc                 # Save cursor position.
# Move up 9 lines
i=0
while [ $((++i)) -lt 10 ]; do tput cuu1; done

if [[ "$WeatherSuccess" == true ]] ; then
    # Depending on length of your city name and country name you will:
    #   1. Comment out next three lines of code. Uncomment fourth code line.
    #   2. Change subtraction value and set number of print spaces to match
    #      subtraction value. Then place comment on fourth code line.
    Column=$((DateColumn - 10))
    tput cuf $Column        # Move x column number
    # Blank out ", country" with x spaces
    printf "          "
else
    tput cuf $DateColumn    # Position to column 27 for date display
fi

# -h needed to turn off formating: https://askubuntu.com/questions/1013954/bash-substring-stringoffsetlength-error/1013960#1013960
cal > /tmp/terminal1
# -h not supported in Ubuntu 18.04. Use second answer: https://askubuntu.com/a/1028566/307523
tr -cd '\11\12\15\40\60-\136\140-\176' < /tmp/terminal1  > /tmp/terminal

CalLineCnt=1
Today=$(date +"%e")

printf "\033[32m"   # color green -- see list above.

while IFS= read -r Cal; do
    printf "%s" "$Cal"
    if [[ $CalLineCnt -gt 2 ]] ; then
        # See if today is on current line & invert background
        tput cub 22
        for (( j=0 ; j <= 18 ; j += 3 )) ; do
            Test=${Cal:$j:2}            # Current day on calendar line
            if [[ "$Test" == "$Today" ]] ; then
                printf "\033[7m"        # Reverse: [ 7 m
                printf "%s" "$Today"
                printf "\033[0m"        # Normal: [ 0 m
                printf "\033[32m"       # color green -- see list above.
                tput cuf 1
            else
                tput cuf 3
            fi
        done
    fi

    tput cud1               # Down one line
    tput cuf $DateColumn    # Move 27 columns right
    CalLineCnt=$((++CalLineCnt))
done < /tmp/terminal

printf "\033[00m"           # color -- bright white (default)
echo ""

tput rc                     # Restore saved cursor position.

#-------- TIME --------------------------------------------------------------

tput sc                 # Save cursor position.
# Move up 8 lines
i=0
while [ $((++i)) -lt 9 ]; do tput cuu1; done
tput cuf $TimeColumn    # Move 49 columns right

# Do we have the toilet package?
if hash toilet 2>/dev/null; then
    echo " $(date +"%I:%M %P") " | \
        toilet -f future --filter border > /tmp/terminal
# Do we have the figlet package?
elif hash figlet 2>/dev/null; then
#    echo $(date +"%I:%M %P") | figlet > /tmp/terminal
    date +"%I:%M %P" | figlet > /tmp/terminal
# else use standard font
else
#    echo $(date +"%I:%M %P") > /tmp/terminal
    date +"%I:%M %P" > /tmp/terminal
fi

while IFS= read -r Time; do
    printf "\033[01;36m"    # color cyan
    printf "%s" "$Time"
    tput cud1               # Up one line
    tput cuf $TimeColumn    # Move 49 columns right
done < /tmp/terminal

tput rc                     # Restore saved cursor position.

exit 0

输入 curl wttr.in 命令可以查看当前位置天气情况

如果要更换为自己所在城市的天气,在上面的代码19行处,可自行更换为自己所在城市。运行上面的代码还需要下载两个插件,一个是ncaltoilet,分别是日期和时钟

shell 复制代码
sudo apt install ncal toilet

最后编辑 ~/.zshrc 配置文件,在最后一行加上bash /root/weather-clock.sh代码,重新打开WSL即可看到

6.3 启动动画

运行git clone https://github.com/pipeseroni/pipes.sh.git命令,在~/.zshrc 配置文件天气的前一行加上bash /root/pipes.sh/pipes.sh -p 5即可

运行效果为如下图片,按下空格键可退出动画:


总结

通过上述步骤,我们可以有效解决在使用 WSL2 过程中可能遇到的常见问题,并对 WSL2 进行美化,使其使用体验更加友好。希望这篇文章能为您在 Windows 上运行 Linux 提供帮助。希望这篇博客对你有所帮助,如果有任何疑问或需要进一步的信息,欢迎随时讨论🌹

相关推荐
Nancy Shea10 分钟前
centOS安装R语言4.0及以上
linux·r语言
薛文旺12 分钟前
Ubuntu glfw轮询获取按键事件
linux·ubuntu
Rita的程序bug13 分钟前
R语言报错 | object ‘integral‘ not found whilst loading name
运维
乌鸦乌鸦你的小虎牙15 分钟前
jsoncpp 使用说明(ubuntu)
linux·运维·ubuntu
做程序员的第一天15 分钟前
rknntoolkitlite2环境搭建
linux·人工智能·pycharm·rk3588·rknpu
2402_857583491 小时前
掌握Nginx负载均衡中的请求缓存处理:策略与实现
运维·nginx·负载均衡
kk_1778036191 小时前
服务器多核多线程跟高主频的有何区别?
运维·服务器
红龙创客1 小时前
ESXi服务器无法安装Windows11:“不符合此版本的Windows所需最低系统要求“
运维·服务器·windows
wanhengwangluo1 小时前
GPU服务器与CPU服务器的不同之处
运维·服务器·网络安全
希风雨1 小时前
银河麒麟高级服务器操作系统(Host版)V10 安装
运维·服务器