14

I've been able to successfully hibernate and resume my Ubuntu 12.04 Desktop PC using procedure that I've documented in this answer. However, I now have a problem that whenever the PC resumes, it does not locks the screen.

How can I lock the screen after the system resumes back from hibernation?

5 Answers 5

12

I have added an alias into ~/.bashrc file

alias hibernate='sudo echo "Hibernating..."; gnome-screensaver-command -l; sudo pm-hibernate;'

So that it locks the screen before hibernating.

1
  • Wonderful! Take an upvote...
    – Ajoy
    Apr 7, 2017 at 5:10
5

I've been able to workaround this issue, by using the following command

sudo pm-hibernate; gnome-screensaver-command -l

Do note that, you should run this as a normal user and not as root user for locking to work, because by default in Ubuntu, there is no password for the root user so after resume, lock-screen can be unlocked by simply shaking the mouse.

2
  • +1 Thanks a lot for the note that screensaver have to be run as normal user (not as root). It completely makes sense... That's the point... Thank you!
    – zbynour
    Jun 10, 2015 at 12:56
  • Worked fine for me: (Ubuntu 15.04 - 3.19.0-25-generic #26-Ubuntu SMP Fri Jul 24 21:17:31 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux)
    – cantoni
    Aug 6, 2015 at 12:00
3

i have another workaround. i copied this from /etc/acpi/sleep.sh to /etc/acpi/hibernate.sh just before pm-hibernate

if [ x$LOCK_SCREEN = xtrue ]; then
    if pidof xscreensaver > /dev/null; then
    for x in /tmp/.X11-unix/*; do
        displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
        getXuser;
        if [ x"$XAUTHORITY" != x"" ]; then
        export DISPLAY=":$displaynum"
        . /usr/share/acpi-support/screenblank
        fi
    done
    fi
fi

now i can hibernate with lock from power menu or a sleep button

1
  • It works beautifully on 14.04 with encrypted swap (and home), however I can't see sleep.sh there – just to share experience and push some keywords. Oct 1, 2014 at 19:26
0

Not on Ubuntu, but the following works for me:

systemd-inhibit --what sleep --why "Lock screen" --mode delay xdg-screensaver lock & systemctl suspend

Replace xdg-screensaver lock with the command to lock the screen on your system and systemctl suspend with systemctl hibernate if you want to lock the screen before hibernating.

This works by locking the screen in parallel to suspending the machine. However the screen locking command is running as inhibitor that delays the sleep. This way, systemd will wait until the screen is locked before executing.

If you have inhibitors that are blocking the suspend, add the -i flag at the end.

0

There is a better way which automatically locks the screen before hibernating with systemd

Create a file in /etc/systemd/system/lock-on-hibernate.service With the following content:

[Unit]
Description="Make extra sure to lock the screen when hibernating"

[Service]
Type=oneshot
ExecStart=loginctl lock-sessions

[Install]
WantedBy=hibernate.target

Then enable it with

sudo systemctl daemon-reload
sudo systemctl enable lock-on-hibernate.service 

Now hibernatw just like normal systemctl hibernate It will lock your screen automatically before you resume

Note: I'm on fedora 37 and your system may use different hibernation system. So in case make sure you use the correct WantedBy target. (e.gWantedBy=systemd-hibernate.target)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .