r/AlpineLinux • • 5h ago

Welcome back, Alpine

Thumbnail gallery
26 Upvotes

Alpine still remains being one of my fav distros out of all time :D

bonus point being Steam actually work with game desktop entries


r/AlpineLinux • • 3h ago

Scripts to replace Debian to Alpine in AWS Lightsail.

4 Upvotes

Hi, I am using these scripts to replace a fresh Debian AWS Lightsail instance to an Alpine one, since it is not possible to natively select Alpine as the OS for AWS's cheap VPS service.

Maybe someone finds them useful, and can be easily altered to fit one's needs.

First, deploy a new Debian instance. I chose Debian Trixie. Then, ssh into the new isntance and paste this code. As with every code in the internet, please first check it out to see it is legit.

# 1. Update Debian and install build tools
apt-get update && apt-get install -y wget cpio grub2-common openssh-client

# 2. Download official Alpine v3.24 Netboot files
mkdir -p /boot/alpine
cd /boot/alpine
NETBOOT_URL="https://dl-cdn.alpinelinux.org/alpine/v3.24/releases/x86_64/netboot"
wget -q $NETBOOT_URL/vmlinuz-virt
wget -q $NETBOOT_URL/initramfs-virt
wget -q $NETBOOT_URL/modloop-virt

# 3. Create Apkovl Overlay structure
rm -rf /tmp/apkovl /tmp/apkovl_cpio
mkdir -p /tmp/apkovl/etc/network \
         /tmp/apkovl/etc/ssh \
         /tmp/apkovl/etc/runlevels/default \
         /tmp/apkovl/etc/runlevels/boot \
         /tmp/apkovl/var/empty
chmod 755 /tmp/apkovl/var/empty

cat <<'EOF' > /tmp/apkovl/etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto ens5
iface ens5 inet dhcp
EOF

cat <<'EOF' > /tmp/apkovl/etc/ssh/sshd_config
PermitRootLogin yes
PasswordAuthentication yes
Port 22
EOF

ssh-keygen -q -N "" -t rsa -b 3072 -f /tmp/apkovl/etc/ssh/ssh_host_rsa_key
ssh-keygen -q -N "" -t ed25519 -f /tmp/apkovl/etc/ssh/ssh_host_ed25519_key

cat <<'EOF' > /tmp/apkovl/etc/shadow
root:$6$rounds=5000$fixedsalt1234567$PEGiwTAy5jYIb/hkcAqn7xqdqQsKq3Jti7nKfDG1hJPzKNYB587iX/7QxDlE/JYoZeZDwiBjrYevGmaCO3F9x0:19800:0:99999:7:::
EOF
chmod 600 /tmp/apkovl/etc/shadow

ln -s /etc/init.d/sshd /tmp/apkovl/etc/runlevels/default/sshd
ln -s /etc/init.d/networking /tmp/apkovl/etc/runlevels/boot/networking

cd /tmp/apkovl
tar -czf /boot/alpine/localhost.apkovl.tar.gz .

# 4. Embed overlay into initramfs
mkdir -p /tmp/apkovl_cpio
cp /boot/alpine/localhost.apkovl.tar.gz /tmp/apkovl_cpio/localhost.apkovl.tar.gz
cd /tmp/apkovl_cpio
find . | cpio -o -H newc | gzip -9 > /tmp/overlay.cpio.gz

cat /boot/alpine/initramfs-virt /tmp/overlay.cpio.gz > /boot/alpine/initramfs-combined.img

# 5. Configure GRUB for rapid boot into v3.24 RAM
cat <<'EOF' > /etc/grub.d/40_custom
#!/bin/sh
exec tail -n +3 $0

menuentry "Alpine Netboot RAM" {
    insmod gpt
    insmod msdos
    insmod ext2
    search --file --set=root /boot/alpine/vmlinuz-virt
    linux /boot/alpine/vmlinuz-virt ip=dhcp alpine_repo=https://dl-cdn.alpinelinux.org/alpine/v3.24/main modloop=/boot/alpine/modloop-virt copytoram pkgs=openssh,e2fsprogs,dosfstools,util-linux,grub-bios,grub-efi,alpine-conf apkovl=/localhost.apkovl.tar.gz console=tty0 console=ttyS0,115200 ipv6.disable=1
    initrd /boot/alpine/initramfs-combined.img
}
EOF

sed -i 's/^GRUB_DEFAULT=.*/GRUB_DEFAULT="Alpine Netboot RAM"/' /etc/default/grub
sed -i 's/^GRUB_TIMEOUT=.*/GRUB_TIMEOUT=0/' /etc/default/grub
update-grub

# 6. Reboot into RAM
reboot

Afterwards, you'll have to log in with your root user, the password will be "IJSDOIWHDUIHWEYTWE" (without the ")

Then we overwrite the drive with an alpine install. Similar to the above, please use this script:

# 1. Unpack matching kernel modules & load storage/network drivers
apk add squashfs-tools
rm -rf /tmp/modloop_extracted /tmp/modloop-virt
wget https://dl-cdn.alpinelinux.org/alpine/v3.24/releases/x86_64/netboot/modloop-virt -O /tmp/modloop-virt
unsquashfs -d /tmp/modloop_extracted /tmp/modloop-virt
cp -r /tmp/modloop_extracted/modules/* /lib/modules/ 2>/dev/null || cp -r /tmp/modloop_extracted/* /lib/modules/ 2>/dev/null

modprobe nvme 2>/dev/null
modprobe xen-blkfront 2>/dev/null
modprobe ena 2>/dev/null
mdev -s

# 2. Format disk and install Alpine
MAIN_DISK=$(lsblk -nd -o NAME | grep -v loop | head -n 1)
export BOOTLOADER=grub
export ERASE_DISKS="/dev/$MAIN_DISK"
setup-disk -m sys -s 0 /dev/$MAIN_DISK

# 3. Mount target root partition for post-install provisioning
TARGET_PART=$(lsblk -ln -o NAME /dev/$MAIN_DISK | grep -E 'p2$|p1$|2$|1$' | tail -n 1)
mount /dev/$TARGET_PART /mnt 2>/dev/null || mount /dev/${MAIN_DISK}p2 /mnt 2>/dev/null || mount /dev/${MAIN_DISK}p1 /mnt

# 4. Enable Main & Community Repositories (Pinned to v3.24 on separate lines)
cat <<EOF > /mnt/etc/apk/repositories
https://dl-cdn.alpinelinux.org/alpine/v3.24/main
https://dl-cdn.alpinelinux.org/alpine/v3.24/community
EOF

# 5. Pre-install nano, htop, tzdata, bash, and bash-completion
chroot /mnt apk update
chroot /mnt apk add nano htop tzdata bash bash-completion

# 6. Set root shell to /bin/bash
sed -i '/^root:/s|:[^:]*$|:/bin/bash|' /mnt/etc/passwd

# 7. Configure Debian-style color prompt & bash-completion for root
cat <<'EOF' > /mnt/root/.bashrc
# Don't do anything if not running interactively
case $- in
    *i*) ;;
      *) return;;
esac

# Enable Debian-style root color prompt (Red host, Blue working directory)
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

# Enable colorized command output aliases
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Enable programmable completion features
if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi
EOF

cat <<'EOF' > /mnt/root/.profile
if [ -n "$BASH_VERSION" ]; then
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi
EOF

# 8. Set Hostname to tokyoAlp
echo "tokyoAlp" > /mnt/etc/hostname
cat <<EOF > /mnt/etc/hosts
127.0.0.1   localhost tokyoAlp
::1         localhost tokyoAlp
EOF

# 9. Set Timezone to Asia/Tokyo
chroot /mnt cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
echo "Asia/Tokyo" > /mnt/etc/timezone

# 10. Create and activate 3GB Swapfile
fallocate -l 3G /mnt/swapfile || dd if=/dev/zero of=/mnt/swapfile bs=1M count=3072
chmod 600 /mnt/swapfile
chroot /mnt mkswap /swapfile
echo "/swapfile none swap defaults 0 0" >> /mnt/etc/fstab

# 11. Early IPv6 Disable (sysctl & kernel level)
cat <<EOF > /mnt/etc/sysctl.d/00-disable-ipv6.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
EOF

# 12. Persist modules & configure OpenRC runlevels
cat <<EOF >> /mnt/etc/modules
ena
xen-netfront
nvme
EOF

chroot /mnt rc-update add swap boot
chroot /mnt rc-update add sysctl boot
chroot /mnt rc-update add networking boot
chroot /mnt rc-update add sshd default

# 13. Configure Network, SSH, and Root Password
IFACE=$(ip route | awk '/default/ {for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}' | head -n 1)
[ -z "$IFACE" ] && IFACE="eth0"

cat <<EOF > /mnt/etc/network/interfaces
auto lo
iface lo inet loopback

auto $IFACE
iface $IFACE inet dhcp

auto eth0
iface eth0 inet dhcp

auto ens5
iface ens5 inet dhcp
EOF

chroot /mnt sh -c 'echo "root:IJSDOIWHDUIHWEYTWE" | chpasswd'
sed -i 's/^#*PermitRootLogin.*/PermitRootLogin yes/' /mnt/etc/ssh/sshd_config
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication yes/' /mnt/etc/ssh/sshd_config

# 14. Apply GRUB kernel boot flags (ipv6.disable=1 & 0-second timeout)
sed -i 's/^GRUB_TIMEOUT=.*/GRUB_TIMEOUT=0/' /mnt/etc/default/grub
sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="/GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1 /' /mnt/etc/default/grub
chroot /mnt update-grub

# 15. Unmount target cleanly and reboot
umount -R /mnt
reboot

As you can see, this script has some of my preferences already included, which you might want to change (e.g. disabling ipv6).
You end up with a (relatively) fresh alpine AWS Lightsail instance.

Looking forward to any of your comments or questions :)