#!/bin/bash set -euo pipefail if [[ "$(id -u)" -ne 0 ]]; then echo "❌ 请以 root 身份运行此脚本" exit 1 fi USERNAME="${1:-}" if [[ -z "$USERNAME" ]]; then echo "用法: wsl-init.sh <用户名>" echo "示例: wsl-init.sh imbytecat" exit 1 fi echo "🔄 更新系统..." pacman -Syu --noconfirm if ! command -v sudo &>/dev/null; then echo "📦 安装 sudo..." pacman -S --needed --noconfirm sudo fi echo "🔐 配置 sudo 权限..." cat >/etc/sudoers.d/10-wheel <<'EOF' %wheel ALL=(ALL) NOPASSWD: ALL EOF chmod 440 /etc/sudoers.d/10-wheel echo "👤 创建用户 $USERNAME..." if id "$USERNAME" &>/dev/null; then echo "⏩ 用户 $USERNAME 已存在,确保 wheel 组成员" usermod -aG wheel "$USERNAME" else useradd -m -G wheel -s /bin/bash "$USERNAME" echo "请设置 $USERNAME 的密码:" passwd "$USERNAME"