网站搜索

如何在 Linux 中禁用挂起和休眠模式


在本文中,我们将向您介绍如何在 Linux 系统上禁用挂起休眠模式。但在此之前,让我们简要概述一下这两种模式。

当您挂起 Linux 系统时,您基本上是激活或将其置于睡眠模式。尽管计算机仍然处于通电状态,但屏幕会关闭。此外,您的所有文档和申请仍保持打开状态。

当您不使用系统时,挂起系统有助于节省电量。只需单击鼠标或点击任意键盘按钮即可恢复使用系统。有时,您可能需要按电源按钮。

Linux中有3种挂起模式:

  • 挂起至 RAM(正常挂起):这是大多数笔记本电脑在一段时间内不活动或在 PC 使用电池运行时合上盖子时自动进入的模式。在此模式下,电源保留给 RAM,并且大多数组件都被切断。
  • 挂起到磁盘(休眠):在此模式下,机器状态被保存到交换空间中并且系统完全断电。然而,打开它后,一切都会恢复,并且您可以从离开的地方继续。
  • 暂停到两者(混合暂停):这里,机器状态被保存到交换区中,但系统不会关闭。相反,PC 会挂起至 RAM。电池未使用,您可以安全地从磁盘恢复系统并继续工作。此方法比挂起到 RAM 慢得多。

在 Linux 中禁用挂起和休眠

为了防止您的 Linux 系统挂起或进入休眠,您需要禁用以下systemd目标:

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

您将得到如下所示的输出:

hybrid-sleep.target
Created symlink /etc/systemd/system/sleep.target → /dev/null.
Created symlink /etc/systemd/system/suspend.target → /dev/null.
Created symlink /etc/systemd/system/hibernate.target → /dev/null.
Created symlink /etc/systemd/system/hybrid-sleep.target → /dev/null.

然后重新启动系统并重新登录。

使用以下命令验证更改是否已生效:

sudo systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target

从输出中,我们可以看到所有四种状态都已被禁用。

在 Linux 中启用挂起和休眠

要重新启用挂起休眠模式,请运行以下命令:

sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target

这是您将得到的输出。

Removed /etc/systemd/system/sleep.target.
Removed /etc/systemd/system/suspend.target.
Removed /etc/systemd/system/hibernate.target.
Removed /etc/systemd/system/hybrid-sleep.target.

要验证这一点,请运行命令;

sudo systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target

要防止系统在关闭盖子时进入挂起状态,请编辑 /etc/systemd/logind.conf 文件。

sudo vim /etc/systemd/logind.conf

将以下行附加到文件中。

[Login] 
HandleLidSwitch=ignore 
HandleLidSwitchDocked=ignore

保存并退出文件。请务必重新启动以使更改生效。

我们关于如何在 Linux 系统上禁用挂起休眠模式的文章到此结束。我们希望本指南对您有所帮助。非常欢迎您的反馈。