SSH Login Banner

Creating Custom Login Banner Page

The following guide explains how to create a simple custom login banner page that is displayed when a user logs in over ssh.
The following example is aimed at Red Hat Based servers (RHEL, CentOS, Fedora and Oracle Linux). In the example, we will show you how to add dynamic system information and colour to your login banner pages.



Adding Colour to your script


Adding colour into a script is quite simple. In the following example we will cover some of the basic colour commands that can be added into your login script.


The easiest way to use colour within your script is to set up the colours so that they can be referenced easily. The following example script illustrates how the basic colours have been defined.



#!/bin/bash
clear
#Bash Colour Codes

red="\033[00;31m"
RED="\033[01;31m"

echo -e "${red} I am Dark Red"
echo -e "${RED} I am Bright Red"


green="\033[00;32m"
GREEN="\033[01;32m"

echo -e "${green} I am Dark Green"
echo -e "${GREEN} I am Bright Green"

brown="\033[00;33m"
YELLOW="\033[01;33m"

echo -e "${brown} I am Brown"
echo -e "${YELLOW} I am Yellow"

blue="\033[00;34m"
BLUE="\033[01;34m"

echo -e "${blue} I am Dark Blue"
echo -e "${BLUE} I am Bright Blue"

magenta="\033[00;35m"
MAGENTA="\033[01;35m"

echo -e "${magenta} I am Dark Magenta"
echo -e "${MAGENTA} I am Bright Magenta"

cyan="\033[00;36m"
CYAN="\033[01;36m"

echo -e "${cyan} I am Dark Cyan"
echo -e "${CYAN} I am Bright Cyan"

white="\033[00;37m"
WHITE="\033[01;37m"

echo -e "${white} I am Grey"
echo -e "${WHITE} I am White"

#Sets No Colour
NC="\033[00m"

The above script when run will produce output on your screen as follows:


Bash Shell Colour Example

Adding System information to Login Screen


To add system information such as hostname, date, time, cpu, memory we can use some basic commands.


CPU information is held in a file called /proc/cpuinfo.

Memory information is held in a file called /proc/meminfo

We can retrieve this information by using a carefully crafted command that assigns the value to a variable that can then be easily referenced later. For example, if we wanted to display the type of CPU processor on a system we could issue a command similar to the following:



CPUMOD=$(cat /proc/cpuinfo | grep -m 1 -w 'model name' | awk -F: '{print $2}')

When the above line is executed from within a script it will assign the specified information found in the /proc/cpuinfo file to a variable called "CPUMOD". This variable can then be called from within the script. A combination of "grep and "awk" is used to extract the relevant details from the file. (An overview of grep and awk can be found by clicking the relevant link:
grep guide and awk guide


Location of ssh login script


The location of the ssh login script is /etc/profile.d/. This directory is a special area where scripts can be placed that are called when a user logs into a system. When a user logs into a system a file called "/etc/profile" is executed which defines basic default settings. It is here that we specify our custom script location (by default this is /etc/profile.d). In this example we have created a script called: motd.sh.


Custom login motd script


Location: /etc/profile.d/motd,sh

Below is our custom login script. Here we are displaying some basic information such as a "Welcome" message, hostname, cpu information, memory and swap information.



john@ubuntu01-pc:~$ cat motd.sh
 
#!/bin/bash

#Colours
red="\033[00;31m"
RED="\033[01;31m"

green="\033[00;32m"
GREEN="\033[01;32m"

brown="\033[00;33m"
YELLOW="\033[01;33m"

blue="\033[00;34m"
BLUE="\033[01;34m"

purple="\033[00;35m"
PURPLE="\033[01;35m"

cyan="\033[00;36m"
CYAN="\033[01;36m"

white="\033[00;37m"
WHITE="\033[01;37m"

NC="\033[00m"

echo -e "${WHITE}******************************************************************************"
echo -e "${WHITE}**                                                                          **"
echo -e "${WHITE}**                    Powered By Linux                                      **"
echo -e "${WHITE}**                                                                          **"
echo -e "${RED}******************************************************************************"

CPUMOD=$(cat /proc/cpuinfo | grep -m 1 -w 'model name' | awk -F: '{print $2}')
HOSTNAME=$(uname -n)
KERNEL=$(uname -r)
MEMTOTAL=$(cat /proc/meminfo | grep -m 1 -w 'MemTotal' | awk -F: '{print $2}')
MEMFREE=$(cat /proc/meminfo | grep -m 1 -w 'MemFree' | awk -F: '{print $2}')
SWAPTOTAL=$(cat /proc/meminfo | grep -m 1 -w 'SwapTotal' | awk -F: '{print $2}')
SWAPFREE=$(cat /proc/meminfo | grep -m 1 -w 'SwapFree' | awk -F: '{print $2}')

echo -e ""
echo -e "${WHITE} Welcome ${YELLOW}${USER} ${WHITE} to the LandofLinux.com"
echo -e ""

echo -e "${WHITE} Date: "`date`
echo -e ""

echo -e "${WHITE} Hostname:   ${HOSTNAME}"
echo -e "${WHITE} CPU Model: ${CPUMOD}"
echo -e ""
echo -e "${WHITE} Total Memory: ${MEMTOTAL}"
echo -e "${WHITE} Free Memory: ${MEMFREE}"
echo -e ""
echo -e "${WHITE} Swap Total:    ${SWAPTOTAL}"
echo -e "${WHITE} Swap Free:    ${SWAPFREE}"
echo -e ""

echo -e "${RED}******************************************************************************"
# Reset Terminal Colour Back to Normal
echo -e "${NC}"

NOTE: The last line of the script is used to reset the terminal colour back to normal. If you do not include this line, then the last specified colour is used within your terminal session.

Now that we have created and placed the script in the relevant directory, we can now test the script by issuing a "ssh" command connecting to our target host.


Custom ssh login banner page