为你的服务器配上自定义的登录界面

为你的服务器配上自定义的登录界面

我自己在服务器上使用的是 Ubuntu 20.04,每次在登录时,Ubuntu 都会展示一系列的信息,冗余且在多个服务器上看不出差异,并不能非常友好的提醒我这个是哪个服务器。

于是,我决定对其进行自定义,改成我自己喜欢的样子。

1. 分析现有的 Message 格式

想要修改,就一下先分析一下组成结构。我截图所示的输出可以分为三个部分:header、motd 和 last login message

而我主要不满的是 header 和 motd 的部分,对于 last login message,我虽然觉得有些多余,但在很多场景下,确实可以用于提醒。所以我就不隐藏了。不过,如果你希望隐藏 last login 的话,可以在当前用户的根目录下创建一个 .hushlogin 文件来关闭 last login message

touch ~/.hushlogin

2. 修改 motd 和 header

修改 motd

motd 的全称是 Message of Today,他的内容被存放在 /etc/motd 当中,想要修改 Motd 的文字,只需要直接修改 /etc/motd 文件的内容即可。不过需要注意的是,motd 文件不支持做格式化,只支持展示默认的文本。因此,你可以通过在其中加入空行来引起注意。

修改 header

Header 部分存放在 /etc/update-motd.d/ 目录中,你可以通过修改这个文件夹下的文件来实现不同样式的 motd 风格。其中文件名格式为 优先级-文件名的方式进行组织,你可以根据自己的需要,创建适当优先级的文件。

比如,00-header 文件的源码如下

#!/bin/sh
#
#    00-header - create the header of the MOTD
#    Copyright (C) 2009-2010 Canonical Ltd.
#
#    Authors: Dustin Kirkland <kirkland@canonical.com>
#
#    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-release

if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
	# Fall back to using the very slow lsb_release utility
	DISTRIB_DESCRIPTION=$(lsb_release -s -d)
fi

printf "Welcome to %s (%s %s %s)\n" "$DISTRIB_DESCRIPTION" "$(uname -o)" "$(uname -r)" "$(uname -m)"

其中有价值的代码主要就是最后几行的输出。

类似的, 10-help-text 的代码则输出了文字。

#!/bin/sh
#
#    10-help-text - print the help text associated with the distro
#    Copyright (C) 2009-2010 Canonical Ltd.
#
#    Authors: Dustin Kirkland <kirkland@canonical.com>,
#             Brian Murray <brian@canonical.com>
#
#    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"

而我们自己如果想要控制,可以自己写一个文件,比如叫 01-custom ,在其中加入如下代码(这段代码可以生成一段黑底红字的输出,用于提醒我自己这是生产环境服务器),并执行 chmod +x /etc/update-motd.d/01-custom 来设置可执行权限。

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

这里需要注意的是,/etc/update-motd.d 目录下的文件本质上是一个可执行文件,你可以使用 bash / dash 等任何 Shell 来实现,也可以考虑使用 Node.js、Python 之类的脚本来实现。这样就不用像我一样,使用 Termial Color Code 来实现颜色的变化。

执行完成后的结果如下

根据需要自定义

当你找到了具体的文件的位置和配置的方式,就可以根据自己的需要来调整 /etc/motd/etc/update-motd.d 当中文件的内容,来达到你想要的效果了。

这样,就完成了对于 Shell 的登录界面的改造工作了。

为你的服务器配上自定义的登录界面》有2个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注