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

- Link: https://www.ixiqin.com/2022/06/16/for-your-server-with-custom-login-screen/
- Published: 2022-06-16T06:00:00+08:00
- Author: 白宦成

文章目录

 [Toggle](https://www.ixiqin.com/2022/06/16/for-your-server-with-custom-login-screen/#)

 * [1. 分析现有的 Message 格式](https://www.ixiqin.com/2022/06/16/for-your-server-with-custom-login-screen/#1_%e5%88%86%e6%9e%90%e7%8e%b0%e6%9c%89%e7%9a%84_message_%e6%a0%bc%e5%bc%8f)
 * [2. 修改 motd 和 header](https://www.ixiqin.com/2022/06/16/for-your-server-with-custom-login-screen/#2_%e4%bf%ae%e6%94%b9_motd_%e5%92%8c_header)
    - [修改 motd](https://www.ixiqin.com/2022/06/16/for-your-server-with-custom-login-screen/#%e4%bf%ae%e6%94%b9_motd)
    - [修改 header](https://www.ixiqin.com/2022/06/16/for-your-server-with-custom-login-screen/#%e4%bf%ae%e6%94%b9_header)
    - [根据需要自定义](https://www.ixiqin.com/2022/06/16/for-your-server-with-custom-login-screen/#%e6%a0%b9%e6%8d%ae%e9%9c%80%e8%a6%81%e8%87%aa%e5%ae%9a%e4%b9%89)

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

![](https://files.ixiqin.com/2022/06/23093455/d2b5ca33bd970f64a6301fa75ae2eb22-25.
png)

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

![](https://files.ixiqin.com/2022/06/23093455/d2b5ca33bd970f64a6301fa75ae2eb22-29.
png)

## 1. 分析现有的 Message 格式

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

![](https://postimg.aliavv.com/mbp16/xb55q.png)

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

    ```shcb-line-numbers
    touch ~/.hushlogin
    ```

## 2. 修改 motd 和 header

### 修改 motd

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

![](https://postimg.aliavv.com/mbp16/l9p67.png)

### 修改 header

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

![](https://postimg.aliavv.com/mbp16/2czq5.png)

比如，`00-header` 文件的源码如下

    ```php
    #!/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)"
    Code language: PHP (php)
    ```

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

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

    ```php
    #!/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"
    Code language: PHP (php)
    ```

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

    ```javascript
    #!/bin/bash
    printf "\u1b[31mwelcome to ixiqin.com production server\n"
    Code language: JavaScript (javascript)
    ```

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

执行完成后的结果如下

![](https://postimg.aliavv.com/mbp16/6wek6.png)

### 根据需要自定义

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

![](https://files.ixiqin.com/2022/06/23093455/d2b5ca33bd970f64a6301fa75ae2eb22-28.
png)

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