11

I generally want my laptop to be locked when it's suspended but not when I just suspended it because there is a use case in which entering my password after my laptop woke up from suspend is pretty cumbersome. A good compromise is to only require the login password if the laptop was suspended more than 10 minutes ago. How do I do this?

I use Ubuntu 16.04 with Unity.

UTF-8
  • 5,710
  • 10
  • 31
  • 67

3 Answers3

8

Create a file within /lib/systemd/system-sleep/, named e.g: lightdm:

sudo touch /lib/systemd/system-sleep/lightdm

make this file executable:

sudo chmod +x /lib/systemd/system-sleep/lightdm

Every time you "suspend" or "resume" your Ubuntu, this script going to be run.

Open it using your desired text editor, e.g: sudo nano /lib/systemd/system-sleep/lightdm, and paste this lines into it and then save it:

#!/bin/sh
set -e

case "$1" in
   pre)

    #Store current timestamp (while suspending)
    /bin/echo "$(date +%s)" > /tmp/_suspend 
    ;;

   post)
      #Compute old and current timestamp
      oldts="$(cat /tmp/_suspend)"
      ts="$(date +%s)"

      #Prompt for password if suspended > 10 minutes
      if [ $((ts-oldts)) -ge 600 ];
       then
         export XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
         /usr/bin/dm-tool lock
      fi

      /bin/rm /tmp/_suspend
   ;;
esac

What it does?

When you are putting your Ubuntu into "sleep" mode this script will save current timestamps, then while resuming system it will check old timestamps with the current one, if the different was more that "600" second (10 Minuets) it's going to show you "lightdm" lock screen otherwise it does nothing.

For the last step:

open "system settings" -> "Brightness & lock". Disable asking password after waking up from suspend, because we leave handling the lock screen to the script.

enter image description here

After reboot or shutdown you still need to enter your password.

derHugo
  • 3,356
  • 5
  • 31
  • 51
Ravexina
  • 55,668
  • 25
  • 164
  • 183
0

Add a script in /lib/systemd/system-sleep/ to unlock your session if system was suspended for a short time:

cd /lib/systemd/system-sleep/
sudo touch     unlock_early_suspend
sudo chmod 755 unlock_early_suspend
sudo -H gedit     unlock_early_suspend

With this content:

#!/bin/bash
# Don't ask for password on resume if computer has been suspended for a short time

# Max duration of unlocked suspend (seconds)
SUSPEND_GRACE_TIME=600

file_time()      {  stat --format="%Y" "$1";  }

unlock_session()
{
    # Ubuntu 16.04
    sleep 1; loginctl unlock-sessions
}

# Only interested in suspend/resume events here. For hibernate etc tweak this
if [ "$2" != "suspend" ]; then  exit 0;  fi

# Suspend
if [ "$1" = "pre" ]; then  touch /tmp/last_suspend;  fi

# Resume
if [ "$1" = "post" ]; then
    touch /tmp/last_resume
    last_suspend=`file_time /tmp/last_suspend`
    last_resume=`file_time /tmp/last_resume`
    suspend_time=$[$last_resume - $last_suspend]

    if [ "$suspend_time" -le $SUSPEND_GRACE_TIME ]; then
        unlock_session
    fi
fi
muru
  • 197,895
  • 55
  • 485
  • 740
lemonsqueeze
  • 1,634
  • 1
    Initially I thought your way (actively unlocking) is more elegant than the one of the other answer (which actively locks). But then I noticed that yours has a weird property: If I actively lock my screen, I want it to be locked until the password is entered. However, your script would unlock the screen if the computer was suspended for a short period of time while the screen is already locked. That's why I accepted the other answer. – UTF-8 Jun 15 '17 at 12:48
  • Indeed that would be a problem =) – lemonsqueeze Jun 15 '17 at 13:52
-2

I can help you with this. First, go to settings. Select this setting:

Brightness_&Lock

There will be a drop down menu that says Screen Turns Off.

Screen_Turns_Off

After clicking on the drop down menu change both settings so they look like this:

Lock_Screen

BJsgoodlife
  • 1,160
  • 5
  • 31
  • 59
  • This does not work. I set the time to 30 seconds for testing. Like this, I had to enter my password even after suspending the computer for only 5 seconds. Like this, I got right to my desktop even after suspending my computer for 3 minutes. – UTF-8 Jun 15 '17 at 11:06
  • Sorry about that, I forgot a step. – BJsgoodlife Jun 15 '17 at 13:58
  • This does not leave the computer in an unlocked state for a predetermined number of minutes after suspend is initiated as the OP specified. In fact, it locks it immediately when suspend is started. – b_laoshi Jun 19 '17 at 04:30