5

How can I configure my Ubuntu 14.04, logging in using Gnome 3, to lock the screen when I close my laptop lid?

All Settings ► Power only offers Do nothing or Suspend as options when I close the lid.

All Settings ► Brightness & Lock is configured with Lock ON, Lock screen after Screen turns off. It doesn't seem to work.

I've tried editing /etc/systemd/logind.conf to set HandleLidSwitch=lock and rebooting; nothing changed.

  • Having changed nothing, it's now working as expected. headdesk Thank you for the suggestions, I hope they'll be of use to anyone searching for it later. – Alan De Smet Sep 9 '14 at 4:47
3

Easy way: after changing

HandleLidSwitch=lock

you have to restart the service

sudo systemctl restart systemd-logind
  • 1
    This should be the top answer, simple and effective – user689314 Nov 19 '17 at 0:26
3

There may be other simple method available for what you want but you can lock you screen using this script upon closing your lid. I found it on help.ubuntu.com. I am trying to keep it short. For detail explanation you can visit the link.

  • Make the environment variables available for root.

    gedit ~/export_x_info

    and paste the following contets

    # Export the dbus session address on startup so it can be used by any other environment
    sleep 5
    touch $HOME/.Xdbus
    chmod 600 $HOME/.Xdbus
    env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus
    echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus
    # Export XAUTHORITY value on startup so it can be used by cron
    env | grep XAUTHORITY >> $HOME/.Xdbus
    echo 'export XAUTHORITY' >> $HOME/.Xdbus
    
  • Make it eecutable using

    chmod 700 ~/export_x_info

  • You need to run it on startup. So add it to Startup Applications. Select Startup Programs from Gnome Menu, and click Add.

    Name:<Any name you want>
    Command:/home/your_user/export_x_info
    
  • Now you need to edit your /etc/acpi/lid.sh file which catch lid open and close event. Change user_name with your user name

    gksudo gedit /etc/acpi/lid.sh and paste

    /home/your_user/lid_event just after #!/bin/bash. Save and close.

  • Now you need to create a script for close event so that you can lock your screen.

    gedit ~/lid_event and paste the following.

    #!/bin/bash
    grep closed /proc/acpi/button/lid/LID0/state  
     if [ $? = 0 ]
     then
     source /home/user_name/.Xdbus
     DISPLAY=:0.0 su user_name -c "gnome-screensaver-command -l"
     fi
    

    Also check for /proc/acpi/button/lid/LID0/state file because LID0 may be LID.

  • Now restart your ACPI using

    sudo /etc/init.d/acpid restart

  • Make sure that the Power Management option for laptop lid closed is set to do nothing

1

Use this simple script. Don't forget to set all lid status by "Do nothing" option in your Power setting.

#! /bin/bash

while true
  do
    value=`cat  /proc/acpi/button/lid/LID0/state |cut -f2 -d:`
    if [ ! $value=open ]; then
            gnome-screensaver-command -l
    fi
  done

I saved this file by named lid_lock.sh in my Home directory and I run it by sh lid_lock.sh. If you want to run this script in every boot add this line in /etc/rc.local file at the end befor exit 0, .

sh /home/kasiya/lid_lock.sh &

"&" is used for make that exit.

  • 3
    Infinite while will create unnecessary load to system. – g_p Sep 9 '14 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.