From man 5 systemd-sleep.conf:
First, create the /etc/systemd/sleep.conf.d directory:
# mkdir -p /etc/systemd/sleep.conf.d
Then, in /etc/systemd/sleep.conf.d/hibernate-timeout.conf, place the
following, substituting 3600 for the amount of time (in seconds) before a
hibernation should occur after sleep:
[Sleep]
HibernateDelaySec=3600
Normally, when the lid of your laptop is closed, the “suspend” action is run.
This is equivalent to running systemctl suspend. In this mode, the system
will remain suspended, and will not hibernate after a certain amount of time.
We’ll tell the system to use the equivalent of systemctl suspend-then-hibernate
during certain situations (such as closing the lid).
First, create the /etc/systemd/logind.conf.d directory:
# mkdir -p /etc/systemd/logind.conf.d
Then, in /etc/systemd/logind.conf.d/auto-hibernate.conf, place the following:
[Login]
HandleSuspendKey=suspend-then-hibernate
HandleLidSwitch=suspend-then-hibernate
HandleLidSwitchExternalPower=suspend-then-hibernate
Finally, for the changes to take effect, either restart your computer or restart the logind service.
NOTE: Restarting the logind service will log you out of any graphical sessions you may be logged in to.
# systemctl restart systemd-logind.service
If you’re running Ubuntu or Fedora, you may also need to enable hibernate for this to work properly.