LoginSignup
2
0

More than 3 years have passed since last update.

さくらVPSのCentOS7のひな形のSELinuxがdisableになってたのでenforcingに変更(一筋縄ではいかない)

Last updated at Posted at 2019-12-24

はじめに

(書きかけで5ヵ月も放置してすいません。)

さくらVPSでは複数のOSのひな型が用意されています。

デフォルトで選べるCentOS7 x86_64を導入してみたところ、、

あれれSELinuxがdisableになっています。

5. SELinux
> SELinux は無効化された状態となっています。
> 設定ファイル    /etc/sysconfig/selinux
> 設定内容  SELINUX=disabled

なんてことだ。有効にしなくては。

SELinuxを有効にしてみよう(失敗例)

手順としては以下のように行いました。

  • yum update
  • reboot
  • SELinuxをEnforcingに修正
/etc/selinux/config
SELINUX=enforcing
  • 再度reboot
  • すると、ログインできなくなってしまいました。

sshでリモートログイン:
以下のエラーで失敗。

/bin/bash: Permission denied
Connection to xx.xx.xx.xx closed.

VNC: コンソール:
CUIでパスワード入力画面が表示されるが,ログインに失敗する.

  • この後、どうにもならなくなり、OSの再インストールをせざるをえませんでした。

小手先な作業ではダメ

小手先の作業ではダメのようです。検索すると以下の文献がみつかりました

5.4. SELinux の有効化および無効化

Selinuxのファイルが不足しているようです。文献に従い以下の手順で導入しました。

  • OS再インストール
    (以降sshでリモートログインで作業)

  • yum update

  • reboot

  • SELinuxパッケージ追加

$ yum install selinux-policy-targeted selinux-policy libselinux \
 libselinux-python libselinux-utils policycoreutils \
 policycoreutils-python setroubleshoot setroubleshoot-server \
 setroubleshoot-plugins policycoreutils-gui setroubleshoot mcstrans

きちんと確認してませんが、さくらVPSではファイルがいくつも欠落しているようです。

  • reboot
  • SELinuxをpermissiveに変更
/etc/selinux/config
SELINUX=permissive
  • reboot
  • 以下のコマンドでSELinuxのエラー確認し、エラーの無い事を確認。(コマンドは文献参照)
$ grep "SELinux is preventing" /var/log/messages
  • SELinuxをenforcingに変更
/etc/selinux/config
SELINUX=enforcing
  • reboot

  • 再びログイン出来なくなった。

なぜだ?

いろいろ思考錯誤した結果以下の事がわかりました。

SELinuxのラベルですが以下のように変化してました。

  • OSインストール直後
$ ls -lZ  /usr/sbin/sshd
-rwxr-xr-x. root root system_u:object_r:sshd_exec_t:s0 /usr/sbin/sshd

ラベルあり。

  • yum update後
$ ls -lZ  /usr/sbin/sshd
-rwxr-xr-x root root ?                                /usr/sbin/sshd

ラベル消失

  • SELinuxをpermissiveにして再起動

文献によると/etc/selinux/configでSEinuxを有効にして再起動するとラベルの再付与が行われると記載されている。しかし、

$ ls -lZ  /usr/sbin/sshd
-rwxr-xr-x. root root system_u:object_r:unlabeled_t:s0 /usr/sbin/sshd

よく見るとラベルが変である。なぜunlabeledなんだ?

次にSELinuxを有効にしたのち、文献掲載のコマンドでエラーが無い事を確認したが、このエラーはすぐには出ない事がわかった。OS起動後2-3分してから確認すると以下のようなエラーが複数報告されていた。

$ grep "SELinux is preventing" /var/log/messages
May 11 15:38:35 hostname.example.com setroubleshoot: SELinux is preventing /usr/sbin/sshd from write access on the fifo_file fifo_file. For complete SELinux messages run: sealert -l xx-xx-xx-xx-xx

どうやらラベルが正しく付与されてないようです。

SELinuxラベルの強制再付与

SELinuxラベルの強制再付与を行ってみました。

$ touch /.autorelabel
$ reboot

ラベルが正しく付与されました。

$ ls -lZ  /usr/sbin/sshd
-rwxr-xr-x. root root system_u:object_r:sshd_exec_t:s0 /usr/sbin/sshd

文献と挙動が変わってるようですね。

まとめ

まとめると以下のような流れでSELinuxの最有効化ができました。

  • OS再インストール
    (以降sshでリモートログインで作業)

  • yum update

  • reboot

  • SELinuxパッケージ追加

$ yum install selinux-policy-targeted selinux-policy libselinux \
 libselinux-python libselinux-utils policycoreutils \
 policycoreutils-python setroubleshoot setroubleshoot-server \
 setroubleshoot-plugins policycoreutils-gui setroubleshoot mcstrans
  • reboot
  • SELinuxをpermissiveに変更
/etc/selinux/config
SELINUX=permissive
  • reboot

  • SELinuxラベルの強制再付与

$ touch /.autorelabel
  • reboot

  • OS起動後2-3分待ち、以下のコマンドでSELinuxのエラー確認し、エラーの無い事を確認。

$ grep "SELinux is preventing" /var/log/messages
  • SELinuxをenforcingに変更
/etc/selinux/config
SELINUX=enforcing
  • 再度reboot

以上でSELinuxの再有効化ができました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0