Can I put shell commands in the
/etc/motd login banner file? I have tried:
Is this possible?
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.Sign up to join this community
/etc/motd is only read and not executed, so technically speaking, you cannot put shell commands in there.
However, it's possible to execute a shell script at login time that will have the same result. This is usually achieved by adapting the
/etc/profile script that is executed each time a user logs in. A useful practice is to put the command you want to be executed in a script named
/etc/motd.sh and call this script from
/etc/profile, usually at about the end of it.
In Ubuntu servers there is a program called
update-motd from package
UNIX/Linux system adminstrators often communicate important information to console and remote users by maintaining text in the file /etc/motd, which is displayed by the pam_motd(8) module on interactive shell logins.
Traditionally, this file is static text, typically installed by the distribution and only updated on release upgrades, or overwritten by the local administrator with pertinent information.
Ubuntu introduced the update-motd framework, by which the motd(5) is dynamically assembled from a collection of scripts at login.
This collection of scripts lives under
/etc/update-motd.d/. For more information see this wiki page.
Another alternative to generating
/etc/motd, instead of having a script run at login is to have a cron job. Certainly, it is not the same, but I have met this approach sometimes in the past. Note however that this approach has been also used by
update-motd, but it has been abandoned because of problem encountered. You can find a bit more info about this here.
For Centos 7 servers I suggest using my port of
update-motd.d from Ubuntu.
Unlike other solutions it makes the dynamic MOTD actually show only once, when you SSH / login through a console (other solutions make it show when you open new
screen windows, for example).
Install it with:
yum install https://github.com/gdubicki/centos-pam-with-update-motd/releases/download/1.1.8-1022.3/pam-1.1.8-1022.el7.x86_64.rpm
UsePAM yes(and optionally
PrintLastLog no) in your
session optional pam_motd.so motd=/run/motd.dynamic.
/etc/update-motd.d, like on Ubuntu
Please see https://github.com/gdubicki/centos-pam-with-update-motd for the most up to date info.