5.7.2. 永続的な変更: semanage fcontext
semanage fcontext コマンドは、ファイルの SELinux コンテキストを変更します。ターゲットポリシーを使用している場合、このコマンドによる変更が file_contexts にあるファイルになされると /etc/selinux/targeted/contexts/files/file_contexts ファイルに追加され、/web/ ディレクトリー作成時など、新規ファイルやディレクトリーの変更の場合は file_contexts.local に追加されます。ファイルシステムの再ラベリング時に使用される setfiles コマンドと、デフォルトの SELinux コンテキストを復元する restorecon コマンドは、これらのファイルを読み取ります。つまり、semanage fcontext コマンドによる変更は、ファイルシステムが再ラベル付けされても永続的なものとなります。SELinux ポリシーは、ユーザーが特定のファイルの SELinux コンテキストを修正できるかどうかを制御します。
ファイルシステムが再ラベル付けされても SELinux コンテキストの変更が維持されるには、以下の手順を実行します。
semanage fcontext -a options file-name|directory-nameコマンドを実行します。ファイルもしくはディレクトリーへのフルパスを使用します。restorecon -v file-name|directory-nameコマンドを実行し、コンテキストの変更を適用します。
以下の例では、SELinux コンテキストの属性のうち、ファイルのタイプのみを変更しています。
- Linux root ユーザーで
touch /etc/file1コマンドを実行し、新規ファイルを作成します。デフォルトでは、/etc/ディレクトリー内の新規ファイルはetc_tタイプのラベルが付けられます。~]#
ls -Z /etc/file1-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1 - Linux root ユーザーで
semanage fcontext -a -t samba_share_t /etc/file1コマンドを実行し、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/file1semanage fcontext -a -t samba_share_t /etc/file1コマンドが以下のエントリーを/etc/selinux/targeted/contexts/files/file_contexts.localに追加します。/etc/file1 unconfined_u:object_r:samba_share_t:s0
- Linux root ユーザーで
restorecon -v /etc/file1コマンドを実行し、タイプを変更します。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 - Linux root ユーザーで
rm -i /etc/file1コマンドを実行し、file1を削除します。 - Linux root ユーザーで
semanage fcontext -d /etc/file1コマンドを実行し、/etc/file1に追加されたコンテキストを削除します。
以下の例では、新規ディレクトリーの作成と、そのディレクトリーのファイルタイプを Apache HTTP Server が使用するタイプに変更する方法を示します。
- Linux root ユーザーで
mkdir /webコマンドを実行し、新規ディレクトリーを作成します。このディレクトリーにはdefault_tタイプのラベルが付けられます。~]#
ls -dZ /webdrwxr-xr-x root root unconfined_u:object_r:default_t:s0 /webls-dオプションは、lsにコンテンツではなくディレクトリーについての一覧情報を作成させ、-Zオプションはlsに SELinux コンテキスト (この例ではunconfined_u:object_r:default_t:s0) を表示させます。 - Linux root ユーザーで
semanage fcontext -a -t httpd_sys_content_t /webコマンドを実行し、/web/タイプをhttpd_sys_content_tに変更します。-aオプションは新規レコードを追加し、-tオプションはタイプ (httpd_sys_content_t) を定義します。注記: このコマンドの実行は直接にはタイプを変更しません—/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 /websemanage fcontext -a -t httpd_sys_content_t /webコマンドが以下のエントリーを/etc/selinux/targeted/contexts/files/file_contexts.localに追加します。/web unconfined_u:object_r:httpd_sys_content_t:s0
- Linux root ユーザーで
restorecon -v /webコマンドを実行し、タイプを変更します。semanageコマンドは/webのエントリーをfile.contexts.localに追加するので、restoreconコマンドはタイプをhttpd_sys_content_tに変更します。~]#
restorecon -v /webrestorecon reset /web context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0デフォルトでは、新規作成のファイルおよびディレクトリーは、親フォルダの SELinux タイプを引き継ぎます。この例では、/web/に追加された SELinux コンテキストを削除する前は、/web/ディレクトリーに作成されたファイルとディレクトリーはhttpd_sys_content_tタイプのラベル付けがされています。 - Linux root ユーザーで
semanage fcontext -d /webコマンドを実行し、/web/に追加されたコンテキストを削除します。 - Linux root ユーザーで
restorecon -v /webコマンドを実行し、デフォルトの SELinux コンテキストを復元します。
以下の例では、新規ディレクトリーの作成と、そのディレクトリーのファイルタイプを (そのコンテンツとともに) Apache HTTP Server が使用するタイプに変更する方法を示します。この例で使用される設定は、Apache HTTP Server で (/var/www/html/ ではなく) 異なるドキュメントルートを使用する場合に適用します。
- Linux root ユーザーで
mkdir /webコマンドを実行し、新規ディレクトリーを作成します。次にtouch /web/file{1,2,3}コマンドで 3 つの空ファイル (file1、file2、file3) を作成します。/web/ディレクトリーおよびその中のファイルには、default_tタイプのラベルが付けられます。~]#
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 - Linux root ユーザーで
semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"コマンドを実行し、/web/ディレクトリーとその中にあるファイルのタイプをhttpd_sys_content_tに変更します。-aオプションは新規レコードを追加し、-tオプションはタイプ (httpd_sys_content_t) を定義します。"/web(/.*)?"の正規表現は、semanageコマンドが変更を/web/ディレクトリーとその中のファイルに適用させるようにします。注記: このコマンドの実行は直接にはタイプを変更しません—/web/およびその中のファイルはdefault_tタイプのラベル付けがされたままです。~]#
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
- Linux root ユーザーで
restorecon -R -v /webコマンドを実行し、/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 タイプを引き継ぎます。この例では、/web/ディレクトリーに作成されたファイルとディレクトリーはhttpd_sys_content_tタイプのラベルが付けられます。 - Linux root ユーザーで
semanage fcontext -d "/web(/.*)?"コマンドを実行し、"/web(/.*)?"に追加されたコンテキストを削除します。 - Linux root ユーザーで
restorecon -R -v /webコマンドを実行し、デフォルトの SELinux コンテキストを復元します。
以下の例では、SELinux コンテキストを追加・削除する方法を示しています。
- Linux root ユーザーで
semanage fcontext -a -t httpd_sys_content_t /testコマンドを実行します。/test/ディレクトリーはなくても構いません。このコマンドは、以下のコンテキストを/etc/selinux/targeted/contexts/files/file_contexts.localに追加します。/test system_u:object_r:httpd_sys_content_t:s0
- コンテキストを削除するには、Linux root ユーザーで
semanage fcontext -d file-name|directory-nameコマンドを実行します。ここでの file-name|directory-name は、file_contexts.localの最初の部分です。以下はfile_contexts.localのコンテキストの例です。/test system_u:object_r:httpd_sys_content_t:s0
最初の部分は/testになっています。restorecon実行後もしくはファイルシステムが再ラベル付けされた後に/test/ディレクトリーへのhttpd_sys_content_tのラベル付けを防ぐには、Linux root ユーザーでfile_contexts.localからコンテキストを削除します。~]#
semanage fcontext -d /test
/web(/.*)? のようにコンテキストが正規表現の一部である場合、正規表現の前後に引用符を使います。
~]# semanage fcontext -d "/web(/.*)?"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.