$ yum install mailx
Linux 使用者登入時會讀取設定檔:
/etc/profile (login shell 才會讀) ~/.bash_profile (login shell 才會讀) ~/.bashrc (non-login shell 會讀)CentOS 7 中 /etc/profile 會讀 /etc/profile.d/*.sh 所有檔案,
~/.bashrc 會讀 /etc/bashrc,且 /etc/bashrc 又會讀 /etc/profile.d/*.sh
所以我直接在 /etc/profile.d/ 底下新增一個 .sh 的shell script檔案,用來發信
$ vi /etc/profile.d/login.sh
if [ "${LOGNAME}" == "xyz" ] || [ "${USER}" == "xyz" ]; then MAIL_TO="xyz@example.com" else MAIL_TO="admin@example.com,admin2@example.com" fi { echo "LOGNAME=${LOGNAME}" echo "USER=${USER}" echo "SSH_CONNECTION=${SSH_CONNECTION}" echo "date:" `date` echo "who:" `who` } | mail -S "from=ssh<ssh@example.com>" -s "LOGNAME=${LOGNAME},USER=${USER} Access from `who | cut -d'(' -f2 | cut -d')' -f1`" ${MAIL_TO}
參考:
How to Get Root and User SSH Login Email Alerts
bash - How do I set up an email alert when a ssh login is successful? - Ask Ubuntu
Linux /etc/profile檔案詳解及修改後如何立即生效(使用source命令) - IT閱讀
科学网—[转载]login shell与non-login shell的区别 - 王诗翔的博文
沒有留言:
張貼留言