曦远 (DealiAxy)一份私人的文字刊物

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 许可协议。转载请注明出处!

相关文章

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

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

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

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

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

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

Nginx配置http跳转https-强制https访问

前言 最近上线了好几个项目,同时也申请了相应的SSL证书,不过发现了一个问题,用户在浏览器直接输入域名的时候,浏览器默认访问的是http站点,然而我们的网站只支持https访问,所以就访问不了咯。 其实可以在nginx全局配置里面把所有访问重定向到https,不过服务器有些站点是提供http服务的,所以只能每个站点分别配置。 根据搜索引擎找到的资料,本文总结了三种配置方法,有需要的朋友可以选择中意

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

Ubuntu-18-04-修改登录界面背景图

前言 Ubuntu 18.04用了一段时间了,在Gnome桌面的加持下,兔子 18.04 的颜值还不错,加上自己搭配了几套GTK主题,简直美滋滋,唯一美中不足的就是,Ubuntu的登录管理器换成GDM之后,登录界面背景没办法修改了,这怎么行呢,于是经过一番折腾,有了本文。 首先,准备一张图片 图片的尺寸呢,根据你的屏幕分辨率确定咯。 然后把图片放到/usr/share/backgrounds/目录

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

讨论

共 0 条

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

加载评论中…