How to Prevent a Linux System from Sleeping via Terminal

If you are managing a Linux server that controls digital signage, monitors security cameras, or runs a critical background script, you cannot afford for the system to unexpectedly go to sleep or enter suspend mode due to aggressive power-saving protocols.

While desktop environments like GNOME provide graphical power settings, headless servers require command-line intervention. By utilizing the systemctl utility, you can easily block the operating system from ever entering a sleep state.

How to Mask Sleep and Suspend Targets

Modern Linux distributions use systemd to manage power states via specific “targets.” To permanently prevent the system from sleeping, you must “mask” these targets. Masking a service completely disables it by linking it to /dev/null, making it impossible for the system (or any user) to accidentally trigger it.

  1. Open your terminal emulator or connect to your server via SSH.
  2. Execute the following command with root privileges to mask the primary sleep-related targets:
    sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
  3. Press Enter. You should see output confirming that symlinks have been created pointing to /dev/null.

Your Linux machine is now strictly prohibited from entering any form of sleep, suspension, or hibernation, regardless of how long it sits idle.

How to Revert the Changes

If you later decide to repurpose the server into a standard workstation and want to restore normal power-saving features, you must unmask the targets.

  1. Open the terminal.
  2. Execute the following command:
    sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
  3. Press Enter. The terminal will confirm that the symlinks to /dev/null have been removed.

After unmasking, your system will immediately regain the ability to sleep and hibernate according to its configured power plan.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.