Alpine

WSL2部署

vmware部署

  • 创建虚拟机
    • 2C1G10GB仿博客服务器
    • vnet0桥接08:00:27:49:D6:EC,继承90IP
  • 启动进live
    • 加载ISO:alpine-standard-3.23.3-x86_64.iso
    • live模式:启动虚拟机自动进live模式
    • 登录:用户root密码无
  • 临时网卡
    • 查看网卡:ip link
    • 激活网卡:ip link set eth0 up
    • 获取IP:udhcpc -i eth0
    • 验证联网:ping www.baidu.com
  • 临时增强vm(未操作)
    • 安装vmtools:apk add open-vm-tools open-vm-tools-desktop
    • 启用剪贴板:rc-service open-vm-tools start
  • 安装系统
    • 安装向导:setup-alpine
    • 不确定的都直接回车
    • 键盘布局:keyboard layout=cn两次
    • 主机名:hostname=max2alpine
    • 管理员密码:Ch1
    • 包源:清华源=13, 南京=15,自动检测速度最快的源=f
    • 硬盘用途:use=sys;erase=y(耗时很长,感觉n跳过也行)
    • 完成后reboot重启

系统初始化

  • 允许ssh登录

    vi /etc/ssh/sshd_config
    
    # 将 PermitRootLogin prohibit-password
    # 改为:
    PermitRootLogin yes
    
    rc-service sshd restart
  • 更新源

    # 用 sed 批量删除 community 行的 # 注释
    # 可以考虑注释掉官方源,同时第三方源版本改为latest-stable
    sed -i '/community/s/^#//' /etc/apk/repositories
    
    # 更新索引
    apk update
  • 安装docker

    # 安装 Docker 依赖与引擎
    apk add docker
    
    # 启动 Docker 服务并设置开机自启
    rc-update add docker boot
    
    # 验证 Docker 是否安装成功
    dockerd  # 带执行信息的调试
    
    # CentOS 的 Systemd 会自动处理依赖和清理 PID,而 Alpine 的 OpenRC需要手动处理
    # 1. 清除所有假死的服务状态(最关键)
    rc-service docker zap
    
    # 2. 挂载必要的 cgroups (CentOS 自动做,Alpine 需要手动)
    rc-update add cgroups boot
    service cgroups start
    
    # 3. 强制启动 Docker
    service docker start
    
    # 4. 确认服务状态
    service docker status
  • docker配代理

    cat > /etc/docker/daemon.json <<EOF
    {
      "proxies": {
        "https-proxy": "http://192.168.1.120:7890",
        "no-proxy": "huecker.io,ghcr.io,*.test.example.com,.example.org,127.0.0.0/8"
      }
    }
    EOF
    
    rc-service docker restart
  • 安装docker-compose

    # 自动安装
    apk add docker-compose
    
    # 手动安装:加代理的一句话安装 + 执行权限
    COMPOSE_VERSION=$(curl -s --socks5 192.168.1.120:7890 https://api.github.com/repos/docker/compose/releases/latest | grep tag_name | cut -d '"' -f4) && curl -L --socks5 192.168.1.120:7890 "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose
    
    # 验证
    docker-compose --version
  • 用户提权

    apk add sudo
    visudo # 修改sodu配置
    
    # 输入密码可执行sudo
    ghost ALL=(ALL:ALL) ALL
    # 免密不需要sudo的权限
    ghost ALL=(ALL) NOPASSWD: /bin/rm -rf /www/*, /bin/rm -f /www/*, /bin/mkdir -p /www/*, /bin/mkdir /www/*, /bin/find /www/*, /bin/tar -* /www/*, /bin/tar -* -C /, /sbin/apk, /home/ghost/backupGhost.sh, /bin/rm -rf /home/ghost/*, /bin/rm -f /home/ghost/*
    
    adduser ghost docker
    service docker restart
  • 时间同步

    # 安装NTP时间同步服务
    apk add chrony
    
    # 启动并设置开机自启
    rc-service chronyd start
    rc-update add chronyd default
    
    # 手动同步时间(立即生效)
    chronyc makestep
  • 配置ssh公钥

    mkdir -p ~/.ssh
    chmod 700 ~/.ssh  # 必须700,权限太宽会被SSH拒绝
    vi ~/.ssh/authorized_keys # 粘贴公钥
    chmod 600 ~/.ssh/authorized_keys  # 必须600
    chown -R ghost:ghost ~/.ssh  # 确保归属ghost用户
    
    # ----------一些其它设置
    vi /etc/ssh/sshd_config
    
    # 启用公钥认证
    PubkeyAuthentication yes
    
    # 禁用密码认证(全局,所有用户都不能密码登录)
    PasswordAuthentication no
    
    # (可选)只对 ghost 用户禁用密码,其他用户保留密码登录
    Match User ghost
        PasswordAuthentication no
        PubkeyAuthentication yes
  • vi支持中文

    # 自带vi 本身就不支持中文字符,安装成类似centos的大vi
    apk add vim
    rm -f /usr/bin/vi;ln -s /usr/bin/vim /usr/bin/vi
  • ll命令支持

    # root用户执行
    echo "alias ll='ls -l'" >> /etc/profile
    # ghost用户执行
    echo "alias ll='ls -l'" >> ~/.bashrc

vmware增强

  • 安装vmtools

    sudo apk update
    sudo apk add open-vm-tools
    rc-update add open-vm-tools boot # 开机自启
    service open-vm-tools start