Ubuntu – How to configure the screen to lock when I close the lid

laptoplock-screen

I've configured Ubuntu to put the screen in black when I close the lid of the laptop, but I want that, when I open it again, it demand the password like blocking the screen with Ctrl+Alt+L, but I can't find the correct option. Could you tell me?

Best Answer

I am having the same problem in my laptop with Ubuntu 11.10... While a "full" solution is not developed by the community (i.e., adding a 'lock' option in the power manager), I have implemented my own solution. In order to implement my solution, you have to do the following steps:

  1. Using the "Power manager" or "Gnome Tweak Tool" you should configure your laptop to "do nothing" when lid is closed. If you want to install the Gnome Tweak tool, you should use the following command:
    sudo apt-get install gnome-tweak-tool
  2. You should create a directory and a new script file:
    mkdir /etc/acpi/local
    gksudo gedit /etc/acpi/local/lid.sh.post
  3. The new file should contain the following code:
    #!/bin/sh
    
    #########################################################################
    ## Script written by Rafael Fernandes Lopes and Thyago Mendes Neves    ##
    ## Twitter: @rafaelf_l and @ThyagoNeves                                ##
    ##                                                                     ##
    ## Description: This script locks the screen when the laptop lid is    ##
    ## closed in Ubuntu 11.10 (Oneiric Ocelot).                            ##
    ##                                                                     ##
    ## This script can be freely redistributed, modified and used.         ##
    ## Any redistribution must include the information of authors.         ##
    ##                                                                     ##
    ## THIS SCRIPT HAS NO WARRANTY!                                        ##
    #########################################################################
    
    grep -q close /proc/acpi/button/lid/*/state
    if [ $? = 0 ]; then
        # Some system process is used to verify who is the current logged user.
        PROCESS_PID=`pidof gnome-session`
        CURRENT_USER=`ps up $PROCESS_PID | awk '{if ($1 != "USER") { print $1 }}'`
        DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PROCESS_PID/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`
    
        export DBUS_SESSION_BUS_ADDRESS
    
        su -c "gnome-screensaver-command -l" $CURRENT_USER
    fi
    
    Save it!
  4. After saving and closing the created script, you must set the execution permission:
    sudo chmod +x /etc/acpi/local/lid.sh.post
    Ok! Now, you can test the script closing your laptop!
Related Question