How to Change Lid Close Action in Ubuntu 18.04 LTS

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

For those who want it automatic shutdown, hibernate, or do nothing when laptop lid is closed, here’s how to do it by hacking on the configuration file.

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

sudo gedit /etc/systemd/logind.conf

Type in your password (no visual feedback while typing due to security reason) when prompts and hit Enter to continue.

logind-conf-bionic

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.
auto-hibernate

Save the file and finally restart the Systemd service to apply changes via command:

systemctl restart systemd-logind.service

restart-logind-bionic

In addition:

1. Previous steps do not add shutdown or hibernate options in the Power settings utility, but directly do the action when you close the laptop lid.

2. For some laptops, the hibernate function might not work. Run test command:

sudo apt install pm-utils && sudo pm-hibernate

After your computer turns off, switch it back on. Did previous opened applications re-open? If hibernate doesn’t work, check if your swap partition is at least as large as your available RAM.

About ml

ml is a part time stay-at-home dad who've been using Ubuntu Desktop for a few years. He writes in the free time and wishes to share some useful tips with Ubuntu beginners and lovers.

14 comments

  1. Thanks so much for the post.Really thank you! Great.

  2. Thanks for the tip.

    I’ll leave this question here in case anyone can help:
    Is there a way to differentiate what happens depending on the notebook being on battery power or plugged when closing the lid?

    Thank you.

    • The version of systemd used by ubuntu does not handle “HandleLidSwitchExternalPower” message. This means that neither systemd nor gnome can differentiate between lid events with power connected and disconnected

  3. Hi, thanks for this post! I have a related question: when the lid closes, I’d like it to lock, so that when I open it again, it asks for my password. Is there a similar way to do that?
    Thanks!
    -D

  4. This worked perfectly, thank you! I wanted to have this so when i remoted into my laptop it wouldn’t time out.

    many thanks!

  5. the fact that this is so difficult to do for normal users is exactly why people don’t want to use ubuntu

    • this isn’t difficult

      • But it’s completely undiscoverable. (I spent 10 minutes looking for the option before taking to Google.) It’s frustrating that this kind of thing is still necessary for such a basic customization. This is why people scoff at “this is the year of the Linux desktop” comments. (And I say this non-ironically as someone who wrote those type of comments for linux.com 20 years ago.)

        • 10 minutes? You don’t say! No OS is perfect, but Windows is much farther from perfect than Ubuntu 18.04. I think U18 is much better than U16 (desktop), aesthetically and functionally. So, a small issue like this is not that big of a deal. And compared to the issues in W10? Not even in the same league!

          Also… I just went straight to Google in the first minute and found my answer. You could be in W10 still looking for this kind of option days later.

  6. This worked for me on Ubuntu Server 18.04! Thank you

  7. Thanks for the article, it worked on my Ubuntu 18.04.

    `sudo systemctl hibernate` also checks if hibernate works.

  8. It worked for me for several months on my new HP laptop. I could use Bluetooth and play Spotify with the lid closed.
    Then open the lid press spacebar and I am back in my computer.
    Now suddenly when I press the spacebar I get a purple background, on the left my unity apps, I can see and move the mouse cursor but cannot do anything to continue where I left off.
    I can only press the pause in the top right, then screen turns black and when I press spacebar I have to give my password and only then I am back in my work.
    I think there must have been an update that changed some settings but cannot find where and which.
    Any help is welcome.