- CentOS 7 Linux Server Cookbook(Second Edition)
- Oliver Pelz Jonathan Hobson
- 670字
- 2021-07-23 14:28:52
Customizing your system banners and messages
In this recipe, 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.
Getting ready
To complete this recipe, you will require a minimal installation of the CentOS 7 operating system with root privileges and a console-based text editor of your choice.
How to do it...
- 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 recipe by opening the file, /etc/motd
, which stands for 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 back ticks 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 ###################################"
- PHP 從入門到項(xiàng)目實(shí)踐(超值版)
- JavaScript語言精髓與編程實(shí)踐(第3版)
- 我的第一本算法書
- OpenCV 3和Qt5計(jì)算機(jī)視覺應(yīng)用開發(fā)
- 編譯系統(tǒng)透視:圖解編譯原理
- Swift細(xì)致入門與最佳實(shí)踐
- Node.js Design Patterns
- SQL 經(jīng)典實(shí)例
- 軟件測試教程
- 計(jì)算機(jī)應(yīng)用技能實(shí)訓(xùn)教程
- 從零開始學(xué)Android開發(fā)
- 監(jiān)控的藝術(shù):云原生時代的監(jiān)控框架
- UI設(shè)計(jì)基礎(chǔ)培訓(xùn)教程(全彩版)
- Android高級開發(fā)實(shí)戰(zhàn):UI、NDK與安全
- Data Manipulation with R(Second Edition)