暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Linux - MOTD (Message of the day)

原创 喻一桓 2020-07-16
8121

Linux MOTD

时间线:

版本 修改时间 人员 说明
v0.1 2020年7月16日 Leviathan 第一版:介绍了Linux中的MOTD(Message of the day)相关的知识点

在Linux中,motd是【message of the day】的缩写,英文直译就是【当天的提示信息】。
MOTD通常出现在用户成功登陆到Linux后的终端界面的首部,如下:

Authorized users only. All activities may be monitored and reported.

Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Thu Jul 16 14:25:47 2020 from 192.168.27.1


Welcome to 4.19.90-2003.4.0.0036.oe1.x86_64

System information as of time:  2020年 07月 16日 星期四 14:35:13 CST

System load:    0.03
Processes:      150
Memory used:    6.5%
Swap used:      0.0%
Usage On:       24%
IP address:     192.168.27.130
Users online:   2



[root@openeuler ~]# 

如上,在这样的命令反馈中,MOTD的信息是:【Authorized users only. All activities may be monitored and reported.】。

在每次系统登录的时候,通过终端,向登录用户展示一些信息,这些信息可能是:

  1. 欢迎信息
  2. 提示使用系统的注意事项
  3. 系统运行的概要信息
  4. 等等

MOTD:启用

如果希望启用MOTD,则需要修改SSH的配置文件【/etc/ssh/sshd_config】

[root@linux ~]# cat /etc/ssh/sshd_config | grep "PrintMotd"                     
#PrintMotd yes
[root@linux ~]# 
[root@linux ~]# sed -i '/PrintMotd/s/#PrintMotd/PrintMotd/g' /etc/ssh/sshd_config 
[root@linux ~]# 
[root@linux ~]# cat /etc/ssh/sshd_config | grep "PrintMotd"                       
PrintMotd yes
[root@linux ~]# 

如上,就在SSH服务中打开了MOTD的显示参数。
然后,重启SSH服务即可生效:

[root@linux ~]# service sshd status
Redirecting to /bin/systemctl status sshd.service
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-07-06 09:20:00 CST; 1 weeks 3 days ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 1078 (sshd)
    Tasks: 1
   Memory: 2.2M
   CGroup: /system.slice/sshd.service
           └─1078 /usr/sbin/sshd -D

Jul 06 09:20:00 linux sshd[1078]: Server listening on :: port 22.
Jul 06 09:20:00 linux systemd[1]: Started OpenSSH server daemon.
Jul 06 09:21:07 linux sshd[2613]: Accepted password for root from 192.168.27.1 port 8301 ssh2
Jul 06 10:21:37 linux sshd[4901]: Accepted password for root from 192.168.27.1 port 13982 ssh2
Jul 06 21:08:21 linux sshd[62698]: Accepted password for root from 192.168.27.1 port 10393 ssh2
Jul 09 08:23:22 linux sshd[71997]: Accepted password for root from 192.168.27.1 port 1629 ssh2
Jul 13 08:45:27 linux sshd[91487]: Accepted password for root from 192.168.27.1 port 13720 ssh2
Jul 13 09:27:10 linux sshd[92063]: Accepted password for root from 192.168.27.1 port 1329 ssh2
Jul 13 09:35:53 linux sshd[92525]: Accepted password for root from 192.168.27.1 port 1750 ssh2
Jul 16 09:23:16 linux sshd[126752]: Accepted password for root from 192.168.27.1 port 2346 ssh2
[root@linux ~]# 
[root@linux ~]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
[root@linux ~]# 
[root@linux ~]# service sshd status 
Redirecting to /bin/systemctl status sshd.service
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-07-16 14:52:55 CST; 2s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 13169 (sshd)
    Tasks: 1
   Memory: 1.0M
   CGroup: /system.slice/sshd.service
           └─13169 /usr/sbin/sshd -D

Jul 16 14:52:55 linux systemd[1]: Starting OpenSSH server daemon...
Jul 16 14:52:55 linux sshd[13169]: Server listening on 0.0.0.0 port 22.
Jul 16 14:52:55 linux sshd[13169]: Server listening on :: port 22.
Jul 16 14:52:55 linux systemd[1]: Started OpenSSH server daemon.
[root@linux ~]# 

可以通过服务的状态中的【Main PID】看到,重启前后,SSH服务的进程号确有变化。

MOTD:文件【/etc/motd】

如果需要对MOTD的信息进行配置,可以通过操作系统上的文件【/etc/motd】来获取。
例如:

[root@openeuler ~]# cat /etc/motd

Authorized users only. All activities may be monitored and reported.
[root@openeuler ~]# 

但这个文件或这个文件的路径,并不是固定的,有时候它可能位于其他路径,或者该文件是以软链接的方式存在的。
比如,下面的例子显示了该文件可能存放在目录【/run】中的情况:

[root@openeuler ~]# ls -ltr /run | grep motd
drwxr-xr-x  2 root           root             40  7月 16 14:23 motd.d
-rw-r--r--  1 root           root              0  7月 16 14:23 motd
[root@openeuler ~]# 
[root@openeuler ~]# cat /run/motd
[root@openeuler ~]# 
[root@openeuler ~]# ls -ltr /run/motd.d/
总用量 0
[root@openeuler ~]# 

MOTD:显示信息的方式

静态MOTD

在前面的样例代码中可以看到【/etc/motd】中的信息都为静态的内容。
如果用户登录时,希望显示静态的内容,只需要修改【/etc/motd】文件,即可。

在显示静态内容的时候,除了可以是朴实无华的一段字符串之外,还可以对静态内容进行渲染,让原本静态的内容显示的更加生动,从而更具提示效果。

通过【vi】的vi /etc/motd命令打开【/etc/motd】,并执行编辑:

^[[13;34;43m Hello, world ^[[0;0;0m

在需要格式化的文本的前后需要用终端格式语句标注。
可以看到,在上例中,最后的【^[[0;0;0m】将行首的格式恢复为普通的,否则,会影响后续所有的命令行的文本的格式显示。

其语法具体如下:

<ESC>[ {attr};{fg};{bg}m

其中:

  1. ESC的部分,在vi / vim工具中,按键Ctrl+vvi / vim设置为【Visual Block】模式,然后按键ESC,就可以写入字符【^[】了
  2. {attr}:属性部分
代码 说明(English) 说明(中文)
0 Reset all attributes(return to normal mode) 恢复到无格式状态
1 Bright(usually turns on BOLD) 加粗
2 DIM
3 Underline 下划线
5 Blink
7 Reverse
8 Hidden
  1. {fg}:字体颜色
代码 说明(English) 说明(中文)
0 Reset all attributes(return to normal mode) 恢复到无格式状态
30 Black 黑色
31 Red 红色
32 Green 绿色
33 Yellow 黄色
34 Blue 蓝色
35 Magenta 紫红色
36 Cyan 青色
37 White 白色
  1. {bg}:背景颜色
代码 说明(English) 说明(中文)
0 Reset all attributes(return to normal mode) 恢复到无格式状态
40 Black 黑色
41 Red 红色
42 Green 绿色
43 Yellow 黄色
44 Blue 蓝色
45 Magenta 紫红色
46 Cyan 青色
47 White 白色

动态MOTD

文件/etc/motd只能够显示静态的内容,所以想要实现动态的信息,则需要编写脚本来实现。

比如,我写了一个下面这样的脚本:

[root@linux script]# pwd
/script
[root@linux script]# 
[root@linux script]# cat system_info.sh 
#!/bin/bash

date=`date "+%F %T"`
head="Current: $date"

kernel=`uname -r`
hostname=`echo $HOSTNAME`

echo
echo "================================="
echo "$head"
echo "================================="
printf "Kernel  :\t%s\n" $kernel
printf "HostName:\t%s\n" $hostname
echo
echo
[root@linux script]# 
[root@linux script]# sh system_info.sh 

=================================
Current: 2020-07-16 15:44:19
=================================
Kernel  :       3.10.0-1127.el7.x86_64
HostName:       linux


[root@linux script]# 

给这个文件添加执行权限:

[root@linux script]# ls -ltr
total 4
-rw-r--r-- 1 root root 280 Jul 16 15:44 system_info.sh
[root@linux script]# 
[root@linux script]# chmod +x system_info.sh 
[root@linux script]# 
[root@linux script]# ls -ltr
total 4
-rwxr-xr-x 1 root root 280 Jul 16 15:44 system_info.sh
[root@linux script]# 

将对该脚本的调用添加到系统的Profile文件【/etc/profile】中:

[root@linux script]# cat /etc/profile | tail -n 1
unset -f pathmunge
[root@linux script]# 
[root@linux script]# echo "/script/system_info.sh" >> /etc/profile
[root@linux script]# 
[root@linux script]# cat /etc/profile | tail -n 1                 
/script/system_info.sh
[root@linux script]# 

然后,登录用户的时候,就可以看到效果了:

[root@linux script]# su - adamhuan
Last login: Mon Jun 22 23:33:19 CST 2020 on :0

=================================
Current: 2020-07-16 15:48:54
=================================
Kernel  :       3.10.0-1127.el7.x86_64
HostName:       linux


[adamhuan@linux ~]$ 

至此,关于Linux系统中的MOTD的内容,就基本描述完了。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论