Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux tar Command Permissions: Preserve or Change Them

· 5 min read · Home Tech

In this article
  1. What permissions does tar store?
  2. How creation and extraction work
  3. Choose options for the job
  4. Permissions, ownership, and root
  5. Common permission problems
  6. Frequently Asked Questions

The Linux tar command can store file permissions in an archive and, when extracting, can preserve them—but the result depends on the options used, your privileges, and the tar implementation. By default, GNU tar normally restores permission bits for the current user while limiting them according to the process umask; use --same-permissions (or -p) when you specifically need to restore archived modes without that umask adjustment.

What permissions does tar store?

A tar archive records metadata for each member, including its permission mode: the read, write, and execute bits for the owner, group, and other users. It also records ownership identifiers and timestamps, though whether those details are restored is a separate question. The archive is not a live view of the original files; its metadata reflects the files when they were added.

For example, a file with mode 0644 is generally readable and writable by its owner and readable by others. A directory with mode 0755 is traversable by users who can access it. Executable scripts and programs also rely on their execute bit, so extracting them without it can affect whether they run directly.

Permission modes do not include every kind of security metadata. Access-control lists (ACLs), extended attributes, and security labels may require additional options and support from the tar version and filesystem. Do not assume that an archive made with ordinary options is a complete backup of all access controls.

How creation and extraction work

When creating an archive, tar reads the source files and writes their modes as archive metadata. A typical command is:

tar -cf project.tar project/

GNU tar normally saves the permission information it reads. On extraction, it creates files and applies modes subject to its rules, the extracting user's authority, and the umask. The umask removes permission bits from newly created files; for instance, a restrictive umask can make an extracted file less accessible than the mode recorded in the archive.

To extract while asking GNU tar to use the archived permissions as-is, use:

tar -xpf project.tar

Here -x extracts, -p requests preservation of permissions, and -f names the archive. The long form is --same-permissions. In GNU tar, this option overrides the umask for the archived permission bits. It does not grant a regular user the ability to change ownership or bypass filesystem restrictions.

Choose options for the job

Whether to preserve permissions depends on what the archive is for. Use ordinary extraction for many personal file transfers, where the current account's umask should continue to protect newly created files. Preserve modes when restoring a backup or deploying files whose executable and access bits are part of their intended setup.

  • Everyday extraction: tar -xf archive.tar. GNU tar applies its normal extraction rules, including the umask.
  • Restore archived modes: tar -xpf archive.tar. Useful when the modes in the archive should be retained.
  • Create an archive with a chosen root: tar -cpf backup.tar /path is not a reliable way to control later ownership by itself; creation and extraction ownership are distinct. Choose the extraction options and privileges deliberately.
  • Inspect before extracting: tar -tvf archive.tar lists members and their recorded modes, helping you spot unexpected executable or broadly writable files.

Options can vary across implementations. GNU tar is common on Linux, but systems may provide BusyBox tar or another implementation with different option support and defaults. Check tar --help or the local manual page if an option is rejected or behavior differs.

Permissions, ownership, and root

Mode preservation and ownership preservation are not the same thing. A regular user can generally set permission bits on files they own, but cannot make those files belong to arbitrary users or groups. GNU tar's --same-owner requests restoration of recorded ownership; it normally has effect only when extracting as a privileged user. Without that authority, extracted files usually belong to the extracting user.

For a system backup that must restore both modes and owners, an administrator may use:

sudo tar --same-owner --same-permissions -xpf backup.tar -C /restore

Use this only when the archive and destination are trusted. Restoring as root can overwrite files and create entries with powerful ownership or permission settings. Review the archive and extraction destination first, and avoid extracting an untrusted archive into a sensitive directory.

Some modes are special: set-user-ID, set-group-ID, and sticky bits have security implications. The kernel or filesystem may clear certain bits in particular situations, and privileged extraction may restore bits that ordinary extraction cannot. Ownership, filesystem mount options, and platform rules can also affect the final result.

Common permission problems

If an extracted script says “Permission denied,” inspect its mode with ls -l. The archive may not have recorded an execute bit, or extraction may have changed the mode. If appropriate, and only for a trusted script, set the intended mode explicitly—for example, chmod 755 script.sh. Do not make every extracted file executable as a blanket fix.

If permissions seem too restrictive, check the umask and whether extraction used the preservation option. If they seem too permissive, check the archive's recorded modes and the command used to extract it. For sensitive files, it may be safer to set a known restrictive mode after extraction rather than blindly preserving metadata from an unknown source.

Also check directory permissions: execute permission on a directory means users can traverse it, while read permission allows listing its names. A file can have a readable mode and still be inaccessible if a parent directory blocks traversal. Finally, ACLs can grant or deny access beyond what a simple ls -l mode suggests; use tools such as getfacl where available.

Frequently Asked Questions

Does tar preserve permissions by default?
GNU tar stores permission modes in archives. On extraction it normally applies them subject to the umask and extraction rules; use -p or --same-permissions when you need the archived modes preserved without umask adjustment.

What does tar -p do?
For GNU tar extraction, -p is shorthand for --same-permissions. It asks tar to restore the permission bits recorded in the archive rather than restricting them according to the umask.

Does tar preserve file ownership?
Tar records ownership information, but restoring it requires suitable privileges and the appropriate option. A regular user generally receives files owned by that user; GNU tar can restore recorded owners with --same-owner when run with sufficient authority.

How can I see permissions inside a tar archive?
Run tar -tvf archive.tar. The verbose listing displays the archived members and their modes, though it does not guarantee those exact modes will be applied on every system.

Does tar preserve ACLs and extended attributes?
Not necessarily with basic commands. GNU tar provides options such as --acls and --xattrs, but both the creating and extracting environment must support the relevant metadata. Consult the local tar manual before relying on them for a complete backup.