linuxssh登录界面

自定制ssh登录界面

用sudo创建可执行文件/etc/update-motd.d/<执行序号>-文件名(bash脚本),则此脚本会在ssh登录时执行,脚本输出会显示在ssh登录界面上。此法可用户定制酷炫的ssh登录界面,例如

ScreenShot 2019-12-04 17.59.39

安装方法

安装依赖

  • update-motd显示ssh登录提示

  • lolcat 让终端输出彩虹色

  • figlet 让终端创建字符图案

sudo apt update
sudo apt install update-motd lolcat figlet

hostname-color:主机名

预期效果

ScreenShot 2019-12-04 17.59.39的副本

tmp='/etc/update-motd.d/10-hostname-color'
sudo touch $tmp
sudo chmod a+x $tmp
sudo vim $tmp

vim中写入

#!/bin/bash
if [ -e /usr/bin/figlet ] && [ -e /usr/games/lolcat ]; then
    /usr/bin/figlet "$(hostname)" | /usr/games/lolcat -f
fi

neofetch:系统信息

预期效果

image-20231120165641914

sudo apt install neofetch
tmp='/etc/update-motd.d/12-neofetch'
sudo touch $tmp
sudo chmod a+x $tmp
sudo vim $tmp
#!/bin/bash
if [ -e /usr/bin/neofetch ]; then
    echo ""
    /usr/bin/neofetch --off --colors 0 0 6 6 3 7 --color_blocks off --underline off --bar_border off --disable title line_break
fi

diskspace:磁盘使用

预期效果

ScreenShot 2019-12-04 17.59.39的副本 2

tmp='/etc/update-motd.d/35-diskspace'
sudo touch $tmp
sudo chmod a+x $tmp
sudo vim $tmp

vim中写入

#!/bin/bash

# config
max_usage=90
bar_width=50
# colors
white="\e[39m"
green="\e[1;32m"
red="\e[1;31m"
dim="\e[2m"
undim="\e[0m"

# disk usage: ignore zfs, squashfs & tmpfs
mapfile -t dfs < <(df -H -x zfs -x squashfs -x tmpfs -x devtmpfs --output=target,pcent,size | tail -n+2)
printf "\ndisk usage:\n"

for line in "${dfs[@]}"; do
    # get disk usage
    usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
    used_width=$((($usage*$bar_width)/100))
    # color is green if usage < max_usage, else red
    if [ "${usage}" -ge "${max_usage}" ]; then
        color=$red
    else
        color=$green
    fi
    # print green/red bar until used_width
    bar="[${color}"
    for ((i=0; i<$used_width; i++)); do
        bar+="="
    done
    # print dimmmed bar until end
    bar+="${white}${dim}"
    for ((i=$used_width; i<$bar_width; i++)); do
        bar+="="
    done
    bar+="${undim}]"
    # print usage line & bar
    echo "${line}" | awk '{ printf("%-31s%+3s used out of %+4s\n", $1, $2, $3); }' | sed -e 's/^/  /'
    echo -e "${bar}" | sed -e 's/^/  /'
done

landscape-sysinfo:各类负荷

预期效果

image-20231120170110450

sudo apt install bc landscape-common
tmp='/etc/update-motd.d/50-landscape-sysinfo'
sudo touch $tmp
sudo chmod a+x $tmp
sudo vim $tmp

vim中写入

#!/bin/sh
# pam_motd does not carry the environment
[ -f /etc/default/locale ] && . /etc/default/locale
export LANG
cores=$(grep -c ^processor /proc/cpuinfo 2>/dev/null)
[ "$cores" -eq "0" ] && cores=1
threshold="${cores:-1}.0"
if [ $(echo "`cut -f1 -d ' ' /proc/loadavg` < $threshold" | bc) -eq 1 ]; then
    echo
    echo -n "  System information as of "
    /bin/date
    echo
    /usr/bin/landscape-sysinfo
else
    echo
    echo " System information disabled due to load higher than $threshold"
fi

motd:今日信息

motd,即Message of The Day

tmp='/etc/update-motd.d/50-landscape-sysinfo'
sudo touch $tmp
sudo chmod a+x $tmp
sudo vim $tmp # 用vim向当中手动添加自定义的今日提示消息。默认文件内容为空。

试运行-最终效果

运行登录输出的出现

sudo run-parts /etc/update-motd.d

ScreenShot 2019-12-04 17.59.39

如得到上述输出,则配置成功。之后每次ssh <服务器>的截面均是上图输出。注:非ssh登录,则不会有此输出,例如当ssh登录后运行bash,则无此输出。

关闭ssh登录界面提示

关闭所有提示

修改ssh服务端配置

sudo vim /etc/ssh/sshd_config

其中UsePAM no表示关闭ssh登录提示,UsePAM yes表示开启

重启ssh服务端

sudo /etc/init.d/ssh restart

而后ssh登录时,上述修改就会生效。

但执行sudo run-parts /etc/update-motd.d时,始终都会显示ssh登录提示。

关闭特定条目的提示

cd /etc/update-motd.d
# 关闭特定条目的提示
sudo chmod -x /etc/update-motd.d/<特定条目的配置文件>
# 打开特定条目的提示
sudo chmod +x /etc/update-motd.d/<特定条目的配置文件>

执行sudo run-parts /etc/update-motd.d时,始终都会显示修改后的ssh登录提示。