penglongli / blog

18 stars 1 forks source link

Linux UpdateMotd 公告板 #66

Open penglongli opened 6 years ago

penglongli commented 6 years ago

参考文档:

Linux 公告板是个很有意思的东西。 在你通过 SSH 链接上主机后,在界面上能够打印出来上次登录时间、CPU、提示等等自定义的东西。

首先,cd /etc/update-motd.d 文件夹:

root@core-1:/etc/update-motd.d# cd /etc/update-motd.d/
root@core-1:/etc/update-motd.d# ls -l
total 24
-rwxr-xr-x 1 root root 1220 Oct 23  2015 00-header
-rwxr-xr-x 1 root root 1157 Jun 14  2016 10-help-text
-rwxr-xr-x 1 root root  299 Feb  9  2016 91-release-upgrade
-rwxr-xr-x 1 root root  111 Jun 24  2015 97-overlayroot
-rwxr-xr-x 1 root root  142 May 25  2016 98-fsck-at-reboot
-rwxr-xr-x 1 root root  144 May 25  2016 98-reboot-required

然后,我们新建一个文件 99-test

root@core-1:/etc/update-motd.d# vim 99-test
#!/bin/sh

printf "Hello World, Pelin\n"

然后,为这个文件增加可执行权限:

root@core-1:/etc/update-motd.d# chmod +x 99-test

此时,我们在当前命令行窗口执行:

root@core-1:/etc/update-motd.d# run-parts /etc/update-motd.d/
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-91-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Hello World, Pelin

可以发现,我们自定义的信息已经出现在了公告栏中。 (或者也可以打开新的命令行窗口 SSH 链接)

扩展

  1. /var/run/motd.dynamic 与 /run/motd.dynamic

这两个文件内容就是我们 /etc/update-motd.d 目录下脚本动态生成的

  1. 执行顺序

当我们在 /etc/update-motd.d 目录下新建脚本的时候,运行顺序是什么样的?

答: 按照字典序。 例如,我们有如下的几个文件:

98-test
97-test
99-test
90-test
101-test
121-test

执行顺序为:

101-test
121-test
90-test
97-test
98-test
99-test