众所周知,万恶的RedHat于2020年12月宣布放弃CentOS项目的开发,而其中最受国内服务器欢迎的版本CentOS 7也于2024年6月30日结束技术支持。时至今日,通常情况下的正常人应当不会考虑用WSL配置一个过时落伍的CentOS7。很不幸,笔者所需要使用的计算集群仍然在使用古早得令人落泪的CentOS7.5.1804,并且禁止联网。为了便于在近似的环境下载和调试所需要的软件和依赖,笔者不得不尝试在WSL2中配置CentOS7.5。 下面把折腾的过程记录下来,以防遗忘或有其他的倒霉蛋。

WSL配置

一切的开始是确保电脑中有WSL:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --install
wsl --set-default-version

一些博客提到需要下载WSL2内核更新包并安装,但实操中发现没有影响。

检查WSL安装情况:

wsl -v

安装LxRunOffline

管理WSL的程序,可以通过Chocolatey或Scoop安装。实践中发现Choco无法正常安装,未深入检查。
安装Chocolatey:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Chocolatey安装LxRunOffline:

choco install lxrunoffline

安装Scoop:

irm get.scoop.sh | iex

Scoop安装LxRunOffline:

scoop bucket add extras
scoop install lxrunoffline

下载对应版本的CentOS docker镜像

如前所述,CentOS7已于2024 年 6 月 30 日结束支持,这意味着其软件仓库也相应被上游移除,docker-hub上的镜像也被删除。不过GitHub上有早期镜像备份:https://github.com/CentOS/sig-cloud-instance-images
找到对应分支,Git clone到本地:

git clone -b CentOS-7.5.1804 --single-branch https://github.com/CentOS/sig-cloud-instance-images.git

我们需要的是名为centos-7-docker.tar.xz的文件。

安装CentOS到WSL

万事俱备,只欠东风。

LxRunOffline install -n CentOS7 -d {安装路径} -f {镜像下载路径}/centos-7-docker.tar.xz


检查安装结果:

wsl -l -v


实践时发现安装为WSL1版本了,转换到WSL2:

wsl --set-version CentOS7 2


设置为默认发行版, 启动!

wsl -s CentOS7
wsl -d CentOS7

配置CentOS

事情到这里并没有结束,这里的镜像几乎是最简版本,也即缺失了大量常用的命令。
然而更折磨的是,前面提到CentOS已经停止支持,这意味着系统自带的yum源全部失效。我们还需要做一些额外的配置来安装这些命令。
注意到这个版本没有预装wget,先做一点小小的准备工作 (当然后面用curl也可以):

curl -O https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/wget-1.14-18.el7_6.1.x86_64.rpm​
rpm -ivh wget-1.14-18.el7_6.1.x86_64.rpm​

修改yum源

现在我们可以下载国内镜像源或CentOS7-Vault源的yum源配置文件了:

cd /etc/yum.repos.d/​
mkdir repo_bak​
mv *.repo repo_bak/​
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo​
wget https://mirrors.wlnmp.com/centos/Centos7-163-altarch.repo -P /etc/yum.repos.d/
wget https://mirrors.wlnmp.com/centos/Centos7-tencent-altarch.repo -P /etc/yum.repos.d/
wget https://mirrors.wlnmp.com/centos/Centos7-ustc-altarch.repo -P /etc/yum.repos.d/
yum clean all​
yum makecache​

如果需要epel源:

yum install -y epel-release​
wget http://mirrors.aliyun.com/repo/Centos-7.repowget http://mirrors.aliyun.com/repo/Centos-7.repo​
yum clean all​
yum makecache​

之后就可以从yum包管理器来安装其他命令了。
到这里基本配置结束。后面是笔者所偏好的一些其他工具和美化。

初步美化Terminal

令提示符显示格式为:user@host:/current/directory$

sed -i '$a PS1="\\[\\033[01;32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ "' ~/.bashrc​
source ~/.bashrc​

安装一些其他常用命令

wget https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.sh​ 
chmod +x Anaconda3-2024.10-1-Linux-x86_64.sh && sh Anaconda3-2024.10-1-Linux-x86_64.sh
yum install git make autoconf automake libtool ncurses-devel gcc man -y
git clone https://github.com/hishamhm/htop.git 
cd htop/ 
./autogen.sh && ./configure
make -j$(nproc) && make install​
curl -o /etc/yum.repos.d/konimex-neofetch-epel-7.repo https://copr.fedorainfracloud.org/coprs/konimex/neofetch/repo/epel-7/konimex-neofetch-epel-7.repo​
yum install neofetch​ -y

zsh

最近觉得zsh更为美观,配合语法高亮和自动建议插件能够提升不少效率。这里记录安装zsh的方法和一些基本配置:

git clone --depth=1 https://github.com/zsh-users/zsh.git 
cd zsh
./Util/preconfig 
./configure 
make -j$(nproc) && make install 
chsh -s /bin/zsh​ #注意在工作环境中不要设置为默认shell
sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"​
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k 
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.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting​

修改.zshrc里的对应配置项

vim ~/.zshrc​
ZSH_THEME="powerlevel10k/powerlevel10k"
plugins=(git zsh-autosuggestions zsh-syntax-highlighting z extract)​

这样就安装了zsh,使用oh-my-zsh管理配置和插件。主题目前笔者很喜欢powerlevel10k,启用了语法高亮、自动补全、快速跳转路径、通用解压和git命令缩写这几个插件,用起来确实很方便。