如何在 Linux 中配置 ssh 登录导语
登录 ssh

> 
> 了解如何在 Linux 中创建登录导语，来向要登录或登录后的用户显示不同的警告或消息。
> 
> 
> 


![](/data/attachment/album/201803/08/232321r2qar4rbzg2rwuwr.png)


无论何时登录公司的某些生产系统，你都会看到一些登录消息、警告或关于你将登录或已登录的服务器的信息，如下所示。这些是<ruby> 登录导语 <rt>  login banner </rt></ruby>。


![Login welcome messages in Linux](/data/attachment/album/201803/08/232354qeshpt4ezeyghsyb.png)


在本文中，我们将引导你配置它们。


你可以配置两种类型的导语。


1. 用户登录前显示的导语信息（在你选择的文件中配置，例如 `/etc/login.warn`）
2. 用户成功登录后显示的导语信息（在 `/etc/motd` 中配置）


### 如何在用户登录前连接系统时显示消息


当用户连接到服务器并且在登录之前，这个消息将被显示给他。意味着当他输入用户名时，该消息将在密码提示之前显示。


你可以使用任何文件名并在其中输入信息。在这里我们使用 `/etc/login.warn` 并且把我们的消息放在里面。



```
# cat /etc/login.warn
        !!!! Welcome to KernelTalks test server !!!!
This server is meant for testing Linux commands and tools. If you are
not associated with kerneltalks.com and not authorized please dis-connect
immediately.

```

现在，需要将此文件和路径告诉 `sshd` 守护进程，以便它可以为每个用户登录请求获取此标语。对于此，打开 `/etc/sshd/sshd_config` 文件并搜索 `#Banner none`。


这里你需要编辑该配置文件，并写下你的文件名并删除注释标记（`#`）。它应该看起来像：`Banner /etc/login.warn`。


保存文件并重启 `sshd` 守护进程。为避免断开现有的连接用户，请使用 HUP 信号重启 sshd。



```
root@kerneltalks # ps -ef | grep -i sshd
root     14255     1  0 18:42 ?        00:00:00 /usr/sbin/sshd -D
root     19074 14255  0 18:46 ?        00:00:00 sshd: ec2-user [priv]
root     19177 19127  0 18:54 pts/0    00:00:00 grep -i sshd

root@kerneltalks # kill -HUP 14255

```

就是这样了！打开新的会话并尝试登录。你将看待你在上述步骤中配置的消息。


![Login banner in Linux](/data/attachment/album/201803/08/232400d5zix9knzn8iodok.png)


你可以在用户输入密码登录系统之前看到此消息。


### 如何在用户登录后显示消息


消息用户在成功登录系统后看到的<ruby> 当天消息 <rt>  Message Of The Day </rt></ruby>（MOTD）由 `/etc/motd` 控制。编辑这个文件并输入当成功登录后欢迎用户的消息。



```
root@kerneltalks # cat /etc/motd
           W E L C O M E
Welcome to the testing environment of kerneltalks.
Feel free to use this system for testing your Linux
skills. In case of any issues reach out to admin at
info@kerneltalks.com. Thank you.


```

你不需要重启 `sshd` 守护进程来使更改生效。只要保存该文件，`sshd` 守护进程就会下一次登录请求时读取和显示。


![motd in linux](/data/attachment/album/201803/08/232401h3yuy242jb0bu0uu.png)


你可以在上面的截图中看到：黄色框是由 `/etc/motd` 控制的 MOTD，绿色框就是我们之前看到的登录导语。


你可以使用 [cowsay](https://kerneltalks.com/tips-tricks/cowsay-fun-in-linux-terminal/)、[banner](https://kerneltalks.com/howto/create-nice-text-banner-hpux/)、[figlet](https://kerneltalks.com/tips-tricks/create-beautiful-ascii-text-banners-linux/)、[lolcat](https://kerneltalks.com/linux/lolcat-tool-to-rainbow-color-linux-terminal/) 等工具创建出色的引人注目的登录消息。此方法适用于几乎所有 Linux 发行版，如 RedHat、CentOs、Ubuntu、Fedora 等。




---


via: <https://kerneltalks.com/tips-tricks/how-to-configure-login-banners-in-linux/>


作者：[kerneltalks](https://kerneltalks.com) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
*Learn how to create login banners in Linux to display a different warning or information message to the user who is about to log in or after he logs in.*

![](https://z5.kerneltalks.com/wp-content/uploads/2020/06/login-banner-message-in-linux.png)

Whenever you log in to some production systems of the firm, you get to see some login messages, warnings, or info about the server you are about to log in or already logged in like below. Those are the login banners.

![](https://z5.kerneltalks.com/wp-content/uploads/2020/06/Login-message-in-linux.png)

In this article we will walk you through how to configure them.

There are two types of banners you can configure.

- Banner message to display before user logs in (configure in the file of your choice eg.
`/etc/login.warn`

) - Banner message to display after the user successfully logged in (configure in
`/etc/motd`

)

### How to display message when user connects to system before login

This message will be displayed to the user when he connects to the server and before he logged in. This means when he enter the username, this message will be displayed before the password prompt.

You can use any filename and enter your message within. Here we used `/etc/login.warn`

file and put our messages inside.

```
# cat /etc/login.warn
!!!! Welcome to KernelTalks test server !!!!
This server is meant for testing Linux commands and tools. If you are
not associated with kerneltalks.com and not authorized please dis-connect
immediately.
```

Now, you need to supply this file and path to `sshd`

daemon so that it can fetch this banner for each user login request. For that open `/etc/sshd/sshd_config`

file and search for the line `#Banner none`


Here you have to edit the file and write your filename and remove the hash mark. It should look like : `Banner /etc/login.warn`


Save the file and restart `sshd`

daemon. To avoid disconnecting existing connected users, use the HUP signal to restart sshd.

```
root@kerneltalks # ps -ef |grep -i sshd
root 14255 1 0 18:42 ? 00:00:00 /usr/sbin/sshd -D
root 19074 14255 0 18:46 ? 00:00:00 sshd: ec2-user [priv]
root 19177 19127 0 18:54 pts/0 00:00:00 grep -i sshd
root@kerneltalks # kill -HUP 14255
```

That’s it! Open new sessions and try login. You will be greeted with the message you configured in the above steps.

![](https://z5.kerneltalks.com/wp-content/uploads/2020/06/login-banner.png)

You can see the message is displayed before the user enters his password and log in to the system.

### How to display message after user logs in

Message user sees after he logs into the system successfully is **M**essage **O**f **T**he **D**ay & is controlled by `/etc/motd`

file. Edit this file and enter the message you want to greet the user with once he successfully logged in.

```
root@kerneltalks # cat /etc/motd
W E L C O M E
Welcome to the testing environment of kerneltalks.
Feel free to use this system for testing your Linux
skills. In case of any issues reach out to admin at
info@kerneltalks.com. Thank you.
```

You don’t need to restart `sshd`

daemon to take this change effect. As soon as you save the file, its content will be read and displayed by sshd daemon from the very next login request it serves.

![](https://z5.kerneltalks.com/wp-content/uploads/2020/06/motd-message-in-linux.png)

You can see in the above screenshot: Yellow box is MOTD controlled by `/etc/motd`

and the green box is what we saw earlier [login banner](#banner).

You can use tools like [cowsay](https://kerneltalks.com/tips-tricks/cowsay-fun-in-linux-terminal/), [banner](https://kerneltalks.com/howto/create-nice-text-banner-hpux/), [figlet](https://kerneltalks.com/tips-tricks/create-beautiful-ascii-text-banners-linux/), [lolcat ](https://kerneltalks.com/linux/lolcat-tool-to-rainbow-color-linux-terminal/)to create fancy, eye-catching messages to display at login. This method works on almost all Linux distros like RedHat, Centos, Ubuntu, Fedora, etc.

Thank you for this! Exactly what I needed.

Thanks for sharing the knowledge!!