collect in the archive, then the owners and permissions remain the same
for example, tar , which is very often used for archiving , does just that - it preserves both the ownership of files and the permission bits set (executability / write / read for the owner / group / others).
demonstration:
$ touch user.file # создадим файл $ chmod u=rwx,go= user.file # запретим доступ к файлу всем, кроме владельца # а владельцу установим все три бита: rwx $ sudo touch root.file # создадим файл от имени root-а # он будет его владельцем $ tar -czf a.tgz *.file # запакуем эти файлы в архив и сожмём его
check that in the archive:
$ tar -tvf a.tgz -rw-r--r-- root/root 0 2017-06-05 23:22 root.file -rwx------ user/user 0 2017-06-05 23:21 user.file
as we see, everything was correctly archived - both the belonging and the access bits.
but
if you unpack this archive on behalf of an ordinary user, then:
$ mkdir dir $ tar -xf a.tgz -C dir $ ls -l dir -rw-r--r-- 1 user user 0 Jun 5 23:22 root.file -rwx------ 1 user user 0 Jun 5 23:21 user.file
then, as we see, the bits were restored, but the ownership of the files was not: both belong to the user (who was unarchiving).
so that file ownership is restored
You must unpack the archive as root user :
$ sudo tar -xf a.tgz -C dir $ ls -l dir -rw-r--r-- 1 root root 0 Jun 5 23:22 root.file -rwx------ 1 user user 0 Jun 5 23:21 user.file
now everything is “in place”: both ownership and access rights.