Is it possible to put commands in /etc/motd?

/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 libpam-modules:

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.