Raspberry PI – Guide to update Bash MOTD with detailed hardware info

shell instruction as follows:

nano /home/pi/.bashrc

#add default export path for bookworm and bullseye
export PATH="/home/pi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games"

# Get Raspberry Pi model
model=$(cat /proc/device-tree/model | tr -d '\0')

# Get CPU model name
cpu_model=$(lscpu | grep "Model name" | sed -r 's/Model name:\s{1,}//')

# Get the total number of CPU cores (logical)
total_cores=$(lscpu | grep "^CPU(s):" | awk '{print $2}')

# For physical cores, you might use "Core(s) per socket" * "Socket(s)"
# Uncomment the next line to calculate physical cores if needed
# physical_cores=$(($(lscpu | grep "^Core(s) per socket:" | awk '{print $4}') * $(lscpu | grep "^Socket(s):" | awk '{print $2}')))

# Get total memory
total_mem=$(free -h | grep "Mem:" | awk '{print $2}')

# Get hostname
hostname=$(hostname)

# Get IP address (assuming connectivity via a single interface, might need adjustment for multiple interfaces)
ip_address=$(hostname -I | awk '{print $1}')

# Get OS Version
# Attempt to use lsb_release, fall back to /etc/os-release if not available
if command -v lsb_release &> /dev/null; then
    os_version=$(lsb_release -d | awk -F"\t" '{print $2}')
else
    os_version=$(grep PRETTY_NAME /etc/os-release | cut -d= -f2 | tr -d '"')
fi


# Display the information
echo "Hardware: $model, CPU: $cpu_model, Cores: $total_cores, RAM: $total_mem"
# Uncomment the next line to display physical cores
# echo "Physical CPU Cores: $physical_cores"
echo "Hostname: $hostname, IP: $ip_address"
echo "OS Version: $os_version"
echo ""

Remove MOT disclaimer

sudo nano /etc/motd
*wipe all the contents here*

Leave a comment