0%

Window构建高效的Linux开发环境

本文主要包括:

  • 什么是WSL?
  • 安装WSL
  • WSl常用命令

什么是WSL?

WSL是Windows子系统Linux的缩写。它是一个Windows 10的特性,允许用户在Windows操作系统中运行Linux内核。WSL不是虚拟机,也不是仿真器,它实现了Linux内核与Windows之间的精细互操作。

WSL存在的目的

Windows和Linux是两个不同的操作系统,用户如果需要同时使用它们,需要在同一台设备上安装不同的操作系统。WSL的存在正是为了解决这一问题,它可以让Windows用户在本地机器上同时运行Windows和Linux,而无需使用虚拟机或者双重启动来执行类似的任务。

WSL有哪些版本?

WSL1是Microsoft于2016年首次发布的版本,它与Windows内核紧密集成。WSL2则是2020年发布的全新版本,它使用完整的Linux内核,可以快速运行Linux二进制代码,提高性能和兼容性。两个版本都提供了Linux命令行界面和标准的Linux软件包,例如bash、ssh和grep等工具。

WSL与WSL2有什么区别?

WSL2相比WSL1,在性能和兼容性方面有显著提高,主要原因是它使用完整的Linux内核。在WSL2中,Linux内核以Hyper-V轻量级虚拟化的形式运行在Windows操作系统上,提供了对Docker等Linux容器的原生支持,同时还增强了文件系统访问和文件I/O性能。
但是,WSL2的启动时间和内存占用都比WSL1高,需要占用更多的存储空间。用户可以根据具体需求灵活选择哪个版本更适合自己。

安装WSL

开发人员可以在 Windows 计算机上同时访问 Windows 和 Linux 的强大功能。 通过适用于 Linux 的 Windows 子系统 (WSL),开发人员可以安装 Linux 发行版(例如 Ubuntu、OpenSUSE、Kali、Debian、Arch Linux 等),并直接在 Windows 上使用 Linux 应用程序、实用程序和 Bash 命令行工具,不用进行任何修改,也无需承担传统虚拟机或双启动设置的费用。

先决条件

必须运行 Windows 10 版本 2004 及更高版本(内部版本 19041 及更高版本)或 Windows 11 才能使用以下命令。 如果使用的是更早的版本,请参阅手动安装页。

安装 WSL 命令

现在,可以使用单个命令安装运行 WSL 所需的一切内容。 在管理员模式下打开 PowerShell 或 Windows 命令提示符,方法是右键单击并选择“以管理员身份运行”,输入 wsl –install 命令,然后重启计算机。

wsl --install

将 WSL 2 设置为默认版本

wsl --set-default-version 2

更改默认安装的 Linux 发行版

默认情况下,安装的 Linux 分发版为 Ubuntu。 可以使用 -d 标志进行更改。

  • 若要更改安装的发行版,请输入:wsl --install -d <Distribution Name>。 将 <Distribution Name> 替换为要安装的发行版的名称。
  • 若要查看可通过在线商店下载的可用 Linux 发行版列表,请输入:wsl --list --onlinewsl -l -o
  • 若要在初始安装后安装其他 Linux 发行版,还可使用命令:wsl --install -d <Distribution Name>

WSl常用命令

# 列出可用的 Linux 发行版
wsl --list --online
# 列出已安装的 Linux 发行版
wsl -l -v 
# 或者wsl --list --verbose
#############################################################
#  NAME                   STATE           VERSION
#* docker-desktop         Stopped         2
#  Ubuntu                 Stopped         2
#  docker-desktop-data    Stopped         2
#  CentOS7                Stopped         2
  
# 设置默认 Linux 发行版
wsl --set-default CentOS7

# 更新 WSL
wsl --update
# 检查 WSL 状态
wsl --status

# 通过 PowerShell 或 CMD 运行特定的 Linux 发行版
wsl --distribution CentOS7 --user root

# 更改发行版的默认用户
<DistributionName> config --default-user <Username>

# 关闭
wsl --shutdown  

# 导入和导出发行版
# 导出路径默认在C:\Users\Golden下
#wsl --export <Distribution Name> <FileName>
wsl --export CentOS7 CentOS7-Origion.tar.gz
#wsl --import <Distribution Name> <InstallLocation> <FileName>
wsl --import CentOS7-Origion D:\WSL\Centos7-Origion C:\Users\Golden\CentOS7-Origion.tar.gz
# 注销或卸载 Linux 发行版
wsl --unregister CentOS7-Origion

WSL安装Centos

具体参考windows10在WSL2中安装Centos发行版+git+miniconda+vscode

在Centos中,默认使用不了systemctl,会报Failed to get D-Bus connection: Operation not permitted
解决办法:

vim /etc/wsl.conf
## 加入以下内容
[boot]
systemd=true

执行:wsl.exe --shutdown
再次启动wsl即可正常使用

报错:/usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link

cd /usr/lib/wsl
sudo mkdir lib2
sudo ln -s lib/* lib2
sudo ldconfig

修改/etc/ld.so.conf.d/ld.wsl.conf
/usr/lib/wsl/lib 改为
/usr/lib/wsl/lib2
最后,在/etc/wsl.conf中

[automount]
ldconfig = false

阻止 WSL 自动生成/etc/hosts 文件

阻止WSL自动生成/etc/hosts文件
之前修改了/etc/hosts文件,但是重启wsl之后,发现该文件又被还原成之前的内容了
通过查看wsl内的/etc/hosts文件,发现文本里有以下内容:

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false

原来wsl会自动生成这一文件,并且它给出了停止自动生成的方法:

sudo vim /etc/wsl.conf
# 添加以下内容:
[network]
generateHosts = false

WSL每次重启后,都会自动生成hostname,也可以在这里设置:

# 添加以下内容:
[network]
generateHosts = false
hostname = golden-02

具体可以参考WSL 中的高级设置配置

安装并配置oh-my-posh

具体参考【保姆级教程】使用oh-my-posh美化Windows终端

配置themes,并设置命令自动补全

  1. 在Power Shell里执行$PROFILE,查看配置文件在哪
  2. Win +r 输入上面的地址:C:\Users\Golden\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1以打开文件
    oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\catppuccin_mocha.omp.json" | Invoke-Expression 
    Import-Module posh-git # 引入 posh-git
    Import-Module oh-my-posh # 引入 oh-my-posh
    # Set-PSReadLineOption -PredictionSource History # 设置预测文本来源为历史记录
    Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete # 设置 Tab 键,命令行自动补全和提示
    Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录
    Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # 设置向下键为前向搜索历史纪录
    这里使用了catppuccin_mocha主题,所有主题可以在Themes查看

配置插件具体可以查看oh-my-posh - 终端个性化工具 - 美化PowerShell / cmd
字体下载可参考:nerdfonts

在Centos配置oh-my-zsh

需要先安装git

yum install git
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
wget https://gitee.com/heyuanfly/install-oh-my-zsh/raw/master/centos-install-oh-my-zsh.sh

centos-install-oh-my-zsh.sh里的内容:

yum install -y vim  
yum install -y zsh
yum install -y git
wget https://gitee.com/heyuanfly/ohmyzsh/raw/master/tools/install.sh
chmod +x install.sh
./install.sh

具体可以参考CentOS安装Oh my zsh
剩下的就是跟mac安装ohmyzsh是一样的,可以看另一篇博客Mac使用笔记(一)

安装完ls后的颜色不对,可执行文件是红色的

dircolors --print-database > ~/.dir_colors
vim .zshrc
# 在.zshrc里添加以下内容
alias ls='ls -F --show-control-chars --color=auto' 
eval `gdircolors -b $HOME/.dir_colors` 

Failed to get D-Bus connection: Operation not permitted

具体参考Failed to get D-Bus connection: Operation not permitted

Ubuntu配置oh-my-posh

安装 oh-my-posh

wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-amd64 -O /usr/local/bin/oh-my-posh
chmod +x /usr/local/bin/oh-my-posh

配置

mkdir -p /mnt/c/Users/Golden/.posthemes
wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/themes.zip -O  /mnt/c/Users/Golden/.posthemes/themes.zip
unzip /mnt/c/Users/Golden/.posthemes/themes.zip -d  /mnt/c/Users/Golden/.posthemes
chmod u+rw /mnt/c/Users/Golden/.posthemes/*.json
rm  /mnt/c/Users/Golden/.posthemes/themes.zip

注意,这里如果是wsl里的ubuntu,一定要选择 /mnt/c/Users/Golden/下的路径,否则会报如下错误:
Ubuntu安装oh-my-posh报错1
具体可以参考CONFIG ERROR

如果您使用的是 Ubuntu 18.04 或 16.04,则需要先安装正确版本的 golang:

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update

在.bashrc里添加以下配置:

eval "$(oh-my-posh --init --shell bash --config /mnt/c/Users/Golden/.posthemes/catppuccin_mocha.omp.json)"

在ubuntu也安装字体

##不存在该文件时新建
sudo mkdir /usr/share/fonts/ttf
##下载好的文件放入ttf文件中
sudo cp *.ttf /usr/share/fonts/ttf
##给予权限
cd /usr/share/fonts/ttf
sudo chmod 744 *
##重新生成索引
sudo mkfontscale
sudo mkfontdir
sudo fc-cache -f -v

最后发现,在oh-myposh没办法跟oh-my-zsh一样,设置各种插件。所以这里还是放弃,改用在ubuntu使用oh-my-zsh

Ubuntu配置oh-my-zsh

zsh安装与配置

  1. 确定当前使用的shell,默认为bash:
    echo $SHELL
    /bin/bash
  2. 确定是否安装了zsh:
    cat /etc/shells 
    /bin/sh
    /bin/bash
    /bin/rbash
    /bin/dash
    /bin/zsh
    # 如果没有安装:
    sudo apt install zsh
  3. 将shell由bash换为zsh:
    chsh -s /bin/zsh

    上述过程只需要重启terminal,不需要重启服务器。

oh my zsh安装

在zsh的基础上,安装oh-my-zsh:

wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

修改.zshrc:

export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="arrow"
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)
source /etc/profile
source $ZSH/oh-my-zsh.sh
alias ls='ls -F --show-control-chars --color=auto'
eval `dircolors -b $HOME/.dir_colors`

安装插件

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

这里下载可能有些需要梯子,幸好之前设置了clash