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.
23 Answers
Create a file within /lib/systemd/system-sleep/, named e.g: lightdm:
sudo touch /lib/systemd/system-sleep/lightdmmake this file executable:
sudo chmod +x /lib/systemd/system-sleep/lightdmEvery 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 ;;
esacWhat 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.
After reboot or shutdown you still need to enter your password.
2Add 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_suspendWith 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 2 I can help you with this. First, go to settings. Select this setting:
There will be a drop down menu that says Screen Turns Off.
After clicking on the drop down menu change both settings so they look like this:
3