Show Table of Contents
4.6. SELinux コンテキスト: ファイルのラベル付け
SELinux 実行中のシステム上では、すべてのプロセスとファイルにセキュリティー関連の情報を表示するラベルが付けられます。この情報は、SELinux コンテキストと呼ばれます。ファイルに関しては、
ls -Z コマンドでこれを表示できます。
~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
この例では、SELinux は ユーザー (
unconfined_u)、ロール (object_r)、タイプ (user_home_t)、およびレベル (s0) を示しています。この情報は、アクセス制限の決定に使用されます。DAC システムでは、アクセスは Linux ユーザー ID とグループ ID に基づいて制御されます。SELinux ポリシールールは、DAC ルールの後でチェックされます。DAC ルールが最初にアクセスを拒否すると、SELinux ポリシールールは使用されません。
注記
デフォルトでは、新規作成のファイルおよびディレクトリーは、親ディレクトリーの SELinux タイプを引き継ぎます。たとえば、
etc_t タイプのラベルが付けられた /etc ディレクトリー内に新規ファイルを作成すると、このファイルは同じタイプを継承します。
~]$ ls -dZ - /etc
drwxr-xr-x. root root system_u:object_r:etc_t:s0 /etc~]# touch /etc/file1
~]# ls -lZ /etc/file1
-rw-r--r--. root root unconfined_u:object_r:etc_t:s0 /etc/file1
ファイルの SELinux コンテキストを管理するには、
chcon、semanage fcontext、restorecon といった複数のコマンドがあります。
4.6.1. 一時的な変更: chcon
chcon コマンドは、ファイルの SELinux コンテキストを変更します。ただし、chcon コマンドによる変更は、ファイルシステムの再ラベル付けや restorecon コマンドが実行されると維持されません。SELinux ポリシーは、特定のファイルの SELinux コンテキストをユーザーが修正できるかどうかを制御します。chcon を使うと、ユーザーは変更する SELinux コンテキストの一部または全部を提供します。SELinux がアクセスを拒否する一般的な原因は、ファイルタイプが間違っているためです。
クイックリファレンス
- ファイルタイプを変更するには、
chcon -t type file-nameコマンドを実行します。ここでの type はhttpd_sys_content_tなどの SELinux タイプで、file-name はファイル名またはディレクトリー名になります。~]$chcon -t httpd_sys_content_t file-name - ディレクトリーのタイプとそのコンテンツを変更するには、
chcon -R -t type directory-nameコマンドを実行します。ここでの type はhttpd_sys_content_tなどの SELinux タイプで、directory-name はディレクトリー名になります。~]$chcon -R -t httpd_sys_content_t directory-name
手順4.6 ファイルまたはディレクトリーのタイプ変更
以下では SELinux コンテキストのタイプを変更し、他の属性はそのままにしておく手順を説明します。このセクションの例は、ディレクトリーにも適用できます。例えば、
file1 をディレクトリーに置き換えます。
- ホームディレクトリーへ移動します。
- 新規ファイルを作成し、その SELinux コンテキストを表示します。
~]$touch file1~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1この例では、file1の SELinux コンテキストには、SELinuxunconfined_uユーザー、object_rロール、user_home_tタイプ、s0レベルが含まれます。SELinux コンテキストの各パーツの説明は、「2章SELinux コンテキスト」を参照してください。 - 以下のコマンドを実行して、タイプを
samba_share_tに変更します。-tオプションはタイプのみを変更します。そして、変更を確認します。~]$chcon -t samba_share_t file1~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:samba_share_t:s0 file1 file1ファイルの SELinux コンテキストを復元するには、以下のコマンドを実行します。変更内容を表示するには、-vオプションを使用します。~]$restorecon -v file1restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0この例では、以前のタイプであるsamba_share_tが、正しいuser_home_tに復元されました。ターゲットポリシー (Red Hat Enterprise Linux ではデフォルトの SELinux ポリシー) を使用している場合は、restoreconコマンドが/etc/selinux/targeted/contexts/files/ディレクトリー内のファイルを読み取り、どの SELinux コンテキストファイルにするかをチェックします。
手順4.7 ディレクトリーおよびコンテンツタイプの変更
以下の例では、新規ディレクトリーの作成と、そのディレクトリーのファイルタイプをそのコンテンツとともに Apache HTTP Server が使用するタイプに変更する方法を示します。この例で使用される設定は、Apache HTTP Server で (
/var/www/html/ ではなく) 異なるドキュメントルートを使用する場合に適用します。
- root ユーザーとして新規ディレクトリー
web/を作成し、この中に 3 つの空のファイル (file1、file2、file3) を作成します。web/ディレクトリーとその中のファイルは、default_tタイプのラベルが付けられます。~]#mkdir /web~]#touch /web/file{1,2,3}~]#ls -dZ /webdrwxr-xr-x root root unconfined_u:object_r:default_t:s0 /web~]#ls -lZ /web-rw-r--r-- root root unconfined_u:object_r:default_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file3 - root で以下のコマンドを実行し、
web/ディレクトリー (およびそのコンテンツ) のタイプをhttpd_sys_content_tに変更します。~]#chcon -R -t httpd_sys_content_t /web/~]#ls -dZ /web/drwxr-xr-x root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/~]#ls -lZ /web/-rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 file3 - デフォルトの SELinux コンテキストを復元するには、root で
restoreconユーティリティーを使用します。~]#restorecon -R -v /web/restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file3 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
chcon についての詳細は、chcon(1) の man ページを参照してください。
注記
Type Enforcement は、SELinux ターゲットポリシーで使われる主要なパーミッション制御です。ほとんどの場合、SELinux ユーザーとロールは無視することができます。
4.6.2. 永続的な変更: semanage fcontext
semanage fcontext コマンドは、ファイルの SELinux コンテキスト変更に使用します。新規作成ファイルおよびディレクトリーのコンテキストを表示するには、root で以下のコマンドを実行します。
~]# semanage fcontext -C -l
これらのファイルは、2 つのユーティリティーが読み込みます。ファイルシステムのラベル変更には
setfiles ユーティリティーを使用し、デフォルトの SELinux コンテキストを復元するには restorecon ユーティリティーを使用します。つまり、ファイルシステムのラベル変更が行われても、semanage fcontext による変更は維持されます。SELinux ポリシーは、ユーザーが特定ファイルの SELinux コンテキストを修正できるかどうかを制御します。
クイックリファレンス
ファイルシステムのラベル変更が行われても SELinux コンテキストの変更が維持されるようにするには、以下の手順を実行します。
- 以下のコマンドを実行します。ファイルまたはディレクトリーの完全パスを使用します。
~]#semanage fcontext -a options file-name|directory-name restoreconユーティリティーを使用してコンテキスト変更を適用します。~]#restorecon -v file-name|directory-name
手順4.8 ファイルまたはディレクトリーのタイプ変更
以下ではファイルのタイプを変更し、SELinux コンテキストの他の属性はそのままにしておく例を示しています。このセクションの例は、ディレクトリーにも適用できます。例えば、
file1 をディレクトリーに置き換えます。
- root ユーザーとして、
/etcディレクトリー内に新規ファイルを作成します。デフォルトでは、/etcディレクトリー内の新規作成ファイルにはetc_tタイプのラベルが付けられます。~]#touch /etc/file1~]$ls -Z /etc/file1-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1ディレクトリーの情報を確認するには、以下のコマンドを実行します。~]$ls -dZ directory_name - root で以下のコマンドを実行し、
file1のタイプをsamba_share_tに変更します。-aオプションは新規レコードを追加し、-tオプションはタイプ (samba_share_t) を定義します。このコマンドを実行しても、直ちにタイプが変更されるわけではないことに留意してください。file1にはetc_tタイプのラベルが付けられたままです。~]#semanage fcontext -a -t samba_share_t /etc/file1~]#ls -Z /etc/file1-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1~]$
semanage fcontext -C -l/etc/file1 unconfined_u:object_r:samba_share_t:s0 - root で
restoreconユーティリティーを使用してタイプを変更します。semanageが/etc/file1のエントリーをfile_contexts.localに追加したので、restoreconによりタイプがsamba_share_tに変更されます。~]#restorecon -v /etc/file1restorecon reset /etc/file1 context unconfined_u:object_r:etc_t:s0->system_u:object_r:samba_share_t:s0
手順4.9 ディレクトリーおよびコンテンツタイプの変更
以下の例では、新規ディレクトリーの作成と、そのディレクトリーのファイルタイプをそのコンテンツとともに Apache HTTP Server が使用するタイプに変更する方法を示します。この例で使用される設定は、Apache HTTP Server で、
/var/www/html/ ではなく、異なるドキュメントルートを使用する場合に適用します。
- root ユーザーとして新規ディレクトリー
web/を作成し、この中に 3 つの空のファイル (file1、file2、file3) を作成します。web/ディレクトリーとその中のファイルは、default_tタイプのラベルが付けられます。~]#mkdir /web~]#touch /web/file{1,2,3}~]#ls -dZ /webdrwxr-xr-x root root unconfined_u:object_r:default_t:s0 /web~]#ls -lZ /web-rw-r--r-- root root unconfined_u:object_r:default_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file3 - root で以下のコマンドを実行し、
web/ディレクトリーとその中にあるファイルのタイプをhttpd_sys_content_tに変更します。-aオプションは新規レコードを追加し、-tオプションはタイプ (httpd_sys_content_t) を定義します。"/web(/.*)?"の正規表現を使うことで、semanageが変更をweb/とその中のファイルに適用します。このコマンドを実行しても、直接にはタイプを変更しないことに留意してください。web/およびその中のファイルはdefault_tタイプのラベルが付けられたままです。~]#semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"~]$ls -dZ /webdrwxr-xr-x root root unconfined_u:object_r:default_t:s0 /web~]$ls -lZ /web-rw-r--r-- root root unconfined_u:object_r:default_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file3semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"コマンドが以下のエントリーを/etc/selinux/targeted/contexts/files/file_contexts.localに追加します。/web(/.*)? system_u:object_r:httpd_sys_content_t:s0
- root で
restoreconユーティリティーを使用してweb/とその中のすべてのファイルのタイプを変更します。-Rオプションは再帰的なので、web/ディレクトリー下のすべてのファイルとディレクトリーがhttpd_sys_content_tタイプでラベル付けされます。semanageで/web(/.*)?のエントリーをfile.contexts.localに追加したので、restoreconによりhttpd_sys_content_tにタイプが変更されます。~]#restorecon -R -v /webrestorecon reset /web context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /web/file2 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /web/file3 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /web/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0デフォルトでは、新規作成のファイルおよびディレクトリーは、親ディレクトリーの SELinux タイプを引き継ぎます。
手順4.10 追加されたコンテキストの削除
以下では、SELinux コンテキストの追加と削除の例を示しています。
/web(/.*)? のようにコンテキストが正規表現の一部である場合、正規表現の前後に引用符を使います。
~]#semanage fcontext -d "/web(/.*)?"
- コンテキストを削除するには、root ユーザーで以下のコマンドを実行します。ここでの file-name|directory-name は、
file_contexts.localの最初の部分です。~]#semanage fcontext -d file-name|directory-name以下は、file_contexts.local内のコンテキスト例です。/test system_u:object_r:httpd_sys_content_t:s0
最初の部分は/testになっています。restorecon実行後もしくはファイルシステムのラベル交換後に/test/ディレクトリーへのhttpd_sys_content_tのラベル付けを防ぐには、root で以下のコマンドを実行してfile_contexts.localからコンテキストを削除します。~]#semanage fcontext -d /test - root で
restoreconユーティリティーを使用してデフォルトの SELinux コンテキストを復元します。
semanage についての詳細は、semanage(8) の man ページを参照してください。
重要
semanage fcontext -a で SELinux のコンテキストを変更する場合、ファイルシステムの再ラベル付け後もしくは restorecon コマンド実行後におけるファイルの誤ったラベル付けを避けるために、ファイルもしくはディレクトリーへの完全パスを使用してください。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.