Ubuntu修改ssh登录欢迎信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/plm199513100/article/details/81270377

一、任务背景:最近在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 <[email protected]>
#
#    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 utility
        DISTRIB_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 <[email protected]>,
#             Brian Murray <[email protected]>
#
#    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分
else
        printf "\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就行了。

猜你喜欢

转载自blog.csdn.net/plm199513100/article/details/81270377