[Linux][CentOS] CentOS 7 Motd Message Customizing

[Linux][CentOS] CentOS 7 Motd Message Customizing

Ubuntu上有內建的歡迎資訊但在CentOS上只有顯示上一筆登入資料,與Ubuntu相比起來資料量少了很多,實際上這些資料都可以自己自訂,以下說明過程,下面的/etc/motd檔案是一個靜態檔案,可以利用自己寫好的Shell Script來產生資料後寫入到該檔案中,或是在/etc/profile裡面來指定執行的Shell檔案並產出資料,

vim /etc/pam.d/login
    session    optional     pam_motd.so  motd=/etc/motd
    or
    session    optional     pam_motd.so

先來計算一下需要更新的套件包有哪些,當然你要改成列表也是可以,

vim /opt/task/countUpdates.sh
    #!/bin/bash

    yum check-update --quiet | grep -v "^$" | wc -l > /opt/task/updatesNum.List

接著建立要撈取資料的Shell Script,

vim /opt/task/motd.sh
    #!/bin/sh
    #
    printf "\n"
    printf "\t- %s\n\t- Kernel %s\n" "$(cat /etc/redhat-release)" "$(uname -r)"
    printf "\n"

    UPDATES_COUNT=`cat /opt/task/updatesNum.List`

    date=`date`
    load=`cat /proc/loadavg | awk '{print $1}'`
    root_usage=`df -h / | awk '/\// {print $(NF-1)}'`
    memory_usage=`free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { printf("%3.1f%%", used/total*100)}'`
    swap_usage=`free -m | awk '/Swap/ { printf("%3.1f%%", "exit !$2;$3/$2*100") }'`
    users=`users | wc -w`
    time=`uptime | grep -ohe 'up .*' | sed 's/,/\ hours/g' | awk '{ printf $2" "$3 }'`
    processes=`ps aux | wc -l`
    ethup=$(ip -4 ad | grep 'state UP' | awk -F ":" '!/^[0-9]*: ?lo/ {print $2}')
    ip=$(ip ad show dev $ethup |grep -v inet6 | grep inet|awk '{print $2}')

    echo "System information as of: $date"
    echo
    printf "System load:\t%s\tIP Address:\t%s\n" $load $ip
    printf "Memory usage:\t%s\tSystem uptime:\t%s\n" $memory_usage "$time"
    printf "Usage on /:\t%s\tSwap usage:\t%s\n" $root_usage $swap_usage
    printf "Local Users:\t%s\tProcesses:\t%s\n" $users $processes
    echo "Updates available: ${UPDATES_COUNT}"
    echo

    [ -f /etc/motd.tail ] && cat /etc/motd.tail || true

接著把這個檔案執行路徑新增到/etc/profile的裡面,

vim /etc/profile
    /opt/task/motd.sh

上述是.sh類的檔案記得賦予它執行的權限,然後countUpdates.sh可以排進排程中每天定時執行去更新檔案數量,之後重新登入的時候就會有你做好的畫面出現囉!

Reference

Dynamic MOTD on Centos7


https://gist.github.com/cha55son/6042560
Dynamic MOTD Centos/RedHat
https://relativkreativ.at/articles/simple-update-notifications-for-your-centos-redhat-server
https://unix.stackexchange.com/questions/99185/what-do-square-brackets-mean-without-the-if-on-the-left
https://unix.stackexchange.com/questions/32210/why-does-parameter-expansion-with-spaces-without-quotes-work-inside-double-brack

Add a Comment