Commit 4065cce405
Verified · cmc
Layout: unified · split
content/blog/2026-06-01-macos-motd.org added +132
| @@ -0,0 +1,132 @@ | ||
| 1 | #+date: [2026-06-01 Mon 12:02:02] | |
| 2 | #+title: Bring MOTD to macOS | |
| 3 | #+description: | |
| 4 | #+slug: macos-motd | |
| 5 | #+filetags: :mac: | |
| 6 | ||
| 7 | * Messages of the Day (=motd=) | |
| 8 | ||
| 9 | A [[https://en.wikipedia.org/wiki/Message_of_the_day][message of the day]] is, in computer terms, a welcome message that provides | |
| 10 | information and may help users logging onto the system. This is useful for | |
| 11 | numerous reasons, depending on the system, such as community announcements, | |
| 12 | compliance requirements, contextual information for that system, etc. | |
| 13 | ||
| 14 | In this post, I'll be walking through a simple way to generate a message of the | |
| 15 | day for macOS. You can see the end result in this screenshot below: | |
| 16 | ||
| 17 | #+caption: macOS message of the day | |
| 18 | #+attr_html: :alt Terminal output of motd.sh showing the system and user information upon launching a new shell. | |
| 19 | [[https://img.cleberg.net/blog/20260601-macos-motd/macos-motd.webp]] | |
| 20 | ||
| 21 | For an example of a system-provided =motd=, here's the =motd= on my Ubuntu server | |
| 22 | showing the server's default information and any relevant add-ons, such as the | |
| 23 | LAN cockpit URL: | |
| 24 | ||
| 25 | #+caption: Ubuntu message of the day | |
| 26 | #+attr_html: :alt Terminal output of Ubuntu's default server message of the day, showing system information. | |
| 27 | [[https://img.cleberg.net/blog/20260601-macos-motd/ubuntu-motd.webp]] | |
| 28 | ||
| 29 | * Create the Message | |
| 30 | ||
| 31 | First, we need to create the message. In this example, I've created a custom | |
| 32 | shell script below that uses terminal formatting to generate a structured | |
| 33 | output. This script checks for: | |
| 34 | ||
| 35 | - username | |
| 36 | - hostname | |
| 37 | - profile management | |
| 38 | - WiFi SSID | |
| 39 | - IP address | |
| 40 | - Mullvad status | |
| 41 | - battery status | |
| 42 | - uptime | |
| 43 | ||
| 44 | To do this, create a script in your user directory: | |
| 45 | ||
| 46 | #+begin_src shell | |
| 47 | nano ~/.motd.sh | |
| 48 | #+end_src | |
| 49 | ||
| 50 | Then paste the content below and modify as needed: | |
| 51 | ||
| 52 | #+begin_src shell | |
| 53 | BOLD='\033[1m' | |
| 54 | DIM='\033[2m' | |
| 55 | RESET='\033[0m' | |
| 56 | ||
| 57 | HOST=$(scutil --get ComputerName 2>/dev/null || hostname) | |
| 58 | ||
| 59 | IP=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null) | |
| 60 | IP=${IP:-unavailable} | |
| 61 | ||
| 62 | SSID=$(networksetup -getairportnetwork en0 2>/dev/null | sed 's/^Current Wi-Fi Network: //') | |
| 63 | case "$SSID" in | |
| 64 | ""|"You are not associated with an AirPort network.") | |
| 65 | SSID="unavailable" | |
| 66 | ;; | |
| 67 | esac | |
| 68 | ||
| 69 | BATTERY=$(pmset -g batt 2>/dev/null | awk -F'; *' ' | |
| 70 | NR==2 { | |
| 71 | gsub(/^ +| +$/, "", $2) | |
| 72 | gsub(/^ +| +$/, "", $3) | |
| 73 | sub(/ present: true$/, "", $3) | |
| 74 | print $2 ", " $3 | |
| 75 | } | |
| 76 | ') | |
| 77 | BATTERY=${BATTERY:-unavailable} | |
| 78 | ||
| 79 | UPTIME=$(uptime | sed 's/^.*up //; s/, [0-9]* user.*//; s/, load averages.*//') | |
| 80 | UPTIME=${UPTIME:-unavailable} | |
| 81 | ||
| 82 | if profiles status -type enrollment 2>/dev/null | grep -qi "MDM enrollment: Yes"; then | |
| 83 | MANAGED="yes" | |
| 84 | else | |
| 85 | MANAGED="no" | |
| 86 | fi | |
| 87 | ||
| 88 | VPN="unavailable" | |
| 89 | ||
| 90 | if command -v mullvad >/dev/null 2>&1; then | |
| 91 | STATUS=$(mullvad status 2>/dev/null) | |
| 92 | VPN=$(printf '%s\n' "$STATUS" | head -1) | |
| 93 | VPN=${VPN:-unavailable} | |
| 94 | fi | |
| 95 | ||
| 96 | printf "\n" | |
| 97 | printf " ${DIM}%-14s${RESET}${BOLD}%s${RESET} ${DIM}%s${RESET}\n" "$USER" "$(uname -sr)" | |
| 98 | printf " ${DIM}%-14s${RESET}%s\n" "host" "$HOST" | |
| 99 | printf " ${DIM}%-14s${RESET}%s\n" "managed" "$MANAGED" | |
| 100 | printf " ${DIM}%-14s${RESET}%s\n" "wifi" "$SSID" | |
| 101 | printf " ${DIM}%-14s${RESET}%s\n" "ip" "$IP" | |
| 102 | printf " ${DIM}%-14s${RESET}%s\n" "mullvad" "$VPN" | |
| 103 | printf " ${DIM}%-14s${RESET}%s\n" "battery" "$BATTERY" | |
| 104 | printf " ${DIM}%-14s${RESET}%s\n" "uptime" "$UPTIME" | |
| 105 | printf "\n" | |
| 106 | #+end_src | |
| 107 | ||
| 108 | Finally, modify the script file to enable execution: | |
| 109 | ||
| 110 | #+begin_src sh | |
| 111 | chmod +x ~/.motd.sh | |
| 112 | #+end_src | |
| 113 | ||
| 114 | * Load the Message | |
| 115 | ||
| 116 | At this point, the script is ready. We simply need to tell macOS to execute it | |
| 117 | whenever the user launches a new shell. | |
| 118 | ||
| 119 | To do this, open the =.zprofile= file add a line referencing the script: | |
| 120 | ||
| 121 | #+begin_src shell | |
| 122 | nano ~/.zprofile | |
| 123 | #+end_src | |
| 124 | ||
| 125 | #+begin_src | |
| 126 | # Display Message of the Day (MOTD) | |
| 127 | ./motd.sh | |
| 128 | #+end_src | |
| 129 | ||
| 130 | All done! This method is easily customizable and allows for use of dotfile | |
| 131 | management systems, such as stow, since the files live in the user's home | |
| 132 | directory. | |