標籤:motd pam linux系統管理員 login message 登入提示資訊
motd是message of the day的縮寫,意思是“當天的提示資訊”,通常在使用者成功登入到Linux Shell(非互動登入)時出現,該資訊可以從/etc/motd文字檔中找到。
提示:/etc/motd檔案有時不一定是個常規文字檔,也可能是一個軟連結到某個特定的檔案,如/var/run/motd。
一般來說,這個檔案是一個靜態文字檔,只在某個Linux發行版本完成安裝或升級後才會更新。
提示:UNIX/Linux系統管理員也通常會把一些重要的資訊寫到這個檔案裡面,以方便其他人或自己下次成功登入時需要注意什麼和做什麼,非常方便。
通常一個標準的motd資訊有以下內容:
歡迎資訊,一般包括Linux發行版本名稱,核心版本、作業系統位元
作業系統當前資訊,如作業系統負載,進程數量,檔案系統使用方式,目前使用者登入數,記憶體(含swap)使用方式,IP地址
文檔和協助資訊
可更新的軟體包和可升級的安全補丁
由此可見,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指令碼水平了,例如
#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 motd詳解