Remote install Alpine Linux without VNC
远程在无VNC的VPS上安装Alpine Linux

我有一台1$的NAT VPS,内存仅为256MB,运行Debian 11有点吃力,但商家不提供Debian以外的系统,故尝试远程安装Alpine.

参考:Replacing non-Alpine Linux with Alpine remotely

完成效果:

lax6:~# neofetch 
       .hddddddddddddddddddddddh.          root@lax6 
      :dddddddddddddddddddddddddd:         --------- 
     /dddddddddddddddddddddddddddd/        OS: Alpine Linux edge x86_64 
    +dddddddddddddddddddddddddddddd+       Host: KVM RHEL 7.6.0 PC (i440FX + PIIX, 1996) 
  `sdddddddddddddddddddddddddddddddds`     Kernel: 6.1.11-0-virt 
 `ydddddddddddd++hdddddddddddddddddddy`    Uptime: 2 days, 10 hours, 24 mins 
.hddddddddddd+`  `+ddddh:-sdddddddddddh.   Packages: 185 (apk) 
hdddddddddd+`      `+y:    .sddddddddddh   Shell: ash 
ddddddddh+`   `//`   `.`     -sddddddddd   Resolution: 1024x768 
ddddddh+`   `/hddh/`   `:s-    -sddddddd   Terminal: /dev/pts/0 
ddddh+`   `/+/dddddh/`   `+s-    -sddddd   CPU: Intel i7-7700K (1) @ 4.199GHz 
ddd+`   `/o` :dddddddh/`   `oy-    .yddd   Memory: 79MiB / 226MiB 
hdddyo+ohddyosdddddddddho+oydddy++ohdddh
.hddddddddddddddddddddddddddddddddddddh.                           
 `yddddddddddddddddddddddddddddddddddy`                            
  `sdddddddddddddddddddddddddddddddds`
    +dddddddddddddddddddddddddddddd+
     /dddddddddddddddddddddddddddd/
      :dddddddddddddddddddddddddd:
       .hddddddddddddddddddddddh.

话不多说,直接上命令。我这台小鸡的分区是所有空间都在/,BIOS Leagcy启动,不存在第二个分区。

在重装前,设置好SSH Key登陆。

apt update
apt install extlinux
mkdir overlay
cd overlay
mkdir -p etc/ssh etc/network etc/runlevels/{default,boot,sysinit,shutdown} root/.ssh etc/lbu
cp -a /etc/{passwd,group,shadow,gshadow,hostname,resolv.conf,network/interfaces,ssh} etc/
cp /etc/network/interfaces etc/network
cp -a /root/.ssh/authorized_keys root/.ssh
echo "/root/.ssh" > etc/lbu/include
sed -i -e '/^root:/s:/bin/bash:/bin/ash:' etc/passwd
mkdir -p etc/apk
echo "alpine-base iproute2 openssh" > etc/apk/world
ln -s /etc/init.d/{hwclock,modules,sysctl,hostname,bootmisc,syslog} etc/runlevels/boot/
ln -s /etc/init.d/{devfs,dmesg,mdev,hwdrivers} etc/runlevels/sysinit/
ln -s /etc/init.d/{networking,sshd} etc/runlevels/default/
ln -s /etc/init.d/{mount-ro,killprocs,savecache} etc/runlevels/shutdown/

编辑etc/network/interfaces文件

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
        address 192.168.100.22
       netmask 255.255.255.0
        gateway 192.168.100.1

auto eth1
iface eth1 inet6 static
        address 2a09::2/64
        gateway 2a09::1

编辑etc/resolv.conf文件

nameserver 172.16.0.2

然后执行

tar czf ../host.apkovl.tar.gz *
cp ../host.apkovl.tar.gz /
wget https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/alpine-virt-3.18.5-x86_64.iso
#wget https://mirrors.ustc.edu.cn/alpine/latest-stable/releases/x86_64/alpine-virt-3.18.5-x86_64.iso
mkdir /cdrom
mount alpine*.iso /cdrom -o loop
cp -a /cdrom/* /

编辑/boot/extlinux.conf文件

timeout 20
prompt 1
default grsec
label grsec
  kernel /boot/vmlinuz-virt
  append initrd=/boot/initramfs-virt alpine_dev=vda1:ext4 modloop=/boot/modloop-virt modules=loop,cramfs,sd-mod,usb-storage,ext4,ipv6 quiet

然后执行

extlinux -i /boot
reboot

重启后,在/etc/apk/repositories添加软件源

https://dl-3.alpinelinux.org/alpine/v3.17/main/

unmount /media/vda1, 即可正常安装。

/etc/init.d/modloop stop
apk update
umount /media/vda1
setup-alpine

最后修改于 Wed, 15 Feb 2023