Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

5.9.4. Archiving Files with tar


The tar utility does not retain extended attributes by default. Since SELinux contexts are stored in extended attributes, contexts can be lost when archiving files. Use the tar --selinux command to create archives that retain contexts and to restore files from the archives. If a tar archive contains files without extended attributes, or if you want the extended attributes to match the system defaults, use the restorecon utility:
Copy to Clipboard Toggle word wrap
~]$ tar -xvf archive.tar | restorecon -f -
Note that depending on the directory, you may need to be the root user to run the restorecon.
The following example demonstrates creating a tar archive that retains SELinux contexts:

Procedure 5.10. Creating a tar Archive

  1. Change to the /var/www/html/ directory and view its SELinux context:
    Copy to Clipboard Toggle word wrap
    ~]$ cd /var/www/html/
    Copy to Clipboard Toggle word wrap
    html]$ ls -dZ /var/www/html/
    drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 .
  2. As root, create three files (file1, file2, and file3) in /var/www/html/. These files inherit the httpd_sys_content_t type from /var/www/html/:
    Copy to Clipboard Toggle word wrap
    html]# touch file{1,2,3}
    Copy to Clipboard Toggle word wrap
    html]$ 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
    
  3. As root, run the following command to create a tar archive named test.tar. Use the --selinux to retain the SELinux context:
    Copy to Clipboard Toggle word wrap
    html]# tar --selinux -cf test.tar file{1,2,3}
  4. As root, create a new directory named /test/, and then allow all users full access to it:
    Copy to Clipboard Toggle word wrap
    ~]# mkdir /test
    Copy to Clipboard Toggle word wrap
    ~]# chmod 777 /test/
  5. Copy the test.tar file into /test/:
    Copy to Clipboard Toggle word wrap
    ~]$ cp /var/www/html/test.tar /test/
  6. Change into /test/ directory. Once in this directory, run the following command to extract the tar archive. Specify the --selinux option again otherwise the SELinux context will be changed to default_t:
    Copy to Clipboard Toggle word wrap
    ~]$ cd /test/
    Copy to Clipboard Toggle word wrap
    test]$ tar --selinux -xvf test.tar
  7. View the SELinux contexts. The httpd_sys_content_t type has been retained, rather than being changed to default_t, which would have happened had the --selinux not been used:
    Copy to Clipboard Toggle word wrap
    test]$ 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
    
  8. If the /test/ directory is no longer required, as root, run the following command to remove it, as well as all files in it:
    Copy to Clipboard Toggle word wrap
    ~]# rm -ri /test/
See the tar(1) manual page for further information about tar, such as the --xattrs option that retains all extended attributes.
Nach oben
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2025 Red Hat, Inc.