How to disable sleep / suspend / hibernate HP keyboard buttons for Ubuntu 18.04?

Solution 1:

In addition to the Gnome Power settings you can set the systemd settings found in /etc/systemd/logind.conf:

My laptop looks like this:

$ cat /etc/systemd/logind.conf

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See logind.conf(5) for details.

[Login]
#NAutoVTs=6
#ReserveVT=6
#KillUserProcesses=no
#KillOnlyUsers=
#KillExcludeUsers=root
#InhibitDelayMaxSec=5
#HandlePowerKey=poweroff
#HandleSuspendKey=suspend
#HandleHibernateKey=hibernate
HandleLidSwitch=ignore
#HandleLidSwitchDocked=ignore
#PowerKeyIgnoreInhibited=no
#SuspendKeyIgnoreInhibited=no
#HibernateKeyIgnoreInhibited=no
#LidSwitchIgnoreInhibited=yes
#HoldoffTimeoutSec=30s
#IdleAction=ignore
#IdleActionSec=30min
#RuntimeDirectorySize=10%
#RemoveIPC=yes
#UserTasksMax=12288

Notice on my system the only option I've changed is HandleLidswitch. For your system I would override these defaults:

#HandleSuspendKey=suspend
#HandleHibernateKey=hibernate

to this:

HandleSuspendKey=ignore
HandleHibernateKey=ignore

systemd is a little unique in that a line beginning with a hashtag (#) is not only a comment but also represents the default action taken.

Unfortunately I don't have your keyboard to test this configuration.

NOTE: After saving changes either reboot to activate or use:

sudo systemctl restart systemd-logind.service

Solution 2:

Not a solution, but a workaround, as it disables suspend.
Follow these two steps:

  1. Add HandleSuspendKey=ignore to the [Login] section of /etc/systemd/logind.conf.
    If you have enabled debug level in logind logs you get log entries like:

    ... systemd-logind[518]: Refusing operation, as it is turned off. 
    

    in the journal but it still suspends on some other trigger.

  2. Add /etc/systemd/sleep.conf with:

    [Sleep]
    SuspendMode=false
    

    Now it still reacts on suspend key presses, but fails with:

    ...systemd-logind[518]: Failed to process message type=method_call  
    sender=:1.54 destination=:1.1 path=/org/freedesktop/login1  
    interface=org.freedesktop.login1.Manager member=Suspend cookie=26  
    reply_cookie=0 signature=b error-name=n/a error-message=n/a:  
    Sleep verb not supported in journal
    

As said: It's a dirty workaround until someone can find a better solution.

Solution 3:

Please try 'xev' to find out if the event (press button) is read by Linux. Maybe the button is direct triggering a function on the mainboard?

On the other hand, if you've got the keycode in xev, you should be able to remap the sleep button with something other. See: How do I remap certain keys or devices?

Solution 4:

It looks like the ability to configure this was removed from recent versions of gnome-settings-daemon in Ubuntu 18 and Ubuntu 20.

A quick and dirty solution is to killall gsd-media-keys which will prevent all media keys from being handled by gnome, likely including the lid switch. You'll likely have to have edited /etc/systemd/logind.conf to make sure logind handles the keys how you like, and restarted the logind service or your system.

To re-enable the ability to configure this, some small changes are needed to plugins/gsd-media-keys-manager.c inside the gnome-settings-daemon source. These changes are in a merge request at https://gitlab.gnome.org/GNOME/gnome-settings-daemon/-/issues/537 . Here is how to rebuild gnome-settings-daemon with configuration enabled. You'll have to enable source repositories in your package management system.

$ mkdir ~/local-debs
$ cd ~/local-debs

# install build dependencies
$ sudo apt-get build-dep gnome-settings-daemon

# extract distribution package source
$ sudo apt-get source gnome-settings-daemon

# download the changes
$ wget https://siasky.net/CABb_suQRoRKqFo9zJ9YvGIqlLcpila1OFjUtI-f7uS5xQ/suspend-button-settings

# apply the changes.  `true` as an editor skips editing by making it always successful.
$ cd gnome-settings-daemon-*/
$ patch -p1 < ../suspend-button-settings
$ EDITOR=true dpkg-source --commit . suspend-button-settings ../suspend-button-settings

# build the package, skipping tests and signing
$ DEB_BUILD_OPTIONS=nocheck dpkg-buildpackage -us -uc

# install the package
$ sudo dpkg -i ../*.deb

# reboot the system, relogin, or terminate and relaunch the `gsd-media-keys` process ...
# ... [do preferred choice] ...

# finally, disable the suspend button
$ gsettings set org.gnome.settings-daemon.plugins.power suspend-button-action "nothing"

# you can also set it to "interactive" instead of "nothing" to display a power popup