Changer le motd sous Linux
Certaines fois, il arrive que nous ayons besoin d'un motd (traduire : "message of the day"), spécifique ou non. Mais généralement c'est pour en avoir un plus... joli que celui de base.
À la fin de cet article, vous aurez ce message affiché à chaque connexion :
Welcome to Debian GNU/Linux 8.10 (jessie) (3.16.0-5-amd64).
System information as of: Thu Oct 25 11:57:36 CEST 2018
System load: 0.12 IP Address: 192.168.1.254
Memory usage: 63.8% System uptime: 2:23 hours
Usage on /: 42% Swap usage: 0.0%
Local Users: 0 Processes: 134
Préparer le terrain
Pour que cela fonctionne, il faut créer un dossier
/etc/update-motd.d
, puis trois fichiers (ou plus ou moins... à vous de voir). Les rendre exécutables, supprimer le fichier motd et enfin créer un lien symbolique entre les deux.
Allez, pour vous simplifier la vie, voici les commandes à exécuter :
mkdir /etc/update-motd.d/
cd /etc/update-motd.d/
touch {00-header,10-sysinfo,90-footer}
chmod +x /etc/update-motd.d/*
rm /etc/motd
ln -s /var/run/motd /etc/motd
On peut voir les différentes parties du messages créées en 3 fichiers : 00-header
, 10-sysinfo
et 90-footer
.
Bien entendu, vous pouvez créer un fichier 20-main
etc...
Ajouter un exécutable motd dans init.d pour Debian 8
Cette étape n'est pas utile pour Debian 9 (testé avec une netinstall Debian 9 pure), ça fonctionne très bien sans cet exécutable.
Commencez par créer un fichier dans /etc/init.d/motd
avec le code ci dessous :
#!/bin/sh
### BEGIN INIT INFO
# Provides: motd
# Required-Start: hostname $local_fs
# Required-Stop:
# Should-Start:
# Default-Start: 1 2 3 4 5
# Default-Stop:
# Short-Description: Create dynamic part of /etc/motd
# Description: /etc/motd is user-editable and static. This script
# creates the initial dynamic part, by default the
# output of uname, and stores it in /var/run/motd.dynamic.
# Both parts are output by pam_motd.
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/init/vars.sh
do_start () {
# Update motd
uname -snrvm > /var/run/motd.dynamic
}
do_status () {
if [ -f /var/run/motd.dynamic ] ; then
return 0
else
return 4
fi
}
case "$1" in
start|"")
do_start
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
stop)
# No-op
;;
status)
do_status
exit $?
;;
*)
echo "Usage: motd [start|stop|status]" >&2
exit 3
;;
esac
:
Remplir le fichier 00-header
Ajoutez le code ci dessous dans le fichier 00-header
#!/bin/sh
#
# 00-header - create the header of the MOTD
# Copyright (c) 2013 Nick Charlton
# Copyright (c) 2009-2010 Canonical Ltd.
#
# Authors: Nick Charlton
# Dustin Kirkland
#
# 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
#figlet $(hostname)
#printf "\n"
printf "Welcome to %s (%s).\n" "$DISTRIB_DESCRIPTION" "$(uname -r)"
printf "\n"
Remplir le fichier 10-sysinfo
Ajoutez le code ci dessous dans le fichier 10-sysinfo
#!/bin/bash
#
# 10-sysinfo - generate the system information
# Copyright (c) 2013 Nick Charlton
#
# Authors: Nick Charlton
#
# 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.
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)
ip=$(ifconfig $(route | grep default | awk '{ print $8 }') | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
activehost=$(cat /var/tmp/activehost)
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
Pour la ligne
ip=$(ifconfig $(route | grep default | awk '{ print $8 }') | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
, n'hésitez pas à la remplacer par ip=$(ip a show dev eth0 | grep "inet " | awk '{ print $2 }')
sur Debian 9 (et a remplacer eth0 par le nom de votre interface.Remplir le fichier 90-footer
Ajoutez le code ci dessous dans le fichier 90-footer
#!/bin/sh
#
# 90-footer - write the admin's footer to the MOTD
# Copyright (c) 2013 Nick Charlton
# Copyright (c) 2009-2010 Canonical Ltd.
#
# Authors: Nick Charlton
# Dustin Kirkland
#
# 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.
[ -f /etc/motd.tail ] && cat /etc/motd.tail || true
Avec ce code, vous pouvez en plus de tout le reste ajouter un fichier /etc/motd.tail
qui ajoutera un truc en fin du motd. C'est très pratique quand tout le reste est déployé automatiquement.
Ajouter des couleurs aux messages
Comme il s'agit de BASH, il peut être intéressant d'ajouter des couleurs sur certains messages.Voici donc une liste de variables (que personne n'hésitera à remplir) avec les codes couleur de BASH.
# Set colors
RED='\033[0;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
LIGHTGRAY='\033[00;37m'
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
NC='\033[0m' # No Color
Vous pourrez les utiliser avec la commande :
echo -e "${RED}"
Et pour éviter que la console ne se retrouve avec une couleur rouge à vie, ajoutez cela à la fin de votre motd :
echo -e "${NC}"
J'espère que ce petit bout de tuto vous sera utile :)
Article corrigé par Von