5.10.4. tar を使ったファイルのアーカイビング
tar はデフォルトでは 拡張属性を維持しません。SELinux コンテキストは拡張属性に保存されるので、ファイルをアーカイビングするとコンテキストは失われます。tar --selinux を使ってコンテキストを維持するアーカイブを作成します。tar アーカイブが拡張属性のないファイルを含む場合システムデフォルトに拡張属性を適合させたい場合は、restorecon をアーカイブで実行します。
~]$ tar -xvf archive.tar | restorecon -f -
注記: ディレクトリーによっては、Linux root ユーザーで
restorecon コマンドを実行する必要があることもあります。
以下の例では、SELinux コンテキストを保持する tar アーカイブの作成方法を説明します。
- Linux root ユーザーで
touch /var/www/html/file{1,2,3}コマンドを実行し、3 つのファイルを作成します (file1、file2、file3)。これらのファイルは、/var/www/html/ディレクトリーからのhttpd_sys_content_tタイプを継承します。~]# touch /var/www/html/file{1,2,3} ~]# ls -Z /var/www/html/ -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 cd /var/www/html/コマンドで/var/www/html/ディレクトリーに移動します。その後、Linux root ユーザーでtar --selinux -cf test.tar file{1,2,3}コマンドを実行し、test.tarという名前の tar アーカイブを作成します。- Linux root ユーザーで
mkdir /testコマンドを実行し、新規ディレクトリーを作成します。そしてchmod 777 /test/コマンドを実行し、全ユーザーに/test/ディレクトリーへの完全アクセスを可能にします。 cp /var/www/html/test.tar /test/コマンドを実行し、test.tarファイルを/test/ディレクトリーにコピーします。cd /test/コマンドを実行し、/test/ディレクトリーに切り替え、このディレクトリー内でtar -xvf test.tarコマンドを実行して tar アーカイブを抽出します。ls -lZ /test/コマンドで SELinux コンテキストを表示させます。httpd_sys_content_tタイプが維持されます。--selinuxが使われていなければ、default_tに変更されます。~]$ ls -lZ /test/ -rw-r--r-- user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file1 -rw-r--r-- user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file2 -rw-r--r-- user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file3 -rw-r--r-- user1 group1 unconfined_u:object_r:default_t:s0 test.tar/test/ディレクトリーが不要であれば、Linux root ユーザーでrm -ri /test/コマンドを実行し、ディレクトリーとその中の全ファイルをを削除します。
拡張属性すべてを保持する
--xattrs オプションなどの tar に関する詳細情報は、tar(1) man ページを参照してください。