4

I need to change the value of MOTD using a script but I do not know how I should start. Am attaching sample MOTD.

           enter image description here

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
batil
  • 311
  • 2
  • 3
  • 12

3 Answers3

4

To generate a MOTD in the style you're showing in your example you'll likely want to make use of the banner command. You can feed it the output from the hostname command to get a nice banner of your server's name.

To make this your MOTD you'll simply want to direct the output from these command to the file /etc/motd, which is what's used for displaying the MOTD.

$ ...cmd... > /etc/motd

NOTE: The commands I'm going to show below can be swapped into ...cmd....

Example

$ banner $(hostname)

 #####   ######   #######  #######  #     #  #######   #####    #####   
#     #  #     #  #        #        ##    #  #        #     #  #     #  
#        #     #  #        #        # #   #  #        #        #        
#  ####  ######   #####    #####    #  #  #  #####    #  ####  #  ####  
#     #  #   #    #        #        #   # #  #        #     #  #     #  
#     #  #    #   #        #        #    ##  #        #     #  #     #  
 #####   #     #  #######  #######  #     #  #######   #####    #####   

This one liner will give you nearly what you want:

$ (banner "server"; \
    printf "Hostname: %s\nDate    : %s\nUptime  :%s\n\n", \
    "$(hostname -s)" "$(date)" "$(uptime)")

 #####   #######  ######   #     #  #######  ######   
#     #  #        #     #  #     #  #        #     #  
#        #        #     #  #     #  #        #     #  
 #####   #####    ######   #     #  #####    ######   
      #  #        #   #     #   #   #        #   #    
#     #  #        #    #     # #    #        #    #   
 #####   #######  #     #     #     #######  #     #  

Hostname: greeneggs
Date    : Thu Apr 24 22:39:23 EDT 2014
Uptime  : 22:39:23 up 3 days,  8:34,  6 users,  load average: 0.80, 1.06, 1.49

Printing boxes

In addition to using the command line tool banner to print banner text, you can use another helper tool called boxes to wrap a box around arbitrary text.

$ boxes -d shell -p a1l2 <(hostname -s)
##############
#            #
#  greeneggs #
#            #
##############

We can use this approach and scale it up to do what you want like so:

$ boxes -d shell -p a1l2 \
    <(banner "server"; \
        printf "Hostname: %s\nDate    : %s\nUptime  :%s\n" \
        "$(hostname -s)" "$(date)" "$(uptime)")
###################################################################################
#                                                                                 #
#                                                                                 #
#   #####   #######  ######   #     #  #######  ######                            #
#  #     #  #        #     #  #     #  #        #     #                           #
#  #        #        #     #  #     #  #        #     #                           #
#   #####   #####    ######   #     #  #####    ######                            #
#        #  #        #   #     #   #   #        #   #                             #
#  #     #  #        #    #     # #    #        #    #                            #
#   #####   #######  #     #     #     #######  #     #                           #
#                                                                                 #
#  Hostname: greeneggs                                                            #
#  Date    : Thu Apr 24 22:54:09 EDT 2014                                         #
#  Uptime  : 22:54:09 up 3 days,  8:49,  6 users,  load average: 0.63, 0.81, 1.09 #
#                                                                                 #
###################################################################################
slm
  • 363,520
  • 117
  • 767
  • 871
  • Hi slm, thanks it works, however i do not have the boxes tool, is there something like that i can use in aix? Many Thanks – batil Apr 25 '14 at 07:26
  • @zieggy - no idea. I would installed boxes or manually make a box myself and print the contents inside of it. – slm Apr 25 '14 at 12:03
  • thanks, generally answers my concern, will try to check how can i do the boxes! – batil Apr 28 '14 at 00:38
  • Is this dynamic ? Will the output change each time a user has logged in? – GC 13 Apr 26 '17 at 03:30
  • @GC13 - No, the date and uptime etc change every time the above command is run and written to /etc/motd. You need to create a cron job if you want this file to get periodically updated. – slm Apr 26 '17 at 03:35
  • Agree that it can be done using cron job. However, if I want to execute as a script dynamically, how can I do that? For example, I tried to use the above command in /etc/update-motd.d/01-mymotd. Here, 01-mymotd is a file created by me, and when I tried to execute it, I am getting "run part" error. However, the same command works when executed from another shell script. Thanks. – GC 13 Apr 26 '17 at 05:02
  • @GC13 - that error makes me think that since you're on Ubuntu there are commands within the example above that doesn't run in dash (https://wiki.ubuntu.com/DashAsBinSh). When run in your shell you're probably using Bash so it works. Try running the above with /bin/sh instead to see if it works there as well. – slm Apr 27 '17 at 02:25
  • @slm, I am using /bin/sh in 00-header under /etc/update-motd.d/. – GC 13 Apr 27 '17 at 04:45
1

If you want to generate dynamic MOTD, check out this up to date guide

https://ownyourbits.com/2017/04/05/customize-your-motd-login-message-in-debian-and-ubuntu/

Basically, you have to

1) create /etc/update-motd.d

2) place your script there

nachoparker
  • 879
  • 11
  • 7
  • 2
    Please don't answer with 'check out this link'. Try to make the answer self-contained describing all necessary steps in details. Then just leave a link for a source at the end. – ddnomad Apr 10 '17 at 11:53
0

motd is simply a file. From man motd:

NAME
       motd - message of the day

DESCRIPTION
       The  contents  of  /etc/motd  are  displayed by login(1) after a successful
       login but just before it executes the login shell.

      The abbreviation "motd" stands for "message of the day", and this file has
      been traditionally used for exactly that (it requires much less disk space
      than mail to all users).

FILES
      /etc/motd

As a script point of view, something as simple as that would be enough:

#!/bin/sh
printf "     Welcome to $(hostname -s)\n\n" > /etc/motd
Ouki
  • 5,842
  • 4
  • 23
  • 31