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.
目录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:
| Option | Behavior Description |
|---|---|
ignore | Ignore the key press (Recommended for NAS) |
poweroff | Shut down (Default) |
reboot | Restart |
halt | Shut down but do not cut power |
kexec | Fast reboot |
suspend | Suspend (Hibernate to RAM) |
hibernate | Hibernate (Save state to disk) |
hybrid-sleep | Suspend + Hibernate |
lock | Lock 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:
- Set
HandlePowerKey=ignoreto prevent systemd from taking over. - Use
acpidto listen to/etc/acpi/events/powerbtnto 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 Item | Description |
|---|---|
NAutoVTs=6 | The number of automatically generated virtual terminals (tty). By default, the system creates /dev/tty1 through /dev/tty6. |
ReserveVT=6 | The virtual terminal number reserved by systemd (for graphical interface switching or emergency use). |
KillUserProcesses=no | Whether to forcibly terminate all processes after a user logs out. |
KillOnlyUsers= | Only enable the KillUserProcesses behavior for these users (whitelist control). |
KillExcludeUsers=root | Which users are excluded from KillUserProcesses control (root is excluded by default). |
InhibitDelayMaxSec=5 | The maximum wait time (in seconds) for an inhibited shutdown/suspend action. |
UserStopDelaySec=10 | How long to wait after a user logs out before actually terminating the session. |
HandlePowerKey=poweroff | The action when the power button is pressed (default is to power off). |
HandlePowerKeyLongPress=ignore | The action when the power button is long-pressed (default is to ignore). |
HandleRebootKey=reboot | The action when the "reboot button" is pressed. |
HandleRebootKeyLongPress=poweroff | The action when the "reboot button" is long-pressed. |
HandleSuspendKey=suspend | The action when the suspend key is pressed (usually the Sleep key on laptops or some keyboards). |
HandleSuspendKeyLongPress=hibernate | The action when the suspend key is long-pressed. |
HandleHibernateKey=hibernate | The action when the hibernate key is pressed. |
HandleHibernateKeyLongPress=ignore | The action when the hibernate key is long-pressed. |
HandleLidSwitch=suspend | The action when the laptop lid is closed (default is to suspend). |
HandleLidSwitchExternalPower=suspend | The lid-close action when the laptop is connected to external power. |
HandleLidSwitchDocked=ignore | The lid-close action when the laptop is docked (default is to ignore). |
PowerKeyIgnoreInhibited=no | Whether to ignore user program's inhibit (block) on the power key action. |
SuspendKeyIgnoreInhibited=no | Whether to ignore program's inhibit on the suspend key action. |
HibernateKeyIgnoreInhibited=no | Whether to ignore program's inhibit on the hibernate key action. |
LidSwitchIgnoreInhibited=yes | Whether to ignore program's inhibit on the lid-close action (usually set to yes). |
RebootKeyIgnoreInhibited=no | Whether to ignore program's inhibit on the reboot key action. |
HoldoffTimeoutSec=30s | Debounce protection: the minimum time interval between two adjacent events. |
IdleAction=ignore | The action after being idle for too long (default is to ignore). |
IdleActionSec=30min | The 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=yes | Whether to clear a user's IPC (message queues, semaphores, shared memory) after they log out. |
InhibitorsMax=8192 | The maximum number of supported inhibit locks (used to block suspend/shutdown, etc.). |
SessionsMax=8192 | The maximum number of concurrent active sessions supported. |
StopIdleSessionSec=infinity | How long an idle session is kept before being automatically terminated (default is not to terminate). |
References
- systemd-logind.service - Official logind service documentation
- logind.conf.d(5) - Support for extending logind configuration via drop-in files
- Inhibitor Locks - Controlling how programs can temporarily inhibit system behaviors like suspend/shutdown (e.g., preventing sleep during video playback)
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 分钟
在Linux上安装Windows字体
前言 Linux系统下为安装WPS后,由于版权问题并没有自带字体,而这些字体又是平时必须使用的,下面介绍为Linux系统添加字体的方法。 操作 由于Windows系统自带了完善的字体,所以可以从安装的windows系统下拷贝。首先执行命令: mkdir /usr/share/fonts/wps_symbol_fonts/ //创建WPS的字体目录 然后到windows字体目录下,C:/Win
2025-10-27 · 系统配置 · 约 1 分钟
Debian8-stable-升级Python版本到3-6
前言 最近把一个Python项目部署到服务器上,然而服务器上的Python版本实在是太旧了,于是着手进行更新。 互联网上搜索到的方法都是下载Python3.6的代码之后手动编译,然而在服务器上编译安装出了点问题,于是想到了Ubuntu的ppa方式,debian和Ubuntu本属同源,理论上Ubuntu的ppa,debian也是可以用的。 操作步骤如下: 添加软件源 add-apt-reposito
2025-10-27 · 系统配置 · 约 1 分钟
修改Linux系统的鼠标滚轮的速度
Linux默认的鼠标滚轮实在太慢了,总是让我有一种Linux很卡的错觉,而且还不像Windows那样可以直接修改所有程序中的鼠标滚轮速度,不过没事,可以用imwheel这个工具来修改~ 安装 Debian系: sudo apt install imwheel Arch: sudo pacman -S imwheel 配置 创建~/.imwheelrc文件 nano ~/.imwheelrc 编辑内
2025-10-27 · 系统配置 · 约 1 分钟
讨论
共 0 条加载评论中…