在 WSL 中配置开发环境

本帖记录了一些我常用的 WSL 设置。

安装常用包

Ubuntu:

sudo apt install build-essential

Debian:

sudo apt install gcc g++

Debian Common:

sudo apt install gdb valgrind git gpg curl wget zip unzip zsh

设置 Zsh

/bin/zsh 设置成默认 shell

chsh -s /bin/zsh

安装 oh-my-zsh 和常用插件

wget https://gist.githubusercontent.com/dingwen07/1261eac531f7f080d72b78f931a8ca01/raw/omz-setup.sh
chmod +x ./omz-setup.sh
./omz-setup.sh

编辑 ~/.zshrc 启用插件

plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
)

Java 环境

一般使用 Home | SDKMAN! the Software Development Kit Manager 管理 Java 环境。需要安装 unzip 包。

curl -s "https://get.sdkman.io" | bash

为 WSL 配置 Windows Terminal

为 CLI 程序开启鼠标滚动功能

默认情况下 WSL 中的 CLI 程序比如 lessnano 不支持鼠标滚动,需要手动设置。设置完成之后,鼠标操作将默认由应用程序接收,如需选中对应程序中的内容,需要按住 Shift 键然后再进行选择。

less (包括 man

将下面的内容保存为文本文件:

#env
LESS = --mouse

然后运行 lesskey <filename>(较新的发行版可以忽略这一步,将文本保存到 ~/.lesskey 即可)。

nano

~/.nanorc 中添加:

set mouse

vi[m]

~/.vi[m]rc 中添加:

set mouse=a

SSH Agent

使用 npiperelay.exe 将 Windows 套接字 //./pipe/openssh-ssh-agent 转发到 WSL12

首先需要确保 Windows 的 PATH 里有 npiperelay.exe,这样 WSL 内部就可以直接访问。可以通过 WinGet 安装。之后在 WSL 的系统里安装 socat

sudo apt install socat

然后将这一段脚本写入 ~/.win-ssh-agent/agent-bridge.sh

# Code extracted from https://stuartleeks.com/posts/wsl-ssh-key-forward-to-windows/ with minor modifications

# Configure ssh forwarding
export SSH_AUTH_SOCK=$HOME/.win-ssh-agent/agent.sock
# need `ps -ww` to get non-truncated command for matching
# use square brackets to generate a regex match for the process we want but that doesn't match the grep command running it!
ALREADY_RUNNING=$(ps -auxww | grep -q "[n]piperelay.exe -ei -s //./pipe/openssh-ssh-agent"; echo $?)
if [[ $ALREADY_RUNNING != "0" ]]; then
    if [[ -S $SSH_AUTH_SOCK ]]; then
        # not expecting the socket to exist as the forwarding command isn't running (http://www.tldp.org/LDP/abs/html/fto.html)
        echo "removing previous socket..."
        rm $SSH_AUTH_SOCK
    fi
    echo "Starting SSH-Agent relay..."
    # setsid to force new session to keep running
    # set socat to listen on $SSH_AUTH_SOCK and forward to npiperelay which then forwards to openssh-ssh-agent on windows
    (setsid socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork &) >/dev/null 2>&1
fi

之后在 .bashrc 或者 .zshrc 中添加

source $HOME/.win-ssh-agent/agent-bridge.sh

就可以了。

GnuPG

参照 在 WSL2 中使用 GPG 命令行工具以及对 Git commit 进行签名

1Password CLI

使用 op 命令行进行方便且安全的凭据存取,首先在 Windows 中安装:

winget install AgileBits.1Password.CLI

之后在 .bashrc 或者 .zshrc 中添加如下内容:

# Aliases
alias op="op.exe"
# alias sudo="sudo -S <<< $(op read "op://Personal/Account Password/password")"

# Completions
eval "$(op completion zsh)"; compdef _op op.exe

参考

  1. Use 1Password SSH Agent in WSL – DEV Community ↩︎
  2. Forwarding SSH Agent requests from WSL to Windows – stuartleeks.com ↩︎

Mac 时间机器“检测到备份无法可靠恢复”的解决方法

最近备份到Mac mini网络磁盘的时间机器备份出现了无法完成的情况,似乎是在一次内核崩溃之后出现的。

具体的报错信息为:

时间机器检测到“目标”上的备份无法可靠恢复。时间机器必须抹掉现有的备份历史记录并创建新的备份来修复此问题。
Time Machine detected that your backups on “destination” can not be reliably restored. Time Machine must erase your existing backup history and start a new backup to correct this.

出现这个提示并不代表备份已经损坏,但系统并不允许进行新的备份。

如果不想抹掉备份历史记录,可以尝试在网络共享中找到备份磁盘映像(sparsebundle),这个时候磁盘映像文件可能是处于“锁定”状态,按Command + I打开详情将磁盘映像解锁。

然后右键-显示包内容,其中的“token”文件可能也会被锁定,同样解锁它。

然后使用Xcode打开“com.apple.TimeMachine.MachineID.plist”文件,将键“VerificationState”的值从2修改为1并保存。

之后再次重试时间机器备份并验证备份,如果能够成功验证那么备份就没有损坏。

退出移动版