In this process, we will learn how to display a welcome message if a user successfully logs in to our CentOS 7 system using SSH or console, or opens a new terminal window in a graphical window manager. This is often used to show the user informative messages, or for legal reasons.
To Start With: What Do You Need?
To complete this process, you will require a minimal installation of the CentOS 7 operating system with root privileges and a console-based text editor of your choice.
The Process
- To begin, log in to your system using your root user account and create the following new file with your favorite text editor:
vi /etc/motd
- Next, we will put in the following content in this new file:
###############################################
# This computer system is for authorized users only.
# All activity is logged and regularly checked.
# Individuals using this system without authority or
# in excess of their authority are subject to
# having all their services revoked…
############################################### - Save and close this file.
- Congratulations, you have now set a banner message for whenever a user successfully logs in to the system using ssh or a console.
How it works...
For legal reasons, it is strongly recommended that computers display a banner before allowing users to log in; lawyers suggest that the offense of unauthorized access can only be committed if the offender knows at the time that the access he intends to obtain is unauthorized. Login banners are the best way to achieve this. Apart from this reason, you can provide the user with useful system information.
So, what did we learn from this experience?
We started this process by opening the file, /etc/motd, which stands for a message of the day; this content will be displayed after a user logged in a console or ssh. Next, we put in that file a standard legal disclaimer and saved the file.
There's more…
As we have seen, the /etc/motd file displays static text after a user successfully logs in to the system. If you want to also display a message when an ssh connection is first established, you can use ssh banners. The banner behavior is disabled in the ssh daemon configuration file by default, which means that no message will be displayed if a user establishes an ssh connection. To enable this feature, log in as root on your server and open the /etc/ssh/sshd_config file using your favorite text editor, and put in the following content at the end of the file:Banner /etc/ssh-banner
Then, create and open a new file called /etc/ssh-banner, and put in a new custom ssh greeting message.
Finally, restart your ssh daemon using the following line:systemctl restart sshd.service
The next time someone establishes an ssh connection to your server, this new message will be printed out.
The motd file can only print static messages and some system information details, but it is impossible to generate real dynamic messages or use bash commands in it if a user successfully logs in.
Also, motd does not work in non-login shells, such as when you open a new terminal within a graphical window manager. In order to achieve this, we can create a custom script in the /etc/profile.d directory. All scripts in this directory get executed automatically if a user logs in to the system. First, we delete any content in the /etc/motd file, as we don’t want to display two welcome banners. Then, we open the new file, /etc/profile.d/motd.sh, with our text editor and create a custom message, such as the following, where we can use bash commands and write little scripts (use the backticks to run bash shell commands in this file):
#!/bin/bash
echo -e "
##################################
#
# Welcome to `hostname`, you are logged in as `whoami`
# This system is running `cat /etc/redhat-release`
# kernel is `uname -r`
# Uptime is
`uptime | sed 's/.*up ([^,]*), .*/1/'`
# Mem total `cat /proc/meminfo | grep MemTotal | awk {'print $2'}` kB
###################################"