The way to display messages to users who log onto your server is precisely via ssh banners. These are usually stored in /etc/motd
. So, since you want to automate it, you can do the following:
Make a copy of the existing file
sudo cp /etc/motd /etc/motd.orig
Manually edit the file and add your banner or, if you want this automated as well, do something like this:
echo "Server will be going down at 11:00pm UTC, March 1st, for updates." |
sudo tee -a /etc/motd
That will append your message to whatever is currently in the file.
Set up a cronjob that will restore the original file on reboot.
echo '@reboot root cp /etc/motd.orig /etc/motd' | sudo tee -a /etc/crontab
That's it. Anyone who logs in via ssh will now see your message and the message will go away on reboot.
BUT: this isn't a very good idea. For one thing, you will need to remember to remove the @reboot
crontjob after you've rebooted because otherwise, it will run on every reboot from now on. Also, the whole thing doesn't make much sense. The banner system is designed to be manually edited since it's a way for the sysadmin to pass messages to users. Since you will be taking the server down manually, applying your updates or whatever maintenance you need manually and rebooting manually, I don't understand why you would bother automating the removal of the banner. Just add it, do your thing and remove it when it is no longer relevant.
/etc/motd
. Alternatively, sure, you can set up a@reboot
cron job to do it for you. This is exactly why banners exist. The details, however, will depend on your operating system, which you haven't mentioned.at
). Seeman at
for details. Unixes power comes form many small tools working together. Don't expectssh
to do this for you.@reboot
cron job?