Can I put shell commands in the /etc/motd login banner file? I have tried:




Is this possible?


/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.

  • 3
    it's not a common practise to use /etc/motd.sh – user130370 Dec 20 '12 at 16:51
  • 1
    @EricDANNIELOU Yes, one can use whatever script name he/she wants. I just removed the common adjective that was not appropriate. Still, having it named this way makes it easy to spot and know what purpose it serves. – Tonin Dec 20 '12 at 17:18
  • 1
    There seems to be a way of using cron to regularly replace the static motd message: md3v.com/create-a-linux-server-status-motd I think that this profile script seems to be a better way, less moving parts. – CMCDragonkai May 30 '14 at 1:42
  • 4
    Putting output like a MOTD in your profile is likely to break sftp. – Stuart P. Bentley Aug 17 '14 at 20:54
  • 4
    You can place the motd.sh script inside /etc/profile.d/ with permissions 755. This way you wouldn't need to call it from /etc/profile. – Itay Grudev May 31 '16 at 19:28

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.

  • 1
    Related askubuntu post. Force with sudo run-parts /etc/update-motd.d/. – Pablo Bianchi Oct 3 '17 at 22:25
  • 1
    This also minimizes the startup login lag since you don't have to evaluate a script at login (assuming it gets more complex than uptime). – ayman Oct 15 '17 at 1:41

Yes, it's possible... Sorta.

Look up creating a dynamic MOTD. This is an abbreviated form of this documented process.

  • Do I just need to add session optional pam_motd.so to /etc/pam.d/login then make /etc/motd executable? – Justin Dec 19 '12 at 4:21
  • 4
    We really do prefer that answers contain content not pointers to content. Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – Iain Dec 19 '12 at 12:10
  • The dynamic MOTD link essentially says you can add commands which print a message to /etc/profile, so this is equivalent to Tonin's answer. – Andre Holzner Apr 4 '14 at 7:14

protected by HopelessN00b Mar 10 '16 at 10:48

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.