Ubuntu 17.10 and later: Don't suspend when lid is closed, just lock screen

To disable suspend on lid-close

  1. Install Tweaks (gnome-tweak-tool) by running

    sudo apt-get install gnome-tweak-tool
    
  2. Launch Tweaks, go to the "Power" section and disable suspend on lid-close.

    screenshot

To enable lock on lid-close

Undo the above steps before following this. Also, SAVE ALL YOUR WORK, the second command would require you to reboot.

  1. Add the lines below in /etc/systemd/logind.conf

    [Login]
    HandleLidSwitch=lock  
    
  2. Run

    sudo systemctl restart systemd-logind
    

Sources

  • freedesktop.org - logind.conf
  • How to Change Lid Close Action in Ubuntu 18.04 LTS - Tips on Ubuntu

In Ubuntu 18.04 Gnome desktop, there is no option in the Settings utility for configuring the laptop lid close actions. And Gnome Tweaks only offers a switch to enable / disable the "Suspend when laptop lid is closed" option.

For those who want it to shut down automatically, hibernate, or do nothing when the laptop lid is closed, here’s how to do it by hacking the configuration file:

  1. Open a terminal by pressing Ctrl+Alt+T or searching for “Terminal” from start menu. When it opens, run the following command:

    sudo gedit /etc/systemd/logind.conf
    
  2. When the files opens, uncomment the line #HandleLidSwitch=suspend by removing # in the beginning, and change the value to :

    • HandleLidSwitch=poweroff: shutdown / power off when lid is closed.
    • HandleLidSwitch=hibernate: hibernate when lid is closed (need to test if hibernate works).
    • HandleLidSwitch=ignore: do nothing.
    • HandleLidSwitch=suspend: suspend laptop when lid is closed.
  3. Save the file and finally restart the Systemd service to apply changes via command:

    systemctl restart systemd-logind.service
    

Source


You could install dconf-editor to change what happens when lid is closed.

sudo apt-get install dconf-editor

go to org.gnome.settings-daemon.plugins.power in dconf-editor. you can change the value of lid-close-ac-action and lid-close-battery-action for changing the action when lid closed.The possible values are 'suspend','hibernate','logout','shutdown','nothing'.

screenshot

Via CLI would be:

gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action suspend