1. Overview

TAR is a format that can combine multiple files and directories into one file, preserving their original attributes. Specifically, TAR stands for tape archive, as it was initially designed to store data on magnetic tapes.

One of the advantages of using tar archives is that we can compress and transfer them across different systems easily.

However, one of the challenges of using tar archives is that they may contain files with separate ownership and group information, which may not match the desired or existing users and groups on target systems.

In this tutorial, we’ll learn how to modify file ownership inside a tar archive, using various tools and methods.

2. Using tar Options

Perhaps the simplest way we can modify file ownership inside a tar archive is by using the built-in options of the tar command. We can create, extract, list, and manipulate tar archives with the tar command.

tar has several options that can control the ownership and group information of the files in the archive:

  • –owner=NAME: sets the owner of the files in the archive to NAME, which can be either a user name or a numeric user ID
  • –group=NAME: sets the group of the files in the archive to NAME, which can be either a group name or a numeric group ID
  • –no-same-owner: employs the current user as the owner when we’re extracting files from the archive
  • –same-owner: preserves the original ownership of the files inside the archive while extracting

We can use these options when creating or extracting TAR archives, depending on our desired outcome.

2.1. Set File Ownership When Creating Archive

For example, let’s create a tar archive named files.tar from a directory called files, and set the owner and group of all files in the archive to root:

$ tar --owner=root --group=root -cf files.tar files

We use the -c option to create a new archive. Also, the -f option specifies the name of the file to be created which is files.tar.

2.2. Set File Ownership When Extracting Archive

Similarly, let’s extract the tar archive files.tar to the directory files. We’ll ignore the original ownership of the files in the archive, but instead use the current user as the owner:

$ tar --no-same-owner -xf files.tar -C files

Notably, these options may require superuser privileges such as using sudo to execute successfully, depending on the system configuration and permissions.

Additionally, this method is usually very simple and straightforward and doesn’t require any additional tools or dependencies apart from the tar utility itself. Also, it works with any type of tar archive, regardless of compression format.

3. Using fakeroot

Another way we can modify file ownership inside a TAR archive is to use a tool called fakeroot. The fakeroot utility runs another program in an environment where there is root privilege for file manipulation.

We can use fakeroot to create tar archives with arbitrary ownership information, without affecting the ownership of the files. The fakeroot utility intercepts system calls such as chown and chmod, and pretends that they succeed. However, only the program we run under fakeroot believes that the calls succeeded, but the filesystem stays the same.

Moreover, we can also combine the fakeroot utility with the tar command to create tar archives with custom ownership information.

3.1. Set File Ownership When Creating Archive

For example, let’s create a tar archive named files.tar from a directory called files. We’ll ignore the original ownership of the files and the current user will own the file we create:

$ fakeroot sh -c 'chown root:root files/*; tar cf files.tar files'

The code above performs several steps:

  1. run an sh shell under fakeroot and execute the next steps
  2. change the ownership of all the files in the files directory to root
  3. create a tar archive named files.tar from the files directory

Yet, the actual ownership of the files on the filesystem remains unchanged.

3.2. Set File Ownership When Extracting Archive

Similarly, let’s extract the archive we created above to the files directory and preserve the original ownership of the files:

$ tar --same-owner -xf files.tar -C files

Notably, this command may again require superuser privileges.

In addition, the fakeroot method is more flexible than using tar options, as it can change the ownership of any file or directory in the archive, regardless of the current user or group. Also, it doesn’t affect the ownership of the files on the filesystem, which may be desirable for some applications.

4. Using archivemount

Another way we can modify file ownership inside a tar archive is to use a tool called archivemount. The archivemount utility is a program that can mount an archive file as a directory, using FUSE (Filesystem in Userspace).

The archivemount utility can mount various archive files, including tar archives, as read-write filesystems. This means that we can access and modify the files and directories inside the archive as if they were regular files and directories on the filesystem.

We can combine archivemount utility with chown command to change the ownership of files and directories inside the archive.

4.1. Set File Ownership When Creating Archive

For example, let’s mount a tar archive named files.tar to a directory named mnt, and change the owner and group of the files in the archive to root:

$ archivemount files.tar mnt
$ chown -R root:root mnt/*
$ fusermount -u mnt

In this case, we perform three actions:

  1. mount the tar archive named files.tar to a directory named mnt, using FUSE
  2. change the ownership of all the files and directories in mnt to root, recursively
  3. unmount the directory named mnt, thus updating the tar archive named files.tar with the changes

At this point, we should have root as the owner of the archive contents.

4.2. Set File Ownership When Extracting Archive

We created a tar archive named files.tar with archivemount. Now, we want to extract it to the files directory but don’t want to keep the original ownership of the data in the archive. Instead, we set the current user as the owner:

$ tar --no-same-owner -xf files.tar -C files

Notably, this command may yet again require superuser privileges.

In addition, FUSE doesn’t require any temporary files or directories, as it works directly on the archive file. It also supports various archive formats that libarchive supports. Moreover, this method preserves other attributes or metadata of the files or directories in the archive, such as extended attributes.

5. Conclusion

In this article, we’ve understood how to modify file ownership inside a tar archive, using various tools and methods. We’ve also discussed some of the benefits of each method with examples and code snippets to demonstrate them.

We’ve learned that there’s no single best method for modifying the file ownership inside a tar archive. Therefore, we should choose the method that best suits our needs and preferences.

Comments are closed on this article!