LinuxでSELinuxコンテキストを保持しながらファイルをコピーする
SElinuxは、まともなLinuxシステムの重要な一部となっています。さまざまなサービスの構成中に、ファイルSELinuxコンテキストが重要な役割を果たします。後で使用するために事前定義されたSELinuxコンテキストでファイルのコピーまたはバックアップを作成する必要がある場合や、現在の構成を模倣しようとしている場合があります。SELinuxコンテキストを保持しながらファイルのコピーを作成するには、オプション付きの
たとえば、
cp
コマンドを使用し--preserve=context
ます。たとえば、
/etc/services
fileのSELinuxファイルコンテキストを表示してみましょう。[[email protected] ]# ls -Z /etc/services -rw-r--r--. root root system_u:object_r:etc_t:s0 /etc/servicesコピー手順中に、
cp
デフォルトでコマンドが新しいSELinuxファイルコンテキストを作成します。[[email protected] ]# cp /etc/services /tmp/ [[email protected] ]# ls -Z /tmp/services -rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/servicesを使用
preserve=context
すると、cp
SELinuxコンテキストを保持するようにコマンドが指示されます。[[email protected] ]# cp --preserve=context /etc/services /tmp/ cp: overwrite ‘/tmp/services’? y [[email protected] ]# ls -Z /tmp/services -rw-r--r--. root root system_u:object_r:etc_t:s0 /tmp/services上記のSELinuxのコンテキスト保存手順は、ディレクトリにも適用されます。
[[email protected] ]# ls -Zd /etc/ drwxr-xr-x. root root system_u:object_r:etc_t:s0 /etc/ [[email protected] ]# cp -r /etc/ /tmp/ [[email protected] ]# ls -Zd /tmp/etc/ drwxr-xr-x. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/etc/ [[email protected] ]# rm -fr /tmp/etc/ [[email protected] ]# cp -r --preserve=context /etc/ /tmp/ [[email protected] ]# ls -Zd /tmp/etc/ drwxr-xr-x. root root system_u:object_r:etc_t:s0 /tmp/etc/