Linux tar: Cannot change ownership to [..]: Permission denied

In a script I was working on, the tar command always reported the following error when I tried to extract an archive:

Cannot change ownership to uid 1000 , gid 1000: Permission denied

But I was executing the script as root! The reason for this error to occur turned out to be relatively simple. Hint: It has to do with CIFS.

The command in the script looked something like this:

# tar xzf $INPUT_FOLDER/archive.tar.gz -C /mnt/test-nas/[..]

So tar tried to extract the contents of the archive to /mnt/test-nas/[..] and failed (even as root). It turns out that tar tries to preserve the file permissions when run as root:

     -p, --preserve-permissions, --same-permissions
           extract information about file permissions (default for superuser)

Since the filesystem /mnt/test-nas/ was mounted via SMB, even root had trouble setting the correct permissions and therefore failed. You can get around this problem by specifying --no-same-owner, so tar won’t try to set an owner:

# tar xzf $INPUT_FOLDER/archive.tar.gz --no-same-owner -C /mnt/test-nas/[..]