Immediately lock screen when I close laptop lid under Gnome 3 (14.04)

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.


Easy way: after changing

HandleLidSwitch=lock

you have to restart the service

sudo systemctl restart systemd-logind

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