Linux motd详解

简介:
原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://dgd2010.blog.51cto.com/1539422/1661362

  motd是message of the day的缩写,意思是“当天的提示信息”,通常在用户成功登录到Linux后出现,该信息可以从/etc/motd文本文件中找到。

提示:/etc/motd文件有时不一定是个常规文本文件,也可能是一个软链接到某个特定的文件,如/var/run/motd。

一般来说,这个文件是一个静态文本文件,只在某个Linux发行版本完成安装或升级后才会更新。

提示:UNIX/Linux系统管理员也通常会把一些重要的信息写到这个文件里面,以方便其他人或自己下次成功登录时需要注意什么和做什么,非常方便。

通常一个标准的motd信息有以下内容:

  1. 欢迎信息,一般包括Linux发行版本名称,内核版本、操作系统位数

  2. 操作系统当前信息,如操作系统负载,进程数量,文件系统使用情况,当前用户登录数,内存(含swap)使用情况,IP地址

  3. 文档和帮助信息

  4. 可更新的软件包和可升级的安全补丁

由此可见,motd展示出来的一定是一个当前的信息,是一组固定时间下特定参数所对应的数值,而不是一成不变的信息,因此静态的文本文件不足以满足以上内容。

想要做到每次登录都要显示出当前的motd,则需要一种机制来实现,pam_motd就是实现这个功能的模块。可参考motd(5), pam.conf(5), pam.d(5), pam(7), update-motd(5)

在Ubuntu中有update-motd来帮助系统管理员或用户实现这个功能。可执行的脚本位于/etc/update-motd.d/*下,在每次登录时以root身份由pam_motd调用,脚本运行的次序由run-parts(run scripts or programs in a directory,在一个目录里运行脚本或程序)的--lsbsysinit选项决定。pam_motd也支持不动态更新,只需要在其选项中添加noupdate即可。

编辑/etc/pam.d/login文件,

启用动态更新motd模块:session  optional  pam_motd.so  motd=/etc/motd

关闭动态更新motd模块:session  optional  pam_motd.so  noupdate

剩下的就是发挥系统管理员的Shell脚本水平了,例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#Long running operations (such as network calls) or resource intensive scripts should cache output, and only  update that output if it is deemed expired.  For instance:   
#/etc/update-motd.d/50-news
#!/bin/sh   
out= /var/run/foo    
script= "w3m -dump http://news.google.com/"    
if  [ -f  "$out"  ];  then    
   # Output exists, print it    
   echo    
   cat  "$out"    
   # See if it's expired, and background update    
   lastrun=$(stat -c %Y  "$out" ) || lastrun=0    
   expiration=$( expr  $lastrun + 86400)    
   if  [ $( date  +%s) - ge  $expiration ];  then    
     $script >  "$out"  &    
   fi    
else    
   # No cache at all, so update in the background    
   $script >  "$out"  &    
fi
#Scripts should emit a blank line before output, and end with a newline character.  For instance:   
#/etc/update-motd/05-lsb-release
#!/bin/sh   
echo    
lsb-release -a

补充信息:run-parts runs all the executable files named within constraints described below,  found  in  directory  directory.Other files and directories are silently ignored.run-parts运行目录下所有的可执行文件,其他文件或目录将被忽略。可参考man 8 run-parts。

如果换一种思路,就可以将这些思路和想法应用到CentOS上。CentOS同样拥有来自pam包的pam_motd.so模块,只要启用(例如编辑/etc/pam.d/login文件)就可以,与其他pam模块一样,位于/lib64/security/目录下。

PS:其实写文章思路很简单,找到一个一组人群能用到的知识点,查看相关的手册,将这些知识整理出来,结合自己的实际经验,再加一点温馨提示就ok了!有些事情大家都知道…但是愿意去做的有几个?

本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1661362

目录
相关文章
|
Linux 安全 Ubuntu
|
16天前
|
Web App开发 Linux 网络安全
工作中常用到的Linux命令
工作中常用到的Linux命令
|
4天前
|
机器学习/深度学习 缓存 监控
linux查看CPU、内存、网络、磁盘IO命令
`Linux`系统中,使用`top`命令查看CPU状态,要查看CPU详细信息,可利用`cat /proc/cpuinfo`相关命令。`free`命令用于查看内存使用情况。网络相关命令包括`ifconfig`(查看网卡状态)、`ifdown/ifup`(禁用/启用网卡)、`netstat`(列出网络连接,如`-tuln`组合)以及`nslookup`、`ping`、`telnet`、`traceroute`等。磁盘IO方面,`iostat`(如`-k -p ALL`)显示磁盘IO统计,`iotop`(如`-o -d 1`)则用于查看磁盘IO瓶颈。
|
13天前
|
NoSQL Linux Shell
常用的 Linux 命令
常用的 Linux 命令
36 9
|
21小时前
|
监控 Linux Windows
50个必知的Linux命令技巧,你都掌握了吗?(下)
50个必知的Linux命令技巧,你都掌握了吗?(下)
|
1天前
|
Linux Shell Windows
Linux 常用基本命令
Linux 常用基本命令
|
1天前
|
Ubuntu Linux Shell
linux免交互登陆远程主机并执行命令(密钥对和Expect)
linux免交互登陆远程主机并执行命令(密钥对和Expect)
|
1天前
|
Linux
【Linux】常用命令
【Linux】常用命令
21 0
|
1天前
|
安全 Ubuntu Linux
Linux 网络操作命令Telnet
Linux 网络操作命令Telnet
13 0
Linux 网络操作命令Telnet
|
3天前
|
Linux 数据安全/隐私保护
Linux常用命令实例带注释
Linux常用命令实例带注释
30 0