系列文章目录
在 Windows 上运行 Linux:WSL2 完整指南(一)🚪
在 Windows 上运行 Linux:WSL2 完整指南(二)🚪
文章目录
- 系列文章目录
- 前言
- 四、常见问题及解决方法
- [五、WSL 美化](#五、WSL 美化)
-
- [5.1 准备工作](#5.1 准备工作)
- [5.2 安装 oh-my-zsh 美化包](#5.2 安装 oh-my-zsh 美化包)
- [5.3 安装 Powerline9k 主题](#5.3 安装 Powerline9k 主题)
- [5.4 安装字体](#5.4 安装字体)
- 六、oh-my-zsh插件
-
- [6.1 安装插件](#6.1 安装插件)
- [6.2 添加天气和时钟显示](#6.2 添加天气和时钟显示)
- [6.3 启动动画](#6.3 启动动画)
- 总结
前言
在第一篇文章中,我们介绍了 WSL2 的特点和与传统虚拟机的比较,并讲解了如何在 Windows 上安装和配置 WSL2。在这篇文章中,我们将继续解决一些常见问题,并介绍如何对 WSL2 进行美化,使其使用体验更加友好。
![](https://i-blog.csdnimg.cn/direct/cd48acb8071b4a519b1a46424eafd77a.png)
四、常见问题及解决方法
问题二:0x800701bc
描述:在安装完 Linux 分发版之后可能会出现报错。
![](https://i-blog.csdnimg.cn/direct/043767523e754112a0bfc7ba273ec240.png)
这个错误可能是因为电脑版本过低,需要下载安装适用于 x64 计算机的最新 WSL2 Linux 内核更新包。
解决方法:
下载链接:WSL2 Linux 内核更新包🚪。下载完更新包之后点击 next -> finish 即可解决问题。
问题三:0x80080005
描述:在输入用户名 huahua
的时候报错WslLaunchInteractive adduser --quiet --gecos '' huahua failed with error: 0x80080005
这个错误通常与COM(组件对象模型)服务有关,可能是由于WSL安装或配置的问题导致的
解决方法:
- 尝试重启 WSL 服务。在 PowerShell(以管理员身份运行)中执行以下命令:
powershell
net stop LxssManager
net start LxssManager
- 如果特定的Linux发行版出现问题,可以尝试重置该发行版。注意,这会清除该发行版中的所有数据。然后重新安装该发行版。
powershell
wsl --unregister <发行版名称>
- 确保已经启用了WSL和虚拟机平台功能。在PowerShell(以管理员身份运行)中执行以下命令:
powershell
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
- 尝试重装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 美化包
- 安装 zsh:
powershell
sudo apt update
sudo apt install zsh -y
![](https://i-blog.csdnimg.cn/direct/ed8e58ab4ab34a8b893f13c0b43cc315.png)
- 安装oh-my-zsh(代理工具🚪)
powershell
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
![](https://i-blog.csdnimg.cn/direct/7df8cb7e9cfb4dc7a42de44d61bc0c45.png)
5.3 安装 Powerline9k 主题
- 安装主题
shell
git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k
![](https://i-blog.csdnimg.cn/direct/56bd2251a63b495da3cf3f74d8c43051.png)
- 启用主题:编辑
~/.zshrc
配置文件,修改 ZSH_THEME 项的内容为:ZSH_THEME="powerlevel9k/powerlevel9k"
5.4 安装字体
访问 nerd-fonts🚪 的 Github release
页,下载某种字体的压缩包(如 JetBrainsMono.zip
),解压后在 Windows 系统上安装字体文件(有些时候可能需要使用管理员权限安装)
修改 Windows Terminal 的默认字体:
六、oh-my-zsh插件
6.1 安装插件
- 安装
zsh-autosuggestions
(自动补全):
powershell
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
- 安装
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
- 启用插件(包含默认插件):修改
~/.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行处,可自行更换为自己所在城市。运行上面的代码还需要下载两个插件,一个是ncal
和toilet
,分别是日期和时钟
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 提供帮助。希望这篇博客对你有所帮助,如果有任何疑问或需要进一步的信息,欢迎随时讨论🌹