Dynamic MOTD on Alpine Linux

Last updated on .

Alpine Linux doesn't have by default which is the PAM module for updating the MOTD.

It can be installed and setup to work similarly to Debian, it comes with the linux-pam apk package. However, this is a bit overkill just to update the MOTD. An easy solution is to simply run a script on a timer to update the /etc/motd file periodically.

After making sure crond is running (rc-service crond start && rc-update add crond), you can add a script to /etc/periodic/15min/motd such as a similar one to mine below.


C_RESET=$(echo -e "\e[0m")
C_RED=$(echo -e "\e[31m")
C_GREEN=$(echo -e "\e[32m")
C_YELLOW=$(echo -e "\e[33m")
C_BLUE=$(echo -e "\e[34m")
C_MAGENTA=$(echo -e "\e[35m")
C_CYAN=$(echo -e "\e[36m")

UPTIME_SECONDS=$(cat /proc/uptime | cut -d '.' -f1)
UPTIME_MINUTES=$((UPTIME_SECONDS % 86400 % 3600 / 60))

IP=$(ip -o -4 addr show dev eth0 | cut -d' ' -f7 | cut -d'/' -f1)

cat > /etc/motd << EOF
${C_BLUE}Welcome to ${HOSTNAME} (${C_GREEN}${IP}${C_BLUE})!

This system is running ${C_CYAN}$(uname -sr)${C_BLUE}, and
has been online for ${C_MAGENTA}${UPTIME_DAYS} days${C_BLUE}, ${C_MAGENTA}${UPTIME_HOURS} hours${C_BLUE}, ${C_MAGENTA}${UPTIME_MINUTES} minutes${C_BLUE}.${C_RESET}

Of course, this method has its pitfalls since it is 15 minutes out of date at worst, but I think it's good enough for most users of Alpine who want something lightweight anyway.

Output of my script for one of my servers.

I'm pretty happy with how mine looks, and it's a bit refreshing vs. the MOTD from the standard installation.