Configure your server with a custom login interface

Original link: https://www.ixiqin.com/2022/06/16/for-your-server-with-custom-login-screen/

I myself use Ubuntu 20.04 on the server. Every time I log in, Ubuntu will display a series of information, which is redundant and can’t see the difference on multiple servers, and can’t be very friendly to remind me which server this is.

So, I decided to customize it to my liking.

1. Analyze the existing Message format

If you want to modify it, you need to analyze the composition first. The output shown in my screenshot can be divided into three parts: header, motd and last login message

I am mainly dissatisfied with the header and motd parts. Although I think the last login message is a bit redundant, it can indeed be used for reminders in many scenarios. So I don’t hide it. However, if you want to hide the last login, you can create a .hushlogin file in the current user’s root directory to close the last login message

 touch ~/.hushlogin

2. Modify motd and header

modify motd

The full name of motd is Message of Today, and its content is stored in /etc/motd . If you want to modify the text of Motd, you only need to directly modify the content of the /etc/motd file. However, it should be noted that the motd file does not support formatting, and only supports displaying the default text. So you can draw attention by putting blank lines in it.

modify header

The Header part is stored in the /etc/update-motd.d/ directory. You can implement different styles of motd by modifying the files in this folder. The file name format is organized in the way of优先级-文件名, and you can create files with appropriate priority according to your own needs.

For example, the source code of the 00-header file is as follows

 #!/bin/sh## 00-header - create the header of the MOTD# Copyright (C) 2009-2010 Canonical Ltd.## Authors: Dustin Kirkland <[email protected]>## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License along# with this program; if not, write to the Free Software Foundation, Inc.,# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.[ -r /etc/lsb-release ] && . /etc/lsb-releaseif [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then# Fall back to using the very slow lsb_release utilityDISTRIB_DESCRIPTION=$(lsb_release -s -d)fiprintf "Welcome to %s (%s %s %s)\n" "$DISTRIB_DESCRIPTION" "$(uname -o)" "$(uname -r)" "$(uname -m)"

The valuable code is mainly the output of the last few lines.

Similarly, the code for 10-help-text prints the text.

 #!/bin/sh## 10-help-text - print the help text associated with the distro# Copyright (C) 2009-2010 Canonical Ltd.## Authors: Dustin Kirkland <[email protected]>,# Brian Murray <[email protected]>## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License along# with this program; if not, write to the Free Software Foundation, Inc.,# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.printf "\n"printf " * Documentation: https://help.ubuntu.com\n"printf " * Management: https://landscape.canonical.com\n"printf " * Support: https://ubuntu.com/advantage\n"

And if we want to control ourselves, we can write a file by ourselves, such as 01-custom , and add the following code to it (this code can generate a piece of output with red characters on a black background to remind me that this is a production environment server), And execute chmod +x /etc/update-motd.d/01-custom to set executable permissions.

 #!/bin/bashprintf "\u1b[31mwelcome to ixiqin.com production server\n"

It should be noted here that the file in the /etc/update-motd.d directory is essentially an executable file, you can use any shell such as bash / dash to implement it, or consider using Node.js, Python, etc. script to achieve. This way you don’t have to use Terminal Color Code to change the color like I did.

The result after execution is as follows

Customize as needed

When you find the location and configuration of the specific files, you can adjust the contents of the files in /etc/motd and /etc/update-motd.d according to your own needs to achieve the desired effect.

In this way, the transformation of the login interface of the Shell is completed.

This article is reprinted from: https://www.ixiqin.com/2022/06/16/for-your-server-with-custom-login-screen/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment

Your email address will not be published.