Jump to content

Create custom Message Of The Day (motd) CentOS 7


rev.dennis

14 views

Yea a long title for something pretty simple

So what the heck is the Message Of The Day on CentOS?

Well it gives the ability to provide information to users who log into your server.

FIRST we have the banner which is defined in your sshd_config

sudo vi /etc/ssh/sshd_config

Browse or Find Banner and mine looks like this

Banner /etc/login.warn

So if you open up the banner (/etc/login.warn) it looks pretty straight forward and simple which you can customize that you like

*************************************************************
**               NETWORK TEAM JUMP-TEST SERVER             **
**          (accounts are local, no AD integration)        **
**                                                         **
**        All connections are monitored and recorded       **
**Disconnect IMMEDIATELY if you are not an authorized user!**
*************************************************************
login:

Once you authenticate to the linux server with your credentials you may be presented with the Message Of The Day (motd)

[SSH] Logged in (password)

Last login: Thu Nov 19 15:28:05 2020 from 66.14.206.254

===============================================
 - Hostname............: usdet1cent.thezah.corp
 - Operating System....: CentOS Linux release 7.9.2009 (Core)
 - Disk Space..........: 703M remaining
===============================================
 - CPU usage...........: 0.05, 0.07, 0.05 (1, 5, 15 min)
 - Memory used.........: 3152 MB / 3789 MB
 - Swap in use.........: 521 MB
===============================================
 - Users Logged on.....:
dhosang
=============================================== 

To create something like the above to be displayed once logged in, create a file called login-info.sh (for example since you can name it whatever you want) and put it under /etc/profile.d/ since all scripts in this directory will run once a user logs in.

cat /etc/profile.d/login-info.sh
#! /usr/bin/env bash

# Basic info
HOSTNAME=`uname -n`
OSVER=`cat /etc/centos-release`
ROOT=`df -Ph | grep sda1 | awk '{print $4}' | tr -d '\n'`
OLUSRS=`who | cut -d' ' -f1 | sort | uniq`

# System load
MEMORY1=`free -t -m | grep Total | awk '{print $3" MB";}'`
MEMORY2=`free -t -m | grep "Mem" | awk '{print $2" MB";}'`
LOAD1=`cat /proc/loadavg | awk {'print $1'}`
LOAD5=`cat /proc/loadavg | awk {'print $2'}`
LOAD15=`cat /proc/loadavg | awk {'print $3'}`

echo "
===============================================
 - Hostname............: $HOSTNAME
 - Operating System....: $OSVER
 - Disk Space..........: $ROOT remaining
===============================================
 - CPU usage...........: $LOAD1, $LOAD5, $LOAD15 (1, 5, 15 min)
 - Memory used.........: $MEMORY1 / $MEMORY2
 - Swap in use.........: `free -m | tail -n 1 | awk '{print $3}'` MB
===============================================
 - Users Logged on.....:
$OLUSRS
===============================================
"

Here is another example..  maybe the above one is to busy so you may prefer a scaled down version like this one

        - CentOS Linux release 7.9.2009 (Core)
        - Kernel 3.10.0-1127.19.1.el7.x86_64

System information as of: Thu Nov 19 16:43:41 EST 2020

System load:    0.08    IP Address:     10.6.0.136/27
Memory usage:   0.0%    System uptime:  16 days
Usage on /:     13%     Swap usage:     0.0%
Local Users:    4       Processes:      448

So the process is the same..  add the .sh file that has been marked excutable by running something like chmod +x /etc/motd.sh

Here is code you would use to get the above

#!/bin/sh
#
clear
#figlet -f slant $(hostnamectl --pretty)
printf "\n"
printf "\t- %s\n\t- Kernel %s\n" "$(cat /etc/redhat-release)" "$(uname -r)"
printf "\n"

date=`date`
load=`cat /proc/loadavg | awk '{print $1}'`
root_usage=`df -h / | awk '/\// {print $(NF-1)}'`
memory_usage=`free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { printf("%3.1f%%", used/total*100)}'`
swap_usage=`free -m | awk '/Swap/ { printf("%3.1f%%", "exit !$2;$3/$2*100") }'`
users=`users | wc -w`
time=`uptime | grep -ohe 'up .*' | sed 's/,/\ hours/g' | awk '{ printf $2" "$3 }'`
processes=`ps aux | wc -l`
ethup=$(ip -4 ad | grep 'state UP' | grep -v virbr0 | awk -F ":" '!/^[0-9]*: ?lo/ {print $2}')
ip=$(ip ad show dev $ethup |grep -v inet6 | grep inet|awk '{print $2}')

echo "System information as of: $date"
echo
printf "System load:\t%s\tIP Address:\t%s\n" $load $ip
printf "Memory usage:\t%s\tSystem uptime:\t%s\n" $memory_usage "$time"
printf "Usage on /:\t%s\tSwap usage:\t%s\n" $root_usage $swap_usage
printf "Local Users:\t%s\tProcesses:\t%s\n" $users $processes
echo

[ -f /etc/motd.tail ] && cat /etc/motd.tail || true

Hope that helps

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...