5.7. SELinux コンテキスト - ファイルのラベル付け
ls -Z コマンドでこれを表示できます。
~]$ ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
unconfined_u)、ロール (object_r)、タイプ (user_home_t)、レベル (s0) を提示しています。この情報は、アクセス制限の決定に使われます。DAC システムでは、アクセスは Linux ユーザー ID とグループ ID に基づいて制御されます。SELinux ポリシールールは、DAC ルールの後でチェックされます。DAC ルールが最初にアクセスを拒否すると、SELinux ポリシールールは使用されません。
chcon、semanage fcontext、restorecon といった複数のコマンドがあります。
5.7.1. 一時的な変更: chcon
chcon コマンドは、ファイルの SELinux コンテキストを変更します。ただし、chcon コマンドによる変更は、ファイルシステムの再ラベル付けや restorecon コマンドが実行されると維持されません。SELinux ポリシーは、特定のファイルの SELinux コンテキストをユーザーが修正できるかどうかを制御します。chcon を使うと、ユーザーは変更する SELinux コンテキストの一部または全部を提供します。SELinux がアクセスを拒否する一般的な原因は、ファイルタイプが間違っているためです。
- ファイルタイプを変更するには、
chcon -t type file-nameコマンドを実行します。ここでの type はhttpd_sys_content_tなどのタイプで、file-name はファイル名またはディレクトリー名になります。 - ディレクトリーのタイプとそのコンテンツを変更するには、
chcon -R -t type directory-nameコマンドを実行します。ここでの type はhttpd_sys_content_tなどのタイプで、directory-name はディレクトリー名になります。
以下の例では、SELinux コンテキストの属性のうち、タイプのみを変更する方法を示しています。
- 引数なしで
cdコマンドを実行して、ホームディレクトリーに移動します。 touch file1コマンドの実行で新規ファイルを作成します。ls -Z file1コマンドでfile1の SELinux コンテキストを表示します。~]$
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 コンテキストの各パーツの説明は、3章SELinux コンテキスト を参照してください。chcon -t samba_share_t file1コマンドを実行して、タイプをsamba_share_tに変更します。-tオプションのみがタイプを変更します。ls -Z file1で変更を表示します。~]$
ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:samba_share_t:s0 file1restorecon -v 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 6 ではデフォルトの SELinux ポリシー) を使用している場合は、restoreconコマンドが/etc/selinux/targeted/contexts/files/ディレクトリー内のファイルを読み取り、どの SELinux コンテキストファイルを持つべきかをチェックします。
file1 をディレクトリーに置き換えます。
以下の例では、新規ディレクトリーの作成と、そのディレクトリーのファイルタイプを (そのコンテンツとともに) 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 ユーザーで
chcon -R -t httpd_sys_content_t /web/コマンドを実行し、/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 - Linux root ユーザーで
restorecon -R -v /web/コマンドを実行し、デフォルトの SELinux コンテキストを復元します。~]#
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 ページを参照してください。
注記

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.