LoginSignup
21
18

More than 5 years have passed since last update.

Amazon LinuxでSELinuxを有効にする。

Last updated at Posted at 2015-02-12

はじめに

Amazon LinuxではSELinuxが最初から無効になっている。これはけしからんので有効にする。しかし、そのさいいくつか嵌まる箇所があったため、この文書にまとめておく。

環境

今回の例では次のAMIを使用した。

Amazon Linux AMI 2014.09.2 (HVM) - ami-18869819

SELinuxパッケージのインストール

Amazon Linuxでは、SELinuxそのものがインストールされていない。以下のパッケージをインストールし、関連するSELinux、及び、semanager等の設定ツールをインストールする。

bash
$ sudo yum -y install selinux-policy-targeted policycoreutils-python
(中略)
Complete!

SELinuxの有効化

パッケージをインストールしただけでは、SELinuxは有効になっていない。

bash
$ sudo getenforce
Disabled

最新のカーネルの起動パラメータに「security=selinux selinux=1」を追加し、起動時にSELinuxが有効になるようにする。

bash
$ cd /boot/grub
$ sudo cp -p menu.lst menu.lst.org
$ sudo vi menu.lst
# created by imagebuilder
default=0
timeout=1
hiddenmenu

title Amazon Linux 2014.09 (3.14.27-XX.XX.amzn1.x86_64)
root (hd0,0)
kernel /boot/vmlinuz-3.14.27-XX.XX.amzn1.x86_64 root=LABEL=/ console=ttyS0 LANG=en_US.UTF-8 KEYTABLE=us security=selinux selinux=1
initrd /boot/initramfs-3.14.27-XX.XX.amzn1.x86_64.img

(以下省略)

パッケージをインストールしただけでは、初期化用ルートファイルシステムが更新されていない為、再起動してもSELinuxが有効にならない。以下のコマンドを実行して初期化用ルートファイルシステムを手動で更新する。

bash
$ sudo dracut --force
$ ls -lt /boot/initramfs-*
-rw------- 1 root root 12129821  2月 12 18:28 /boot/initramfs-3.14.27-XX.XX.amzn1.x86_64.img
(以下省略)

SELinuxの設定ファイルの「SELINUX=disable」の部分を「SELINUX=permissive」に変更する。最初から「SELINUX=enforcing」に変更すると再起動時にコンテキストの再設定に失敗し、SSHでログイン出来なくなるため、この手順で変更する必要がある。

bash
$ cd /etc/selinux
$ sudo cp -p config config.org
$ sudo vi config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

次回起動時にファイルシステムのSELinuxコンテキストが再設定されるように以下のファイルを作成する。

bash
$ sudo touch /.autorelabel

設定を有効にするためにインスタンスを再起動する。

bash
$ sudo shutdown -r now

再起動後、SELinuxが有効になっている事を確認する。

bash
$ sudo getenforce
Permissive

SELinuxコンテキストが正しく設定されていることを確認する。

bash
$ ls -lZ /etc

SSHのポートを22以外に変更している場合、以下のコマンドを実行する。これを忘れるとSSHでログイン出来なくなる。もちろん、/etc/sshd_configを変更したときに注意書きがあったことを覚えていますよね:-)

bash
$ sudo semanage port -a -t ssh_port_t -p tcp <変更したポート番号>
bash
$cat /etc/ssh_config
(中略)
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
(中略)

別ターミナルからインスタンスにSSHで接続し、SELinuxのログにSSH関連のエラーが出力されないことを確認する。エラーが出ている状態で次以降の手順を行うと再起動後にSSHでログイン出来なくなるため、よく確認すること。

bash
sudo grep sshd /var/log/audit/audit.log

上記の確認をもう一度行うこと。

その後、SELinuxの設定ファイルの「SELINUX=permissive」の部分を「SELINUX=enforcing」に変更する。

bash
$ cd /etc/selinux
$ sudo cp -p config config.org
$ sudo vi config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

設定を有効にするためにインスタンスを再起動する。

bash
$ sudo shutdown -r now

再起動後、SELinuxが有効になっている事を確認する。

bash
$ sudo getenforce
Enforcing

参考

Red Hat Enterprise Linux/7/SELinux User's and Administrator's Guide/
4.4. Enabling and Disabling SELinux
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/SELinux_Users_and_Administrators_Guide/sect-Security-Enhanced_Linux-Working_with_SELinux-Enabling_and_Disabling_SELinux.html

21
18
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
21
18