Useful Commands and Tips for Working with Alpine Linux
- Last updated: Jul 1, 2024
In this article, I'll be sharing a few tips on using Alpine Linux, a GNU/Linux distribution renowned for its small size, simplicity, and security. Unlike most other distributions, it doesn't use systemd which is quite rare these days. I've started using it for several different applications and services, and I must say I've been rather taken with its qualities. I'll be using this page as a reminder, in the hope that it will be useful to others.
Useful Commands
- Update Alpine Linux:
root@host:~# apk add --upgrade apk-tools && apk upgrade --available
- Install package:
root@host:~# apk add vim
Python
- Install
python3:
root@host:~# apk add python3
- Add
pip:
Create and activate a virtual environment:
host:~$ python -m venv .web3
host:~$ source .web3/bin/activate
Install pip in the virtual environment and check the version:
(.web3) host:~$ python -m ensurepip
(.web3) host:~$ python -m pip --version
Cron
Using Cron in Alpine Linux is quite special, especially if you're used to using Debian. Let's see how it works. For this example, let's say wa want to configure crond to run a python script (/root/script.py in this case) every 5 minutes.
- Create a
/etc/periodic/5minfolder:
root@host:~# mkdir /etc/periodic/5min
- Add an entry in the
/etc/crontabs/rootfile for the/etc/periodic/5minfolder:
*/5 * * * * run-parts /etc/periodic/5min
- Create a
/etc/periodic/5min/pscript(do not use the.shextension) shell script:
#!/bin/sh
/root/script.py
- Make the file executable :
root@host:~# chmod +x /etc/periodic/5min/pscript
- Run a test:
root@host:~# run-parts --test /etc/periodic/5min/
- Take into account modifications:
root@host:~# rc-service crond start && rc-update add crond
Installing VMware Tools
If you're installing Alpine Linux in a virtual machine in a VMware ESXi environment, it's recommended to install VMware Tools for optimum performance and functionality.
- Edit
/etc/apk/repositoriesand uncomment the community line:
#/media/cdrom/apks
http://dl-cdn.alpinelinux.org/alpine/v3.19/main
http://dl-cdn.alpinelinux.org/alpine/v3.19/community
- Install the
open-vmtoolspackages:
root@host:~# apk add open-vm-tools open-vm-tools-guestinfo open-vm-tools-deploypkg
- Start the
open-vmtoolsservice:
root@host:~# rc-service open-vm-tools start
- Enable
open-vmtoolsat boot:
root@host:~# rc-update add open-vm-tools boot
How to upgrade Alpine Linux
Let's assume we want to upgrade Alpine Linux version from 3.19 to 3.20.
- First, update the current Alpine Linux version:
root@host:~# apk add --upgrade apk-tools && apk upgrade --available
- Edit the
/etc/apk/repositoriesfile and replace 3.19 number with 3.20:
#/media/cdrom/apks
http://dl-cdn.alpinelinux.org/alpine/v3.20/main
http://dl-cdn.alpinelinux.org/alpine/v3.20/community
- Run the upgrade:
root@host:~# apk update && apk upgrade --available
- Restart system:
root@host:~# reboot
nftables
- Install
nftables:
root@host:~# apk add nftables
- Restart:
root@host:~# reboot
- Edit
/etc/nftables.nftand define your own rules:
#!/usr/sbin/nft -f
# vim: set ts=4 sw=4:
# You can find examples in /usr/share/nftables/.
# Clear all prior state
flush ruleset
#IPv4
table ip filter {
chain input {
type filter hook input priority 0; policy accept;
}
chain forward {
type filter hook forward priority 0; policy accept;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
#IPv6
table ip6 filter {
chain input {
type filter hook input priority 0; policy accept;
}
chain forward {
type filter hook forward priority 0; policy accept;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
- Configure the
nftablesservice to start automatically at boot:
root@host:~# rc-update add nftables boot