Win11环境下折腾WSL2中的Debian11

系统前置设置

启用系统功能

应用后重启系统。
更新 WSL 2 Linux 内核,跳过前面细节直接下载安装补丁

wsl --set-default-version 2

上面将 WSL 2 设置为默认版本,对于已安装的WSL可以行执行wsl --set-version Debian 2即可将Debian升级到WSL2版。建议全新安装

安装Debian

下载Debian
下载完的*.AppxBundle文件用压缩工具打开,如下图:

平台选择

解压后的*.appx依旧用压缩工具打开,正常解压到单独文件夹即可:
Debian解压

解压后双击debian.exe安装。

WSL升级Debian到12(Bookworm)_非必要

配置软件源

清华源参考, 中科大源参考, 其它源参考
这一步需要用到一些vi的快捷操作方式: a i o, dd d0 y0 p, :wq :q!

sudo cat > /etc/apt/sources.list << EOF
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free

deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free

deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free

deb http://mirrors.tuna.tsinghua.edu.cn/debian-security/ bullseye-security main contrib non-free
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security/ bullseye-security main contrib non-free
EOF

apt-get安装和卸载命令

apt-get update:更新安装列表
apt-get upgrade:升级软件
apt-get install software_name :安装软件
apt-get --purge remove software_name :卸载软件及其配置
apt-get autoremove software_name:卸载软件及其依赖的安装包
dpkg --list:罗列已安装软件

启用 systemd

#cmd命令下:
wsl --update #先更新wsl
wsl --version #查看版本 >0.67.6
# wsl命令下:
sudo echo -e "[boot]\nsystemd=true" | sudo tee -a /etc/wsl.conf
# 重启wsl后生效
# 验证生效否
ps --no-headers -o comm 1

旧版wsl启用systemd

sudo apt install apt-transport-https

sudo wget -O /etc/apt/trusted.gpg.d/wsl-transdebian.gpg https://arkane-systems.github.io/wsl-transdebian/apt/wsl-transdebian.gpg

sudo chmod a+r /etc/apt/trusted.gpg.d/wsl-transdebian.gpg

sudo cat << EOF > /etc/apt/sources.list.d/wsl-transdebian.list
deb https://arkane-systems.github.io/wsl-transdebian/apt/ bullseye main
deb-src https://arkane-systems.github.io/wsl-transdebian/apt/ bullseye main
EOF

sudo apt update
sudo apt -y install systemd-genie
# 启动
wsl genie -s

命令行连接WiFi

# 普通wifi 用nmtui图形化操作,或者用如下命令
nmcli dev wifi connect "WiFi的名字" password "WiFi的密码"
# 隐藏wifi
nmcli c add type wifi con-name "自定义连接名称" ifname "无线网卡名称" ssid "WiFi的名字"
nmcli con modify "自定义连接名称" wifi-sec.key-mgmt wpa-psk
nmcli con modify "自定义连接名称" wifi-sec.psk "WiFi的密码"
nmcli con up "自定义连接名称"
# 同步时间的命令
ntpd -d -q -n -p ntp3.aliyun.com

Debian in WSL 安装python

apt安装方式

sudo apt-get install python3
python3 -V    # check version of python3
sudo apt-get install python3-pip

安装Miniconda

下载Linux下的安装包:64位32位

sudo bash Miniconda3-latest-Linux-x86_64.sh
#一路Enter,一路yes... 
#最后选择路径时注意,默认的/root/anaconda3可能权限问题,装完了无法使用
Miniconda3 will now be installed into this location:
/root/miniconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/miniconda3] >>>/home/$USER/anaconda3    #自定义安装路径
#装完后编辑/home/$USER/.bashrc,追加一行
echo 'export PATH="$PATH:/home/$USER/anaconda3/bin"' >> /home/$USER/.bashrc
#软连接
sudo ln -s /opt/anaconda3/bin/python /usr/bin/python
sudo ln -s /opt/anaconda3/bin/pip /usr/bin/pip
sudo ln -s /opt/anaconda3/bin/conda /usr/bin/conda

重新启动shell,设置conda源;批量更新 conda update conda

软连接的操作是为了解决sudo python提示找不到命令的错误

补足WSL中的ps命令

解决deb包的依赖问题

  1. 执行完dpkg命令后,执行sudo apt-get -f install
  2. 安装gdebi,之后执行sudo gdebi package.deb会自动安装上依赖文件

直接运行sudo apt-get install procps 会遇到各种依赖版本不适用的问题,所以采用安装deb包的方式补足:procps_3.3.15-2_amd64.deb,依赖libprocps7_3.3.15-2_amd64.deb
使用sudo gdebi package.deb依次安装libprocps7procps即可补足。
补足后测试效果:ps aux|grep python

安装SSH

方便使用WinSCP连接管理Debian系统的文件

# 查找已装
sudo whereis openssh-server
# 安装
sudo apt-get install openssh-server
#  - 修改SSH Server的监听端口,这里修改为33
sudo sed -i '/Port /c Port 33' /etc/ssh/sshd_config
#  - 修改SSH Server的监听地址
sudo sed -i '/ListenAddress 0.0.0.0/c ListenAddress 0.0.0.0' /etc/ssh/sshd_config
#  - 修改SSH Server允许使用用户名密码的方式登录
sudo sed -i '/PasswordAuthentication /c PasswordAuthentication yes' /etc/ssh/sshd_config
#  - 修改SSH Server允许远程root用户登录
sudo sed -i '/PermitRootLogin /c PermitRootLogin yes' /etc/ssh/sshd_config
# - 重启ssh服务
sudo service ssh restart

SSH自动启动

在文件~/.profile末尾追加一行:sudo service ssh start

sudo sed -i '$a sudo service ssh start' ~/.profile

免密sudo

username自行修改

sudo vi /etc/sudoers.d/passwordless
# 文件内容
username    ALL=(ALL) NOPASSWD:ALL 

计划任务

# 查看
crontab -l
# 编辑
crontab -e  # /var/spool/cron/root
reboot   # 普通重启
shutdown -r now  # 立刻重启(root用户使用)
shutdown -r 10   # 10分钟后重启
shutdown -r 20:35   # 在时间为20:35的时候重启
init 6
# 重启服务
service crond restart
# 装载
crontab /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

30 02 * * * root reboot  #02:30 每天凌晨2点30分重启系统
* */6 * * * root reboot  #每隔6小时重启一次
*/30 * * * * root reboot #每隔30分钟重启一次
30 02 1 * * root reboot #每月1日02:30 时重启服务器
0 0 */2 * * root reboot #每2天00点重启一次服务器
crontab.png

WinSCP提权

# 查看sftp-server执行文件目录:
cat /etc/ssh/sshd_config|grep sftp
Subsystem       sftp    /usr/lib/openssh/sftp-server
# 编辑/etc/sudoers为特定用户添加执行sftp sudo权限:
# 修改为可写
chmod +w /etc/sudoers
vi /etc/sudoers
# 添加如下字段
username    ALL=NOPASSWD:  /usr/lib/openssh/sftp-server
# 再次修改为只读
chmod -w /etc/sudoers
WinSCP会话-高级设置

静态IP

Windows下修改虚拟网卡vEthernet (WSL)地址:

netsh interface ipv4 set address "vEthernet (WSL)" static 172.20.176.1 255.255.240.0

wsl中修改~/.bashrc

# static ip
if [ "$(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1)" != "172.20.176.29/20" ]; then
    sudo ip addr del $(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1) dev eth0
    sudo ip addr add 172.20.176.29/20 broadcast 172.20.176.255 dev eth0
    sudo ip route add 0.0.0.0/0 via 172.20.176.1 dev eth0
fi

END

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容