openSUSE Leap 16

SELinux : 動作モードの設定2025/10/22

 

SELinux (Security-Enhanced Linux) の基本操作/設定です。

SELinux を設定することにより、各種リソースへの強制アクセス制御 (MAC - Mandatory Access Control) 機能が利用可能となります。

[1] SELinux が現在利用可能かどうかは、現在の動作モードを表示することで確認できます。
(システムインストール時のデフォルト動作モードは Enforcing)
# 現在の動作モード表示

dlp:~ #
getenforce

Enforcing
# enforcing   ⇒  MAC 有効な状態 (デフォルト)
# permissive  ⇒  ポリシーに従って監査ログの記録のみを行う (アクセス拒否はしない)
# disabled    ⇒  無効な状態

# 以下でも確認可
# [Current mode] 行が該当

dlp:~ #
sestatus

SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      33
[2] [permissive] ⇔ [enforcing] の動作モード切り替えは [setenforce] コマンドで変更可能です。
ただし、システム再起動すると元々の設定に戻ります。また [disabled] への一時的な切り替えはできません。
dlp:~ #
getenforce

Enforcing
# [setenforce 0] で [Permissive] に切り替え

dlp:~ #
setenforce 0

dlp:~ #
getenforce

Permissive
# [setenforce 1] で [Enforcing] に切り替え

dlp:~ #
setenforce 1

dlp:~ #
getenforce

Enforcing
[3] 動作モードを恒久的に変更する場合は設定ファイルで設定します。
dlp:~ #
vi /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELinux can be completly disabled with the "selinux=0" kernel
# commandline option.
#
# SELINUX= can take one of these two values:
#     enforcing  - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
# Previously SELinux could be disabled by changing the value to
# 'disabled'. This is deprecated and should not be used anymore.
# If you want to disable linux add 'selinux=0' to the kernel
# command line. For details see
# https://github.com/SELinuxProject/selinux-kernel/wiki/DEPRECATE-runtime-disable
# 設定する動作モードに変更
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
SELINUXTYPE=targeted

# 変更後はシステム再起動

dlp:~ #
[4] SELinux を無効化する場合は、カーネルパラメーターに無効化の設定をします。
dlp:~ #
vi /etc/default/grub
# 11行目 : [selinux=0] に変更
GRUB_CMDLINE_LINUX_DEFAULT="mitigations=auto quiet security=selinux selinux=0"

# 変更を反映

dlp:~ #
grub2-mkconfig -o /boot/grub2/grub.cfg

Generating grub configuration file ...
Found theme: /boot/grub2/themes/openSUSE/theme.txt
Found linux image: /boot/vmlinuz-6.12.0-160000.5-default
Found initrd image: /boot/initrd-6.12.0-160000.5-default
Warning: os-prober will be executed to detect other bootable partitions.
Its output will be used to detect bootable binaries on them and create new boot entries.
37.452022 | DM multipath kernel driver not loaded
Adding boot menu entry for UEFI Firmware Settings ...
done

# 再起動して変更を有効にする

dlp:~ #
[5] 動作モードが無効な状態 (disabled) から 有効な状態 (enforcing/permissive) に変更する場合は、リラべリングが必要になります。 これは、無効な状態の時に作成されたファイルは SELinux のラベル付けが行われていないため、それらについても SELinux によるアクセス制御を有効にするためには、後述する SELinux コンテキストと呼ばれる情報でラベル付けをする必要があるためです。 リラべリングは、システム再起動時の起動中に実行されますが、相応の時間がかかるため注意が必要です。
# 以下のように設定して再起動すると次回起動時にリラべリングされる

dlp:~ #
fixfiles -F onboot

System will relabel on next boot
# 上記コマンドで以下のファイルが作成される

dlp:~ #
ll /.autorelabel

-rw-r--r--. 1 root root 3 Oct 22 08:45 /.autorelabel
関連コンテンツ