一馬力のメモ帳

MVSからLinuxまで。基盤屋さんは眠らない

CentOS7.2でctrl-alt-delは完全に無効化できないぞって話

毎度。一馬力です。

ctrl-alt-del!

バルス!!と同じくらいキケンなキーワードです

Windowsに慣れ親しんだ人たちは困った時,お世話になるコマンドだと思います。

ctrl-alt-del。これ同じIAマシン上で動作するLinuxサーバで入力しちゃうと

悲惨な結末がまってます。

そう、サーバの再起動です。

Linuxサーバを運用している皆さんの中には

ctrl-alt-delを無効化している人も多いのではないでしょうか?

今回はそのctrl-alt-del無効化がCentOS7.2,RHEL7.2では完全に動作しない!!

という話です。

目次


ctrl-alt-delを無効化する方法

まずはctrl-alt-delを無効化する方法
CentOS7では”systemctl”コマンドで無効化します。

設定コマンド

systemctl mask ctrl-alt-del.target

コマンド実行結果

[root@CentOS7 system]#  systemctl mask ctrl-alt-del.target
Created symlink from /etc/systemd/system/ctrl-alt-del.target to /dev/null.
[root@CentOS7 system]# pwd
/etc/systemd/system
[root@CentOS7 system]# ll
lrwxrwxrwx. 1 root root    9  2月  4 22:24 ctrl-alt-del.target -> /dev/null

ctrl-alt-delを入力し再起動されない事を確認

設定後早速”ctrl-alt-del”を入力して動作を確認してみます、、、、。
とは言ってもBlog上では動作確認の結果をうまく伝えられないので

tail -f /var/log/messages コマンドを実行した上で”ctrl-alt-del”を入力してみると。

[root@CentOS7 ~]# tail -f /var/log/messages
Feb  4 21:44:38 CentOS7 systemd: Failed to enqueue ctrl-alt-del.target job: Unit ctrl-alt-del.target is masked. ★
Feb  4 21:44:38 CentOS7 systemd: Received SIGINT.

しっかり”ctrl-alt-del”が無効化されているログが表示されますね。(上の★部分)

これでIAマシンのバルス(”ctrl-alt-del”)からサーバを守れたわけです。

ctrl-alt-delを連打–>サーバ再起動 T_T

注意:本番稼働中のサーバで実験しないで下さい

さて、”ctrl-alt-del”。一回入力した程度ではなんともなかったので
向きになって連打してみます。

連打です。”ctrl-alt-del”を押しっぱなしでしても良いかもしれません。
バルス祭りです。すると、、、。

、、、、。。。再起動しちゃいましたね、、、。

再起動時のログ

さて再起動時のログを確認してみると

Feb  4 21:44:40 CentOS7 systemd: Failed to enqueue ctrl-alt-del.target job: Unit ctrl-alt-del.target is masked.Feb  4 21:44:40 CentOS7 systemd: Received SIGINT.
Feb  4 21:44:40 CentOS7 systemd: Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately. ★
Feb  4 21:44:40 CentOS7 systemd: Shutting down.

systemdさんから「2秒以内に7回”ctrl-alt-del”押されたから今すぐ再起動するね^^」(★部分)
という悲しいお知らせが、、。

何してくれてんだい!!って感じですね。
せっかくバルス(”ctrl-alt-del”)無効化したのに、意味ないじゃん!です。

 ”ctrl-alt-del”投入時の動作解析

こんにゃろっ!ってことで仕組みを調べてみましょう。

対象の確認

まずは,対象の確認から。
今回バルス(”ctrl-alt-del”)無効化がきかないのはCentOS7.2です。

[root@CentOS7 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@CentOS7 ~]# uname -a
Linux CentOS7 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@CentOS7 ~]#

解析

ログ(下の★部分)から,再起動するタイミングで
systemdさんからメッセージが出力されている事が分かります

Feb  4 21:44:40 CentOS7 systemd: Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately. ★

自分でsystemdですって名乗っているくらいなので,間違いなく彼(systemd)の仕業でしょう。
f:id:htbariki:20170430143023p:plain

systemdのソースコードダウンロード

こまったらソースコードです。
読めば動きがわかるってのはLinuxの良いところです。

systemdのソースコードのダウンロード方法はしたのコマンド例を参考にしてください

[root@CentOS7 systemd-219]# yumdownloader --source systemd
[root@CentOS7 systemd-219]# rpm -ivh systemd-219-30.el7_3.6.src.rpm
[root@CentOS7 systemd-219]# tar Jxvf systemd-219.tar.xz

再起動時のメッセージ出力箇所の特定(grep)

ソースコードを展開したので,次はメッセージから調べる場所を特定していきます。

“ Ctrl-Alt-Del was pressed more than 7 times within 2s “のメッセージを足がかりに
Systemdのどのコードを読めばよいかgrepを利用して特定していきます。

検索をかけた結果”/core/manager.c”から該当のメッセージは出力されているようですな

[root@CentOS7 systemd-219]# find ./ -type f -print |xargs grep "Ctrl-Alt-Del was pressed more than 7 times within 2s"
./src/core/manager.c:                                        log_notice("Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
./src/core/manager.c:                                        status_printf(NULL, true, false, "Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
[root@CentOS7 systemd-219]#

 /core/manager.cを覗いてみると

/core/manager.cを覗いてみると。。。ありました

 /core/manager.c  case SIGINT; * ratelimit_test();

ですね。

まったく。こんな思いやりはいらないのに。。。
2秒以内に7回呼び出されるとあえなくrebootです。

[root@CentOS7 systemd-219]# less ./src/core/manager.c

                case SIGINT:
                        if (m->running_as == SYSTEMD_SYSTEM) {

                                /* If the user presses C-A-D more than
                                 * 7 times within 2s, we reboot
                                 * immediately. */

                                if (ratelimit_test(&m->ctrl_alt_del_ratelimit))
                                        manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
                                else {
                                        log_notice("Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
                                        status_printf(NULL, true, false, "Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
                                        m->exit_code = MANAGER_REBOOT;
                                }

                                break;
                        }

                        /* Run the exit target if there is one, if not, just exit. */
                        if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) {
                                m->exit_code = MANAGER_EXIT;
                                return 0;
                        }

                        break;

解決策 (CentOS7.2.1511&Systemd 219-19.el7では回避不能)

対象のバージョン再確認

まずはOS。CentOS Linux release 7.2.1511です。

CentOS7
[root@CentOS7 systemd-219]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@CentOS7 systemd-219]#

つづいてSystemdのバージョン。219-19.el7と。。

[root@CentOS7 systemd-219]# yum list|grep systemd
systemd.x86_64                          219-19.el7                     @anaconda

systemdの最新コードを確認( github)

ソースを確認するとsystemd232ではsystemdのオプションが増えて
CtrlAltDelBurstAction=を指定する事で無効化できるようですな。

systemd/NEWS
https://github.com/systemd/systemd/blob/6b3d378331fe714c7bf2263eaa9a8b33fc878e7c/NEWS 

CHANGES WITH 232:

        * A new setting CtrlAltDelBurstAction= has been added to


          /etc/systemd/system.conf which may be used to configure the precise

          behaviour if the user on the console presses Ctrl-Alt-Del more often

          than 7 times in 2s. Previously this would unconditionally result in

          an expedited, immediate reboot. With this new setting the precise

          operation may be configured in more detail, and also turned off

          entirely.

man (systemd233)

ねんのためオプションの意味をmanでしらべてみます。

確認時点のmanのバージョンは”systemd233”でした

https://www.freedesktop.org/software/systemd/man/systemd-system.conf.html#CtrlAltDelBurstAction=

Options

All options are configured in the "[Manager]" section:

LogLevel=, LogTarget=, LogColor=, LogLocation=, DumpCore=yes, CrashChangeVT=no, CrashShell=no, CrashReboot=no, ShowStatus=yes, DefaultStandardOutput=journal, DefaultStandardError=inherit
Configures various parameters of basic manager operation. These options may be overridden by the respective command line arguments. See systemd(1) for details about these command line arguments.

CtrlAltDelBurstAction=
Defines what action will be performed if user presses Ctrl-Alt-Delete more than 7 times in 2s. Can be set to "reboot-force", "poweroff-force", "reboot-immediate", "poweroff-immediate" or disabled with "none". Defaults to "reboot-force”.

まとめ

  • CentOS7.2 systemd219ではctrl-alt-delを完全には無効化できない
  • systemd232からはCtrlAltDelBurstAction=で無効化できる

みなさんも運用中のサーバでバルス!!(ctrl-alt-del)→Rebootしちゃわないように気をつけて下さいね。

参考資料

 Ctrl-Alt-Del を無効化すす方法

RHEL7 で Ctrl-Alt-Del を無効にする - Red Hat Customer Portal
https://access.redhat.com/node/1521333 

 systemdのソースコード[github]

https://github.com/systemd/systemd/blob/6b3d378331fe714c7bf2263eaa9a8b33fc878e7c/NEWS 

試した環境

ホストOS

2500/15.4 MJLT2J/A MacBook Pro Retinaディスプレイ

MacBook Pro (Retina, 13-inch, Late 2013)
macOS Sierra
    システムのバージョン:     macOS 10.12.4 (16E195)
    カーネルのバージョン:     Darwin 16.5.0
VirtualBox
 VirtualBox バージョン 5.1.6 r110634 (Qt5.5.1)
 Oracle_VM_VirtualBox_Extension_Pack-5.1.6r110634

ゲストOS

[root@CentOS7 system]# uname -a
Linux CentOS7 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux