返回文章列表文章索引阅读全文
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 许可协议。转载请注明出处!

相关文章

给你的Deepin系统换主题~

我现在的桌面: 因为美国封锁然后华为电脑用上Deepin系统的事情,Deepin系统最近名声大噪,其实Deepin本来的界面就很好看,号称打造最适合国人的系统不是盖的。 不过Deepin有一个坏处,就是默认主题的标题栏太大了…… 太占空间。所以我们要换主题折腾一下。 Deepin的桌面是Qt写的,所以首先要换KDE主题,然后再安排一下GTK3的主题,在Deepin系统设置里面换的主题就是GTK的,

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

Debian8-stable-升级Python版本到3-6

前言 最近把一个Python项目部署到服务器上,然而服务器上的Python版本实在是太旧了,于是着手进行更新。 互联网上搜索到的方法都是下载Python3.6的代码之后手动编译,然而在服务器上编译安装出了点问题,于是想到了Ubuntu的ppa方式,debian和Ubuntu本属同源,理论上Ubuntu的ppa,debian也是可以用的。 操作步骤如下: 添加软件源 add-apt-reposito

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

讨论

共 0 条

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

加载评论中…