一、任务背景:最近在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登录欢迎信息相关推荐

  1. ubuntu修改ssh端口_在Ubuntu上更改SSH欢迎横幅

    ubuntu修改ssh端口 Every time I connect to my Ubuntu development server through my ssh client, I receive ...

  2. ubuntu修改ssh服务的端口号

    一.找到ssh配置文件位置 vim /etc/ssh/sshd_config 二.修改ssh登录端口号 修改 port 22 为 port xxxx 三.重启ssh服务 /etc/init.d/ssh ...

  3. Linux下修改SSH登录端口

    Linux下修改SSH登录端口 LINUX 的默认SSH 端口是 22.为了防止别人暴力破解,建议修改SSH 访问端口:vim /etc/ssh/sshd_config 找到Port 22 这一行,这 ...

  4. 若依框架获取和修改当前登录用户信息

    若依框架获取和修改当前登录用户信息 后台修改 前端修改 前言:做一些功能的时候我们肯定得用到当前登陆者信息,所以我就查找了一下若依怎么获取当前登录者信息,用this.$store.state.user ...

  5. ubuntu服务器ssh登录密码修改,Ubuntu-18.04 下修改root用户密码,安装SSH服务,允许root用户远程登录,安装vsftp服务器...

    修改root用户密码 打开终端,输入 sudo passwd root 指令: 安装SSH服务 ssh默认端口号是22,可以在/etc/ssh/sshd_config文件中修改 查看服务器否开启:ne ...

  6. Firefly-RK3288 Ubuntu18.04 修改终端登录显示信息

    目录 起因 探究 结果 参考资料 Platform: Firefly-RK3288 OS: Ubuntu 18.04 Module: motd 起因 最近在调试一块Firefly-RK3288 的板子 ...

  7. 腾讯云配置Ubuntu使用SSH登录

    今天用新客户优惠花50购买了一台装有Ubuntu的腾讯云服务器,没想到这么难用,无法使用SSH直接连接,得经过配置 PasswordAuthentication yes 才行.首先这个提示界面要仔细阅 ...

  8. Linux下修改ssh登录密码

    文章目录 三.修改linux的ssh登录密码 1. 改修登录密码 2.修改密码失败原因 3.修改权限后修改登录密码 三.修改linux的ssh登录密码 1. 改修登录密码 如果权限没有问题的话那么直接 ...

  9. Linux安全加固之修改SSH登录端口

    Linux的默认登录端口是22,这种默认端口往往本身就存在一定的风险,为了加固Linux的安全性,可以通过修改SSH的默认登录端口来实现,操作比较简单,只要三步就行 1. 修改配置文件 使用命令进入配 ...

最新文章

  1. Maya游戏角色绑定入门学习教程 Game Character Rigging for Beginners in Maya
  2. Unity NGUI ScrollView 苹果式滑动
  3. 计算机考试一年有肌肉,阅卷老师最想看到什么样的字体?电脑阅卷时代,这种字体很吃香...
  4. Centos7 ping不了百度
  5. wxPython事件处理
  6. Item category dropdown list的determine - filtering 逻辑
  7. C#实现网页加载后将页面截取成长图片 | Playwright版
  8. 静态工厂方法与传统构造方法
  9. 超棒的Glide图片加载
  10. Scala进阶-函数练习
  11. n元n次方程求解c 语言,解n元一次方程
  12. C语言程序设计基础讲座之指针的慨念
  13. @Import注解使用及源码分析
  14. Rayman的绝顶之路——Leetcode每日一题打卡10
  15. 利用集成支持向量机模型评估信贷风险
  16. 怎么制作证件照电子版?安利下面这三款软件给你
  17. 手机天猫将全面升级,成为天猫新零售入口丨对话天猫总裁靖捷
  18. 关于职业规划的重要性
  19. nginx中不同client设置User-Agent与user_agent的坑
  20. bilibili 网页端如何关灯和开灯?

热门文章

  1. python解析gff文件中的转录本
  2. 网页头部的声明 lang=zh和 lang=zh-cn 及 lang=zh-cmn的区别
  3. 樱花庄的宠物女孩AtCoder Grand Contest 015E - Mr.Aoki Incubator
  4. C++语言涉猎笔记(二)
  5. linux 原始套接字 绑定网卡,Linux网络数据捕获之原始套接字
  6. 电容的原理与应用(补充中)
  7. 【git】No supported authentication methods available(server sent:pubickey)
  8. 什么是 游戏引擎 ?各个主流引擎的区别
  9. 绘制动态心形图案::R语言绘制心形图
  10. Vue源码学习 - 组件化(三) 合并配置