返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
系统配置

Be careful not to accidentally shut down the NAS server! Modify Linux's power button function

NAS unexpectedly shut down offline? The power key is controlled by the stupid system. Modify `logind.conf` to make it ignore the power key, preventing accidental restarts or shutdowns. Anyone who uses it knows, don't treat NAS like a toy.

曦远 (DealiAxy)约 19 分钟
目录9 个章节

Preface

This is what happened

Today I wanted to use a service on the NAS and suddenly found it offline

I checked and realized it had shut down

Very strange, there haven't been any power outages these past few days...

I then analyzed the system logs

Noticed this log entry right before the shutdown

Jul 13 23:24:33 pve systemd-logind[1062]: Power key pressed short.
Jul 13 23:24:33 pve systemd-logind[1062]: Powering off...
Jul 13 23:24:33 pve systemd-logind[1062]: System is powering down.
Jul 13 23:24:33 pve systemd[1]: 104.scope: Deactivated successfully.

So it was an accidental press of the power button

This won't do. The NAS cannot just shut down by accident

Modifying Power Button Functionality

In Linux (including PVE), the behavior of the power button is controlled by systemd-logind. You can not only disable it but also change it to perform other actions, such as: suspend, hibernate, lock screen, ignore, etc.

Edit logind configuration file

Open the configuration file:

sudo nano /etc/systemd/logind.conf

Find this line (add it if it doesn't exist):

HandlePowerKey=poweroff

Then change it to the desired behavior, for example:

OptionBehavior Description
ignoreIgnore the key press (Recommended for NAS)
poweroffShut down (Default)
rebootRestart
haltShut down but do not cut power
kexecFast reboot
suspendSuspend (Hibernate to RAM)
hibernateHibernate (Save state to disk)
hybrid-sleepSuspend + Hibernate
lockLock screen (May require desktop environment support)

Restart the logind service to take effect

sudo systemctl restart systemd-logind

Extensions

Custom Script

If you want the power button to trigger a custom script, follow these steps:

  1. Set HandlePowerKey=ignore to prevent systemd from taking over.
  2. Use acpid to listen to /etc/acpi/events/powerbtn to trigger your own script.

For most PVE users, setting the power key to ignore is the most reliable approach to avoid issues like accidental shutdowns or unintended triggers.

Other Settings

If you are using a laptop as a server, you can also configure lid closure behavior and similar settings.

# /etc/systemd/logind.conf
HandlePowerKey=ignore
HandleRebootKey=ignore
HandleSuspendKey=ignore
HandleLidSwitch=ignore       # If a laptop chassis lid is present
HandleLidSwitchDocked=ignore

Configuration Quick Reference Table

Here are the configuration descriptions for the /etc/systemd/logind.conf file.

Configuration ItemDescription
NAutoVTs=6The number of automatically generated virtual terminals (tty). By default, the system creates /dev/tty1 through /dev/tty6.
ReserveVT=6The virtual terminal number reserved by systemd (for graphical interface switching or emergency use).
KillUserProcesses=noWhether to forcibly terminate all processes after a user logs out.
KillOnlyUsers=Only enable the KillUserProcesses behavior for these users (whitelist control).
KillExcludeUsers=rootWhich users are excluded from KillUserProcesses control (root is excluded by default).
InhibitDelayMaxSec=5The maximum wait time (in seconds) for an inhibited shutdown/suspend action.
UserStopDelaySec=10How long to wait after a user logs out before actually terminating the session.
HandlePowerKey=poweroffThe action when the power button is pressed (default is to power off).
HandlePowerKeyLongPress=ignoreThe action when the power button is long-pressed (default is to ignore).
HandleRebootKey=rebootThe action when the "reboot button" is pressed.
HandleRebootKeyLongPress=poweroffThe action when the "reboot button" is long-pressed.
HandleSuspendKey=suspendThe action when the suspend key is pressed (usually the Sleep key on laptops or some keyboards).
HandleSuspendKeyLongPress=hibernateThe action when the suspend key is long-pressed.
HandleHibernateKey=hibernateThe action when the hibernate key is pressed.
HandleHibernateKeyLongPress=ignoreThe action when the hibernate key is long-pressed.
HandleLidSwitch=suspendThe action when the laptop lid is closed (default is to suspend).
HandleLidSwitchExternalPower=suspendThe lid-close action when the laptop is connected to external power.
HandleLidSwitchDocked=ignoreThe lid-close action when the laptop is docked (default is to ignore).
PowerKeyIgnoreInhibited=noWhether to ignore user program's inhibit (block) on the power key action.
SuspendKeyIgnoreInhibited=noWhether to ignore program's inhibit on the suspend key action.
HibernateKeyIgnoreInhibited=noWhether to ignore program's inhibit on the hibernate key action.
LidSwitchIgnoreInhibited=yesWhether to ignore program's inhibit on the lid-close action (usually set to yes).
RebootKeyIgnoreInhibited=noWhether to ignore program's inhibit on the reboot key action.
HoldoffTimeoutSec=30sDebounce protection: the minimum time interval between two adjacent events.
IdleAction=ignoreThe action after being idle for too long (default is to ignore).
IdleActionSec=30minThe time considered idle (used with IdleAction).
RuntimeDirectorySize=10%The maximum space each user's /run/user/UID directory can use (relative to total memory).
RuntimeDirectoryInodesMax=The maximum number of inodes each user's /run/user/UID can use (no limit if not set).
RemoveIPC=yesWhether to clear a user's IPC (message queues, semaphores, shared memory) after they log out.
InhibitorsMax=8192The maximum number of supported inhibit locks (used to block suspend/shutdown, etc.).
SessionsMax=8192The maximum number of concurrent active sessions supported.
StopIdleSessionSec=infinityHow long an idle session is kept before being automatically terminated (default is not to terminate).

References

You can also view this directly on the system using the man command

man logind.conf

_EOF_

Be careful not to accidentally shut down the NAS server! Modify Linux's power button function

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

内存不够用?在Linux上使用swapfile配置交换空间

前言 几个月前我还兴高采烈写了篇文章说给笔记本升级了内存配置,从乞丐版的4G一跃成为16G高富帅,然而,这几个月来我备受电脑死机的折磨,我现在有理由怀疑我可能买了假的内存条,于是没有办法,只能换回原厂的4G内存条,~~(然后准备赚部台式机)~~,问题不大。 换上4G内存条之后效果显著,只开了一个谷歌浏览器,内存占用就已经到了80%了,JB家的IDE是决计不敢再用了,可是这样还是难以满足系统对内存空

2025-10-27 · 系统配置 · 约 1 分钟

Debian配置默认不开启图形界面

前言 Linux的图形界面实在是太耗资源了,同时开几个虚拟机感觉已经hold不住了,赶紧把这些Linux服务器的图形界面都关掉。 CLI:command Line Interface,命令行界面。 现在的Linux发行版默认登陆界面几乎都是默认为GUI界面,如果按照旧版本的修改inittab文件,在一些比较新的版本是没有这个文件的(Linux的启动服务机制发生改变) 修改Grub配置文件 打开gr

2025-10-27 · 系统配置 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…

Be careful not to accidentally shut down the NAS server! Modify Linux's power button function · StarBlog