Stop HDDs spinning up on ssh login in Ubuntu server

Please see Update at the bottom! (04.01.2023)

Ever wondered why your harddrives are spinniung up nearly everytime you login to your homeserver over ssh? fsck is the culprit!

Well kind of. Everytime you login to Ubuntu via ssh, you get a nice overview of your system. (example below) This will show some general information about your system like average load, memory usage and tempeature. Other interesting things are the availability of security critical updates and the last time someone loged in with this account.

This welcome message is called message of the day or motd for short. And here it gets interesting. Part of this motd is also a check of the available harddrives for future fsck error checks. In the standard configuration this motd will check once every hour (3600s) if any of the installed HDDs need to be checked by fsck at the next reboot.

Everytime this check is triggered your HDDs will spinup. Since I dont use my data drives not that much, they are asleep most of the time. And they also should stay asleep when I access my server remotely to reduce the amount of spin ups.

To do this you need to edit the motd script here:

nano /usr/lib/update-notifier/update-motd-fsck-at-reboot

Exchange the value of 3600 seconds in line 29 with something more to your liking. I entered 86400 which equals a day in seconds.

now=$(date +%s)
if [ $(($stampt + 86400)) -lt $now ] || [ $stampt -gt $now ] \
   || [ $stampt -lt $last_boot ]
then
    #echo $stampt $now need update
        NEEDS_FSCK_CHECK=yes
fi

to save and exit press CTRL+x > y > ENTER

The change should take effect immediately and your harddrives should stay quiet!

Source: https://lumanet.de/sleeping-disks-spin-up-at-ssh-login-why-it-happens-and-how-to-get-rid-of-it and https://askubuntu.com/questions/282245/ssh-login-wakes-spun-down-storage-drives

Update:

There is an aknowledged bug in the script, see:

Bug #1957863 “coding errors in update-motd-fsck-at-reboot script...” : Bugs : update-notifier package : Ubuntu
The script /usr/lib/update-notifier/update-motd-fsck-at-reboot contains some coding errors.One of the errors hides the other error: 1. It sets two variables NEEDS_FSCK_CHECK and NEED_FSCK_CHECK (missing ‘S’) but only checks one of them (NEEDS_FSCK_CHECK). Because of this error, the script doesn’t…

Also there is a much easier way to disable the spinup of the drive at all, simply by setting the ENV variable every hour via CRON:

01 * * * * root f=/var/lib/update-notifier/fsck-at-reboot ; test -e $f && touch $f