Install Alpine Linux on your Linode
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
DeprecatedThis guide has been deprecated and is no longer being maintained.
Alpine Linux is a small, security-oriented Linux distro. It’s regularly updated with security patches, and runs on the grsecurity kernel. All binaries are statically linked and built against musl libc.
Before You Begin
Familiarize yourself with Lish, as most of this guide will require an out-of-band connection.
Installing Alpine in this manner will destroy all existing data on the installation target disks. Back up all data on the disks which you intend to install Alpine on.
The initial portion of this guide involves creating the disk images, so you make sure you have plenty of free space for them. A minimal installation of Alpine requires less than 1 GB, but depending on your needs, you may want to allow for more.
This guide assumes a consistent present working directory, meaning all commands should be run from the same directory. In most cases, it will be
/alpine
or a chroot of said directory.
Prepare the Linode
Disk Images
In this section, we’ll create the disk images necessary to install Alpine Linux. Although it is not strictly necessary, giving each disk a descriptive name upon creation will make it easier to keep track of its role in the system.
Log in to the Linode Manager and select the Linode to install Alpine Linux on.
Create your boot disk image by selecting Create a new Disk under the Disks section. The size should be between 128 and 256 MB, and the type should be ext4.
Note Boot drives (disk images) will need to store your kernel and your initramfs. Currently, Alpine will need about 21 MB for each kernel/initramfs combination, so even 128 MB is enough for several kernel versions, just remember to keep an eye on the available storage in/boot
when you perform upgrades of the kernel.Create your root disk image with as much space as you need, although if you want a swap disk image, make sure you leave room for it. The root disk image should be of the ext4 type as well.
Optionally, create a swap disk image with type swap.
Note Between 256 and 512 MB of swap is a good estimate. Many sources recommend much more than this, but a new installation of Alpine will use less than 50 MB of RAM when fully booted.
Configuration Profile
Create a new configuration profile for your Linode. Choose GRUB 2 from the kernel menu, which will use the GRUB 2 bootloader to boot Alpine’s kernel from your disk image. Set your boot disk image as /dev/sda
, your root disk image as /dev/sdb
, and your swap disk image, if you created one, as /dev/sdc
.
Turn off all the Filesystem/Boot Helpers. The rest of the settings can be left at their default values.
Install Alpine Linux
Boot into Rescue Mode
From the Linode Cloud Manager, boot your Linode into Rescue Mode, with your boot disk image as
/dev/sda
, your root disk image as/dev/sdb
, and your swap as /dev/sdc
.Once the Linode has booted, connect to it via Lish. If you are not familiar with Lish, there is a simple web interface for it in the Linode Cloud Manager. Access it by clicking the Launch Console link from the Linode’s dashboard.
Mount Drives
Create the
/alpine
directory. This will serve as a mount point for the root disk image:mkdir /alpine
Mount the root disk to the new
/alpine
directory and navigate to it:mount /dev/sdb /alpine cd /alpine
Create a
boot
directory and mount the boot disk image to it:mkdir boot mount /dev/sda /alpine/boot
Download APK Tools
Update the CA Certificates package so
curl
can verify the download:update-ca-certificates
Select your desired Alpine Linux release. In most cases, you can use the latest stable release
Identify the current version of the
apk-tools-static
package. You will need to navigate into themain/x86_64
directory of your chosen release in your web browser.For example, the latest stable version’s
apk-tools-static
package can be found athttp://nl.alpinelinux.org/alpine/latest-stable/main/x86_64/
. From there, simply search forapk-tools-static
. Once you’ve found it, copy the file’s location. To do this in most browsers, right click the filename and select Copy Link Address.Download and extract the
apk-tools-static
package to your Linode. You should still be working in the/alpine
directory when performing this step. Replaceaddress
in the following command with the address you copied in the previous step:curl -s address | tar xz
Perform the initial distro installation. This will use the latest stable build of Alpine:
./sbin/apk.static --repository https://nl.alpinelinux.org/alpine/latest-stable/main/ --update-cache --allow-untrusted --root /alpine --initdb add alpine-base alpine-mirrors
This should output a series of installation messages, followed by a message showing how many packages were installed and their sizes.
System Configuration
In this section, we will modify critical system files. It is recommended that you make backup copies before making changes.
Configure your filesystem table (fstab), entering a single hard tab between each column. This file specifies how each disk is initialized or mounted into the overall filesystem:
- File: /alpine/etc/fstab
1 2 3
/dev/sdb / ext4 defaults,noatime 0 0 /dev/sda /boot ext4 defaults,noatime 0 1 /dev/sdc swap swap defaults 0 0
Uncomment the line below to enable a serial console output. This gives you visibility over Lish when booting the installed system.
- File: /alpine/etc/inittab
1 2
# Put a getty on the serial port ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100
Create the GRUB 2 boot configuration directory:
mkdir /alpine/boot/grub
Create a new file,
grub.cfg
within this directory, and add the following contents. This file specifies configuration options for GRUB 2 to use during the boot process:- File: /alpine/boot/grub/grub.cfg
1 2 3 4 5 6 7 8
set root=(hd0) set default="Alpine Linux" set timeout=0 menuentry "Alpine Linux" { linux /vmlinuz-vanilla root=/dev/sdb modules=sd-mod,usb-storage,ext4 console=ttyS0 quiet initrd /initramfs-vanilla }
Create a
mkinitfs
directory:mkdir /alpine/etc/mkinitfs
Create a new file,
mkinitfs.conf
, within this directory and add the following contents. This file specifies options for building the initial RAM file system (initramfs):- File: /alpine/etc/mkinitfs/mkinitfs.conf
1
features="ata ide scsi virtio base ext4"
Copy the recovery system’s
resolv.conf
file into/alpine/etc
. Optionally, you can fill in your own choice of DNS resolvers.cp /etc/resolv.conf /alpine/etc
If you want to allow root logins over Lish, add
ttyS0
tosecuretty
:echo ttyS0 >> /alpine/etc/securetty
Install the Kernel
Bind the
/proc
and/dev
directories to the/alpine
directory:mount --bind /proc /alpine/proc mount --bind /dev /alpine/dev
Enter the Alpine chroot jail:
chroot /alpine /bin/sh
Select a mirror to use when downloading or updating packages:
setup-apkrepos
You can select a mirror by entering its corresponding number, or
f
to automatically pick the fastest mirror.Update your packages:
apk update
Set your hostname, replacing
example
with a hostname of your choice:setup-hostname -n example
Configure important services to start automatically using Alpine’s OpenRC init system:
rc-update add networking boot rc-update add urandom boot rc-update add crond
If you’ll need other services, you can also add them now. The above is intended to serve as a starting point.
Install the kernel:
apk add linux-vanilla
Exit the chroot jail:
exit
Configure Alpine Linux
Reboot into Alpine
Reboot into the Alpine Linux configuration you made earlier by selecting the button next to the profile in the Linode Manager and clicking Reboot. If this is the only configuration profile, this can also be accomplished directly in Lish using the
boot 1
command. If there are other profiles, you can substitute1
with the list position of your Alpine profile.Note During boot, the output in Lish that networking fails to start. This is expected, and will be fixed shortly.Use Lish to log in as
root
. You will not be prompted for a password since it has not yet been set.
Configuration
Set up and start networking. Alpine has a handy script that will configure the network interface file for you and guide you through the various options. It’s capable of advanced configuration, like bridging and bonding, but for a basic setup, its defaults should be sufficient.
setup-interfaces
Press enter 3 times to accept the defaults of
eth0
,dhcp
, andno
, then restart the networking service:service networking restart
Your Linode should now have a functional network connection.
Set a root password:
passwd
Create a limited user account to avoid using root for all commands. Replace
example-user
with a username of your choice:adduser example-user
Install the
sudo
package:apk add sudo
Add your new user to the
wheel
group:adduser example-user wheel
Allow the
wheel
group to temporarily elevate their privileges with sudo:sed -i '/%wheel/s/^# //' /etc/sudoers
Install and configure the SSH daemon (SSHD). Alpine has a simple setup script to handle this:
setup-sshd
We recommend the
openssh
server if you want full SFTP access.dropbear
is a more lightweight option, although it only provides SSH access.
Next Steps
At this point, you should be able to connect to your server via SSH. Alpine is very lightweight, and doesn’t install very much unless you ask it to.
Install Packages
Alpine’s website provides a searchable library of available packages.
A few packages to consider:
- Text editor:
nano
orvim
- Web server:
lighttpd
,apache2
, ornginx
- Scripting languages:
php
,perl
, orpython
- Database servers:
mysql
orpostgresql
Note that some of these combinations may require additional dependencies. To install a new package, use the following command, replacing package
with the package name(s):
apk add package
For example, to add Apache, PHP, and MySQL:
apk add apache2 php mysql
For more information, see Alpine’s wiki page on package management.
Secure Your Server
Before using your Linode in a development or production capacity, make sure you’ve taken some basic security precautions. Our
Securing Your Server guide provides a good starting point but you should also research additional, Alpine-specific security options. Keep in mind that most security packages (e.g. iptables
.) will need to be installed.
For more information, refer to their wiki page on security.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on