Show Table of Contents
13.4. 設定例
以下では、SELinux がどのように Apache HTTP Server を補完するのか、Apache HTTP Server の全機能をどのように維持するのかを実践的な例を用いて示します。
13.4.1. 静的なサイトを稼働させる
静的な web サイトを作成する場合は、その web サイトの
.html ファイルに httpd_sys_content_t タイプのラベルを付けます。デフォルトでは、Apache HTTP Server は httpd_sys_content_t タイプのラベルが付いたファイルに書き込みはできません。以下の 例では、読み取り専用 web サイト向けのファイルを保存する新規ディレクトリーを作成します。
- root で
mkdirユーティリティーを使用して最上位のディレクトリーを作成します。~]#
mkdir /mywebsite - root で
/mywebsite/index.htmlファイルを作成します。以下のコンテンツを/mywebsite/index.htmlにコピーして貼り付けます。<html> <h2>index.html from /mywebsite/</h2> </html>
/mywebsite/およびその配下のファイルやサブディレクトリーへの読み取り専用アクセスを Apache HTTP Server に許可するために、このディレクトリー にhttpd_sys_content_tタイプのラベルを付けます。root で以下のコマンドを実行してラベルの変更をファイルコンテキスト設定に追加します。~]#
semanage fcontext -a -t httpd_sys_content_t "/mywebsite(/.*)?"- root で
restoreconを使用してラベル変更を適用します。~]#
restorecon -R -v /mywebsiterestorecon reset /mywebsite context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /mywebsite/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 - この例の場合、root で
/etc/httpd/conf/httpd.confファイルを編集します。既存のDocumentRootオプションをコメントアウトし、DocumentRoot "/mywebsite"オプションを追加します。編集後は以下のようになります。#DocumentRoot "/var/www/html" DocumentRoot "/mywebsite"
- root で以下のコマンドを実行して Apache HTTP Server の状態を確認します。サーバーが停止している場合は起動します。
~]#
systemctl status httpd.servicehttpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead)~]#
systemctl start httpd.serviceサーバーが稼働している場合は、root で以下のコマンドを実行してサービスを再起動します (httpd.confへの変更にもこれを適用)。~]#
systemctl status httpd.servicehttpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: active (running) since Wed 2014-02-05 13:16:46 CET; 2s ago~]#
systemctl restart httpd.service - web ブラウザで
http://localhost/index.htmlに移動します。以下のように表示されます。index.html from /mywebsite/
13.4.2. NFS および CIFS ボリュームの共有
クライアント側の NFS マウントは、デフォルトで NFS ボリュームのポリシーで定義されたデフォルトのコンテキストでラベル付けされます。共通ポリシーでは、このデフォルトのコンテキストは、
nfs_t タイプを使用します。またデフォルトでは、クライアント側にマウントされた Samba 共有は、ポリシーが定義したデフォルトのコンテキストでラベル付けされます。共通ポリシーでは、このデフォルトのコンテキストは cifs_t タイプを使用します。
ポリシー設定によっては、サービスが
nfs_t または cifs_t タイプのラベルが付けられたファイルを読み取れない場合もあります。これにより、これらのタイプのラベルが付けられたファイルシステムがマウントされ、他のサービスが読み取ったり、エクスポートすることを防ぐことができます。ブール値をオンやオフに切り替えて、nfs_t や cifs_t タイプにアクセス可能なサービスを制御することができます。
(
nfs_t タイプのラベルが付けられている) NFS ボリュームへのアクセスと共有を httpd に許可する場合は、httpd_use_nfs ブール値を有効にします。
~]# setsebool -P httpd_use_nfs on
(
cifs_t タイプのラベルが付けられている) CIFS ボリュームへのアクセスと共有を httpd に許可する場合は、httpd_use_cifs ブール値を有効にします。
~]# setsebool -P httpd_use_cifs on注記
再起動後に
setsebool による変更を維持したくない場合は、-P オプションを使用しないでください。
13.4.3. サービス間でのファイル共有
Type Enforcement を使用すると、プロセスが別のプロセス用のファイルにアクセスしてしまうのを防ぐのに役立ちます。たとえば、デフォルトでは Samba は
httpd_sys_content_t タイプのラベルが付いたファイルを読み込みことはできません。このタイプは Apache HTTP Server での使用を目的としています。目的のファイルに public_content_t または public_content_rw_t タイプのラベルを付けると、Apache HTTP Server、FTP、rsync、Samba 間でファイルを共有することができるようになります。
以下の例では、ディレクトリーとファイルを作成し、Apache HTTP Server、FTP、rsync、Samba でそのディレクトリーとファイルを共有 (読み取り専用) できるようにします。
- root で
mkdirを使用して、複数サービス間でファイルを共有するための最上位の新規ディレクトリーを作成します。~]#
mkdir /shares - ファイルコンテキスト設定のパターンに合致しないファイルやディレクトリーには、
default_tタイプのラベルが付いている場合があります。制限のあるサービスは、このタイプのファイルやディレクトリーにはアクセスできません。~]$
ls -dZ /sharesdrwxr-xr-x root root unconfined_u:object_r:default_t:s0 /shares - root で
/shares/index.htmlファイルを作成します。以下のコンテンツをコピーして/shares/index.htmlに貼り付けます。<html> <body> <p>Hello</p> </body> </html>
/shares/にpublic_content_tタイプのラベルを付けることで、Apache HTTP Server、FTP、rsync、Samba による読み取り専用アクセスを許可します。root で以下のコマンドを実行し、ラベルの変更をファイルコンテキスト設定に追加します。~]#
semanage fcontext -a -t public_content_t "/shares(/.*)?"- root で
restoreconユーティリティーを使用してラベル変更を適用します。~]#
restorecon -R -v /shares/restorecon reset /shares context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 restorecon reset /shares/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
Samba で
/shares/ を共有する場合は、以下の手順にしたがいます。
- samba、samba-common、samba-client の各パッケージがインストールされていることを確認します (バージョン番号は使用しているバージョンによって異なります)。
~]$
rpm -q samba samba-common samba-clientsamba-3.4.0-0.41.el6.3.i686 samba-common-3.4.0-0.41.el6.3.i686 samba-client-3.4.0-0.41.el6.3.i686上記のパッケージがインストールされていない場合は、root で以下のコマンドを実行して、これらをインストールします。~]#
yum install package-name - root で
/etc/samba/smb.confファイルを編集します。Samba で/shares/ディレクトリーを共有するために、以下のエントリーをこのファイルの末尾に追加します。[shares] comment = Documents for Apache HTTP Server, FTP, rsync, and Samba path = /shares public = yes writable = no
- Samba ファイルシステムのマウントには Samba アカウントが必要になります。root で以下のコマンドを実行し、Samba アカウントを作成します。username は既存の Linux ユーザーにします。たとえば、
smbpasswd -a testuserを実行すると、Linux のtestuserユーザー用の Samba アカウントが作成されます。~]#
smbpasswd -a testuserNew SMB password: Enter a password Retype new SMB password: Enter the same password again Added user testuser.上記のコマンドを実行する際に、システムに存在しないアカウントのユーザー名を指定すると、Cannot locate Unix account for 'username'!エラーが発生します。 - Samba サービスを開始します。
~]#
systemctl start smb.service - 以下のコマンドを実行し、利用可能な共有を表示します。username はステップ 3 で追加した Samba アカウントにします。パスワードの入力を求められたら、ステップ 3 で Samba アカウントに割り当てたパスワードを入力します (バージョン番号は使用しているバージョンによって異なります)。
~]$
smbclient -U username -L localhostEnter username's password: Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6] Sharename Type Comment --------- ---- ------- shares Disk Documents for Apache HTTP Server, FTP, rsync, and Samba IPC$ IPC IPC Service (Samba Server Version 3.4.0-0.41.el6) username Disk Home Directories Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6] Server Comment --------- ------- Workgroup Master --------- ------- mkdirユーティリティーを使って新規ディレクトリーを作成します。このディレクトリーは Samba 共有のsharesをマウントする際に使用します。~]#
mkdir /test/- root で以下のコマンドを実行して、Samba 共有の
sharesを/test/にマウントします。username はステップ 3 のユーザー名にしてください。~]#
mount //localhost/shares /test/ -o user=usernameステップ 3 で設定した username のパスワードを入力します。 - Samba で共有されているファイルのコンテンツを表示します。
~]$
cat /test/index.html<html> <body> <p>Hello</p> </body> </html>
Apache HTTP Server で
/shares/ を共有する場合は、以下の手順にしたがいます。
- httpd パッケージがインストールされていることを確認します (バージョン番号は使用しているバージョンによって異なります)。
~]$
rpm -q httpdhttpd-2.2.11-6.i386このパッケージがインストールされていない場合は、root でyumユーティリティーを使用してインストールします。~]#
yum install httpd /var/www/html/ディレクトリーに移動します。root で以下のコマンドを実行して/shares/ディレクトリーへのリンク (sharesという名前にします) を作成します。html]#
ln -s /shares/ shares- Apache HTTP Server を起動します。
~]#
systemctl start httpd.service - web ブラウザを使って
http://localhost/sharesに移動します。/shares/index.htmlが表示されます。
デフォルトでは、
index.html ファイルが存在していれば、Apache HTTP Server はこれを読み込みます。/shares/ に file1、file2、file3 しかなく index.html がない場合、http://localhost/shares にアクセスするとディレクトリー一覧が表示されます。
index.htmlファイルを削除します。~]#
rm -i /shares/index.html- root で
touchユーティリティーを使用して/shares/に新規ファイルを 3 つ作成します。~]#
touch /shares/file{1,2,3}~]#ls -Z /shares/-rw-r--r-- root root system_u:object_r:public_content_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:public_content_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:public_content_t:s0 file3 - root で以下のコマンドを実行して Apache HTTP Server の状態を確認します。
~]#
systemctl status httpd.servicehttpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead)サーバーが停止している場合は、これを起動します。~]#
systemctl start httpd.service - web ブラウザで
http://localhost/sharesに移動します。ディレクトリー一覧が表示されます。
13.4.4. ポート番号を変更する
ポリシー設定によっては、サービスが特定のポート番号でのみ実行できるようにすることが可能です。ポリシーを変更せずサービスが実行されるポートを変えようとすると、サービスの起動に失敗する場合があります。root ユーザーで
semanage ユーティリティーを使用して、SELinux が httpd にリッスンを許可しているポートを表示します。
~]# semanage port -l | grep -w http_port_t
http_port_t tcp 80, 443, 488, 8008, 8009, 8443
デフォルトでは、SELinux で
httpd にリッスンを許可している TCP ポートは 80、443、488、8008、8009、8443 になります。httpd で http_port_t 用に記載されていないポートをリッスンするよう /etc/httpd/conf/httpd.conf を設定すると、httpd の起動に失敗します。
httpd が TCP ポート 80、443、488、8008、8009、8443 以外のポートで実行するようにするには、以下の手順で設定します。
- root で
/etc/httpd/conf/httpd.confファイルを編集し、SELinux ポリシーではhttpd用に設定されていないポートをListenオプションに記載します。以下の例では、httpdが IP アドレス 10.0.0.1、TCP ポート 12345 でリッスンするよう設定します。# Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 Listen 10.0.0.1:12345
- root で以下のコマンドを実行し、SELinux ポリシーの設定にこのポートを追加します。
~]#
semanage port -a -t http_port_t -p tcp 12345 - ポートが追加されたことを確認します。
~]#
semanage port -l | grep -w http_port_thttp_port_t tcp 12345, 80, 443, 488, 8008, 8009, 8443
ポート 12345 で
httpd を実行しないようになったら、root ユーザーで semanage ユーティリティーを実行してポリシー設定からそのポートを削除します。
~]# semanage port -d -t http_port_t -p tcp 12345
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.