Table of contents
You guys must be wondering what is the technical part here, and why this blog called as "Message of the Day",
The Message of the Day (MOTD)
It is the first message users see after logging into a Linux machine. You can use it to display anything you want.
Lets take an example when i login to my Kali Linux, a default message pops up like below,
It's a cool way to display some important information or just welcome note for users. I will take you through steps to setup a simple MOTD and make it fancy with dynamic system information.
Important things to know before getting started -
/etc/motd
- It is a file on Unix-like systems that contains the message of the day to be displayed to users when they login.
/etc/profile.d
- It is a directory in Unix-like systems that contains scripts executed by the shell when any user logs in. These scripts are typically used to set environment variables, aliases, and other shell settings for all users on the system.
Step-by-Step Guide to Setting Up a MOTD
Logging in and Checking Current MOTD
First things first, let’s log into the server and check what our current MOTD is.
C:\Users\gupta> ssh ajeet@192.168.50.146
ajeet@192.168.50.146's password:
Last login: Thu Jul 18 16:57:35 2024 from 192.168.50.74
[ajeet@centos1 ~]$ cat /etc/motd
[ajeet@centos1 ~]$
The MOTD file is currently blank.
Setting Up a Basic MOTD
We'll use a text editor like vi or nano to setup up a basic MOTD.
[root@centos1 ~]# vi /etc/motd
Add the content of your choice , save the file and exit the text editor. Let’s verify if MOTD updated.
[root@centos1 ~]# cat /etc/motd
Welcome to Linux Dev Server,
If you are not an authorized user, kindly exit and report to support.
Thanks,
Now, when we login to new session we'll get a customized MOTD.
Customizing the MOTD with Dynamic Content
Write a script that will display our dynamic system information in the MOTD.
[root@centos1 ~]# touch /etc/profile.d/motd.sh
[root@centos1 ~]# cd /etc/profile.d
[root@centos1 profile.d]# ls -l
total 100
-rw-r--r--. 1 root root 0 Jul 18 17:29 motd.sh
Edit the script file:
[root@centos1 profile.d]# vi motd.sh
Add the script of your choice to motd.sh
, I'm using a script to display basic things like OS release, kernel version, memory usage, network information, etc. :
#!/bin/bash
#
#
echo -e "
#######################################
#
# Welcome to $(hostname), you logged in as $(whoami).
# If you are not an authorized user, kindly logout and contact support; else check below.
#
# System Information:
# --------------------
# Operating System: $(cat /etc/redhat-release)
# Kernel Version: $(uname -r)
# Uptime: $(uptime -p)
# Total Users Logged In: $(who | wc -l)
# Current Date and Time: $(date)
#
# Disk Usage:
# -----------
# $(df -h | grep '^Filesystem')
#
# Memory Usage:
# -------------
# $(free -h | grep 'Mem:')
#
# Network Information:
# ---------------------
# IP Address: $(hostname -I | awk '{print $1}')
#
# Please ensure to follow the company policies while using this system.
#
#######################################
"
Save and close the file. Ensure the script has execute permissions:
[root@centos1 ~]# chmod +x /etc/profile.d/motd.sh
Backup and Update SSH Configuration
It is always suggested to take backup of configuration file before making any changes.
[root@centos1 ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
Make sure the backup is there:
[root@centos1 ~]# ls -l /etc/ssh/sshd_config*
-rw-------. 1 root root 3667 Jun 3 16:06 /etc/ssh/sshd_config
-rw-------. 1 root root 3667 Jul 18 17:35 /etc/ssh/sshd_config.bak
Edit the SSH configuration file to ensure MOTD is displayed:
[root@centos1 ~]# vi /etc/ssh/sshd_config
Ensure the following line is present and not commented out:
PrintMotd yes
Restart the SSH service to apply changes:
bashCopy code[root@centos1 ~]# systemctl restart sshd.service
Verify the Updated MOTD
Login using an SSH client such as PuTTY and verify the updated MOTD. You will notice that you are now presented with the custom dynamic system information MOTD message.
Conclusion
This guide explains how to set up and customize the MOTD on Linux server, which will display message banners containing useful system information to users upon initial login (and any time they open a new terminal).