6

If I'm connected to my home wifi network, I do not want to have a lock screen when I resume from suspend or after X minutes of inactivity.

If I'm not connected to any wifi network or the name of the wifi network is different than my home network, I want the lock screen to be active.

How can I do that?

4 Answers4

4

You can use this shell script. Check this to get the info of your WiFi.

#!/bin/sh

trusted_essid="trusted-wifi-name"

while : do current_essid=$(iwgetid -r) if [ "$trusted_essid" = "$current_essid" ] then gsettings set org.gnome.desktop.lockdown disable-lock-screen true echo 1 else gsettings set org.gnome.desktop.lockdown disable-lock-screen false echo 2 fi sleep 60 done

Edit this file as root:

sudo gedit /etc/network/interfaces

add under wlan0 (or your Wifi interface, ls /sys/class/net | grep -o "wl[^\t]\+"):

post-up sh /path/to/wifi-smartlock.sh
Pablo Bianchi
  • 15,657
Wiffzack
  • 236
  • 1
  • 5
1

A modern way to do this is to use NetworkManager's dispatcher.d

Step 1 - Create a script that does what we want, in dispatcher.d:

sudo nano /etc/NetworkManager/dispatcher.d/99-disable-screen-lock

Step 2 - copy and past the following script:

#!/bin/bash -e

trusted_essid="YOUR-NETWORK-ESSID" user="YOUR-USERNAME"

if [ "$2" = "up" ]; then if [ "$CONNECTION_ID" = "$trusted_essid" ]; then if [ sudo -u $user gsettings get org.gnome.desktop.screensaver lock-enabled = "true" ]; then

        touch /home/$user/.screen_lock_temporarily_disabled
        sudo -u $user dbus-launch gsettings set org.gnome.desktop.screensaver lock-enabled 'false'
    fi
fi

fi

if [ "$2" = "down" ]; then if [ -f /home/$user/.screen_lock_temporarily_disabled ]; then

    sudo -u $user dbus-launch gsettings set org.gnome.desktop.screensaver lock-enabled 'true'
    rm /home/$user/.screen_lock_temporarily_disabled
fi

fi

exit 0

Step 3 - Set the script to executable

sudo chmod +x /etc/NetworkManager/dispatcher.d/99-disable-screen-lock

That's it. The script basically is run every time you connect or disconnect to any network. It then checks if the network you just connected to is the one you trust, and if the screenlock is currently enabled. If this is the case, then it disables the screen lock, and generates a temporary file to keep in mind that it was this script that disabled the screen lock. This is important because we don't want the script to be enabling the screen lock if we manualy (and intentionaly) disabled it for any reason.

Every time you disconnect from any network, the script checks for the temprary file and re-enables the screen lock

Hope you find it useful!

kairos
  • 11
  • 1
  • Welcome to AskUbuntu and thank you for your contribution. Please take the [tour]. This is a well formatted and explained answer. In the future, please do not finish off your answers with irrelevant text such as "hope this helps", "this should work", "let me know if this works", etc. These are common sayings in personal conversations, but Stack Exchange Q&A's are not designed to be conversational. It is always implied that you hope your answer will be found useful, but it never needs to be said in your answer. Thanks again for your answer. – mickmackusa Feb 05 '22 at 09:26
0

For those who use Cinnamon I have prepared a solution here:

https://github.com/cookiebinary1/trusted-wifi-area

Main bash script (check github for more info):

#!/bin/sh

current_essid="" config_file=~/.trusted-wifi-area while : do new_essid=$(iwgetid -r) if [ "$new_essid" = "" ] then new_essid="no-wifi" fi

if [ "$new_essid" != "$current_essid" ] then current_essid="$new_essid" echo "New wifi: $new_essid" killall cinnamon-screensaver sleep 5s

  if grep -q "$new_essid" "$config_file"; then
    cinnamon-screensaver --disable-locking & echo "Restart screensaver without locking..."
  else
    cinnamon-screensaver & echo "Restart screensaver with locking..."
  fi

fi sleep 1s done

0

When I connect to my home network, I run a script to send an unlock command to gnone-screensaver. Here's what I came up with:

File /etc/network/if-up.d/smart-unlock

#!/bin/sh
runuser -l marc -c '/path/to/smart_unlock.py'

Replace marc with your current $USER.

File /path/to/smart_unlock.py

import os

ssid = 'trusted-wifi-name'

if os.system("/sbin/iwgetid | /bin/grep %s" % ssid) == 0: for j in range(2): os.system( ". ~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-%s && " "export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID && " "dbus-send --session --dest=org.gnome.ScreenSaver --type=method_call --print-reply --reply-timeout=20000 /org/gnome/ScreenSaver org.gnome.ScreenSaver.SetActive boolean:false" % j)

Pablo Bianchi
  • 15,657