如何防止意外重启 Linux(基于 systemd)

发布于 2023-03-18 15:22:23 字数 3252 浏览 116 评论 0

说是有一个 RedHat 的客户提出需求:为了防止意外重启 Linux,要求 root 用户明确地执行某条命令后才允许对 Linux 进程重启操作。我们都知道 systemd linux 中的 reboot 是基于 reboot.target 来实现的,因此最直观的做法应该就是 mask 掉这个 target 了。

lujun9972:~/ $ sudo systemctl mask reboot.target
[sudo] lujun9972 的密码:
Created symlink /etc/systemd/system/reboot.target → /dev/null.
lujun9972:~/ $ sudo systemctl reboot
Failed to reboot system via logind: Access denied
Failed to start reboot.target: Unit reboot.target is masked.
lujun9972:~/ $ reboot now
Failed to write reboot parameter file: 权限不够
lujun9972:~/ $ sudo reboot now
Failed to talk to init daemon.

然后 RedHat 工程师玩出了另一种花样(下面内容摘自于https://access.redhat.com/solutions/1580343):

  1. 创建一个 unit 文件 /etc/systemd/system/reboot-guard.service

    [Unit]
    Description=Reboot Guard
    [Service]
    ExecStart=/bin/true
    [Install]
    RequiredBy=shutdown.target
    

    这一步是核心操作了,由于 /bin/true 执行后立即停止,因此这个被 shutdown.target 依赖的单元被显示停止,那么 shutdown.target 单元本身也就跟着立即停止了。

  2. 再创建一个 unit 文件 /etc/systemd/system/start-reboot-guard.service

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

    这一步保证Linux启动后自动enable reboot-guard 服务

  3. 执行 systemctl daemon-reload 重新加载配置
  4. 执行 systemctl enable reboot-guard start-reboot-guard 启用新服务
  5. 再尝试重启就会失败了

    [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!
    
  6. 如果需要重启,那么只需要先执行 systemctl disable reboot-guard 禁止 reboot-guard 命令即可

    [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.
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

極樂鬼

暂无简介

文章
评论
26 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文