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 许可协议。转载请注明出处!
相关文章
设置Linux默认的文件管理器
搜索了半天,也试了很久。 用以下命令可以: 使用Xfce的文件管理器 xdg-mime default Thunar.desktop inode/directory thunar.desktop换成你想要的文件管理器名称 再不行就只能把原来的那个文件管理器卸载掉= =。 我比较喜欢用Gnome的文件管理器: xdg-mime default org.gnome.Nautilus.desktop i
2025-10-27 · 系统配置 · 约 1 分钟
内存不够用?在Linux上使用swapfile配置交换空间
前言 几个月前我还兴高采烈写了篇文章说给笔记本升级了内存配置,从乞丐版的4G一跃成为16G高富帅,然而,这几个月来我备受电脑死机的折磨,我现在有理由怀疑我可能买了假的内存条,于是没有办法,只能换回原厂的4G内存条,~~(然后准备赚部台式机)~~,问题不大。 换上4G内存条之后效果显著,只开了一个谷歌浏览器,内存占用就已经到了80%了,JB家的IDE是决计不敢再用了,可是这样还是难以满足系统对内存空
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 分钟
Debian配置默认不开启图形界面
前言 Linux的图形界面实在是太耗资源了,同时开几个虚拟机感觉已经hold不住了,赶紧把这些Linux服务器的图形界面都关掉。 CLI:command Line Interface,命令行界面。 现在的Linux发行版默认登陆界面几乎都是默认为GUI界面,如果按照旧版本的修改inittab文件,在一些比较新的版本是没有这个文件的(Linux的启动服务机制发生改变) 修改Grub配置文件 打开gr
2025-10-27 · 系统配置 · 约 1 分钟
讨论
共 0 条加载评论中…