Ubuntu修改ssh登录欢迎信息
一、任務背景:最近在linux基礎教學時,給班里的同學分了幾個組,并在騰訊云主機上給他們實際的配置了各個組和各個用戶,想讓班里的同學可以通過ssh連上云服務器進行linux練習。這兩天突發奇想,給各個組制定個分數規則(為了促進他們學習的意思,^_^),而且他們每次登錄都可以看到自己組的分數了(本質上就是修改主機登錄時候的歡迎信息)。
二、任務說明:在一般的linux發行版中(如centos),/etc/issue里存放了用戶成功登錄前顯示的信息;/etc/motd存放了用戶成功登錄后顯示的信息;但是在ubuntu中有些不一樣,它相關的是/etc/update-motd.d/文件夾下的幾個腳本,如下所示:
/etc/update-motd.d/腳本列表:
00-header
10-help-text
90-updates-available
91-release-upgrade
98-fsck-at-reboot
98-reboot-required
其中,當我們通過ssh登錄主機時,會輸出/var/run/motd.dynamic 中的信息。而/var/run/motd.dynamic 中的信息就是用戶登錄時系統已root身份執行上述/etc/update-motd.d/ 下面的幾個腳本所生成的。所以解決問題的思路是修改這幾個文件下面的腳本。
對于這幾個腳本來說,其中00-header主要是顯示一些歡迎信息的頭部:其script代碼如下:
#!/bin/sh # # 00-header - create the header of the MOTD # Copyright (C) 2009-2010 Canonical Ltd. # # Authors: Dustin Kirkland <kirkland@canonical.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. [ -r /etc/lsb-release ] && . /etc/lsb-release if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then# Fall back to using the very slow lsb_release utilityDISTRIB_DESCRIPTION=$(lsb_release -s -d) fi printf "Welcome to %s (%s %s %s)\n" "$DISTRIB_DESCRIPTION" "$(uname -o)" "$(uname -r)" "$(uname -m)"10-help-text中主要是一些提供幫助查詢網站的信息,其腳本代碼如下:
#!/bin/sh # # 10-help-text - print the help text associated with the distro # Copyright (C) 2009-2010 Canonical Ltd. # # Authors: Dustin Kirkland <kirkland@canonical.com>, # Brian Murray <brian@canonical.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.printf "\n" printf " * Documentation: https://help.ubuntu.com\n" printf " * Management: https://landscape.canonical.com\n" printf " * Support: https://ubuntu.com/advantage\n"剩下的基本上沒怎么用到,在這里也不介紹了。
三、任務實現
解決問題基本的思路是:先介紹下問題背景吧。系統中有5個組分別命名為group1-group5.每個組的成員命名為1_1,1_2…1_10(第二組類似)。本人在/home/ubuntu/training下面新建了一個score.txt的文件用來保存每個組的分數。格式如下:
group1 100
group2 100
group3 100
group4 100
group5 100
具體思路是:先取得登錄用戶的用戶名,通過用戶名取得用戶所在的組(即選取用戶名前面的數字)。然后在score.txt文件中,通過對應的組選擇對于的分數,打印出來。經過修改之后,10-help-text中的代碼清空了,00-header中的腳本如下所示:
#!/bin/bash log_name=$(logname) #獲取登錄的主機名,命令是logname printf "Hello, %s. Welcome to Pan's cloud computer!\r\n" "$log_name"log_group="group"$(echo "$log_name" | cut -d '_' -f 1) #獲取當前用戶所在的組score=$(grep "$log_group" /home/ubuntu/training/score.txt | cut -f 2) #獲取用戶所在組的分數 printf "\r\nthe score in your group now is %s. " "$score"if [ $((score)) -le 60 ] ; then printf "You need work hard for your group! Go FOR IT!\r\n" #如果大于60分 elseprintf "\r\nToday is a luckly day. GOOK LUCK!\r\n" #如果小于60分 fi exit 0注意:在獲得登錄的主機名的時候不能使用whoami,因為whoami得出的是當前執行此程序的是哪個用戶。有前面所述我們知道用戶登錄時是暫時以root身份來執行這些腳本,所以whoami得出的都是root。而logname可以得出當前登錄的用戶名。
最后,還有一點,就是原先的登錄信息中還有個顯示LastLogin的信息的,在這里本人也把它去除了,配置的是/etc/ssh/sshd_config里的一個屬性PrintLastLog 選擇為no就行了。
總結
以上是生活随笔為你收集整理的Ubuntu修改ssh登录欢迎信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 叮咚~您有一份GitHub2020年度报
- 下一篇: ubuntu18.04安装pycharm