Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

 $(uptime)

and

`uptime`

Is this possible?

share|improve this question
up vote 10 down vote accepted

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

share|improve this answer
    
it's not a common practise to use /etc/motd.sh – user130370 Dec 20 '12 at 16:51
    
@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
    
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
3  
Putting output like a MOTD in your profile is likely to break sftp. – Stuart P. Bentley Aug 17 '14 at 20:54
    
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 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.

share|improve this answer

Yes, it's possible... Sorta.

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

share|improve this answer
    
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
3  
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 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.