RHEL 7 systemd: How to prevent root from rebooting until a prescribed action is taken

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux 7

Issue

  • We don't want root to be able to casually reboot or poweroff the system
    Instead, the root user must be required to run a specific command to allow reboots

Resolution

Option 0: Open a support case and ask for BZ 1269726

Option 1: Create a custom service that must be manually disabled to allow shutdown

  1. Create a new unit file:   /etc/systemd/system/reboot-guard.service

    [Unit]
    Description=Reboot Guard
    [Service]
    ExecStart=/bin/true
    [Install]
    RequiredBy=shutdown.target
    
  2. Create another new unit file:   /etc/systemd/system/start-reboot-guard.service

    [Unit]
    Description=Start Reboot Guard
    [Service]
    ExecStart=/bin/systemctl enable reboot-guard
    [Install]
    WantedBy=multi-user.target
    
  3. Execute:   systemctl daemon-reload

  4. Execute:   systemctl enable reboot-guard start-reboot-guard

  5. Attempt to reboot or shutdown and notice that it fails, e.g.:

    [root]# systemctl reboot
    Failed to issue method call: Transaction contains conflicting jobs 'stop' and 'start' for reboot.target. Probably contradicting requirement dependencies configured.
    [root]# shutdown -h now
    Failed to issue method call: Transaction contains conflicting jobs 'stop' and 'start' for poweroff.target. Probably contradicting requirement dependencies configured.
    
    Broadcast message from root@example.com on pts/1 (Mon 2015-08-17 13:37:10 EDT):
    
    The system is going down for power-off NOW!
    

    Note however that legacy tools (shutdown, reboot, poweroff, and halt) will still broadcast the wall message, despite the expected and desired failure

  6. To allow shutdowns, execute:   systemctl disable reboot-guard
    Example:

    [root]# systemctl poweroff
    Failed to issue method call: Transaction contains conflicting jobs 'stop' and 'start' for poweroff.target. Probably contradicting requirement dependencies configured.
    [root]# systemctl disable reboot-guard.service 
    rm '/etc/systemd/system/shutdown.target.requires/reboot-guard.service'
    [root]# systemctl poweroff
    Broadcast message from root@example.com on pts/1 (Mon 2015-08-17 13:37:50 EDT):
    
    The system is going down for system halt NOW!
    [  OK  ] Started Show Plymouth Power Off Screen.
    [  OK  ] Started Restore /run/initramfs.
    [  OK  ] Stopped LSB: Bring up/down networking.
             Stopping Network Manager...
    [  OK  ] Stopped Network Manager.
    ...
    [  OK  ] Reached target Unmount All Filesystems.
    [  OK  ] Stopped target Local File Systems (Pre).
             Stopping Remount Root and Kernel File Systems...
    [  OK  ] Stopped Remount Root and Kernel File Systems.
    [  OK  ] Reached target Shutdown.
    Cannot finalize remaining file systems and devices, giving up.
    dracut Warning: Killing all remaining processes
    Powering off.
    [ 4653.288570] Power down.
    
  7. Note that at startup start-reboot-guard.service will automatically re-enable reboot-guard.service

Option 2: Use reboot-guard, an UNSUPPORTED third-party application that prevents shutdown under specific configurable conditions

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.