18

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

 $(uptime)

and

`uptime`

Is this possible?

ewwhite
  • 194,921
  • 91
  • 434
  • 799
Justin
  • 5,008
  • 19
  • 58
  • 82

3 Answers3

22

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

Læti
  • 2,075
  • 21
  • 33
  • 3
    it's not a common practise to use /etc/motd.sh –  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. – Læti Dec 20 '12 at 17:18
  • 1
    There seems to be a way of using cron to regularly replace the static motd message: http://www.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 01:42
  • 5
    Putting output like a MOTD in your profile is likely to break sftp. – Stuart P. Bentley Aug 17 '14 at 20:54
  • 6
    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
  • 1
    Using both `/etc/profile` and well as `/etc/profile.d` causes side effects of scripts being run NOT only during really first login (SSH/console) but also for example when you open a new `screen` window. The way to go is using PAM-based solution - see the other answers for them. – Greg Dubicki Feb 23 '20 at 00:44
9

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.

Greg Dubicki
  • 1,191
  • 1
  • 14
  • 30
Wtower
  • 584
  • 6
  • 11
  • 1
    Related [askubuntu post](https://askubuntu.com/questions/105689/how-is-etc-motd-updated). Force with `sudo run-parts /etc/update-motd.d/`. – Pablo A Oct 03 '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 01:41
1

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

Use with:

  1. Delete the default static /etc/motd.
  2. Make SSHD not show the static MOTD with lines PrintMotd no, Banner none, UsePAM yes (and optionally PrintLastLog no) in your /etc/ssh/sshd_config & reload sshd service.
  3. Add this line to your /etc/pam.d/sshd: session optional pam_motd.so motd=/run/motd.dynamic.
  4. Add your scripts to /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.

Greg Dubicki
  • 1,191
  • 1
  • 14
  • 30