brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tar with numeric owners keeps file permissions right when you restore on another machine

Permissions
~/tips/tip-permissions-tar-numeric-owner

sudo tar --numeric-owner -cpf app.tar -C /srv app

Stores and restores owners by number, so a restore elsewhere gets the right owners.

tar stores owners by name and number, and when you extract as root it matches by name. On a machine where the same name has another UID, files can end up owned by the wrong account.

Use numbers both when you create and when you extract:

sudo tar --numeric-owner -cpf app.tar -C /srv app
sudo tar --numeric-owner -xpf app.tar -C /srv
tar --numeric-owner -tvf app.tar | head

The listing shows 1002/1002 instead of names, which is what gets restored. That matters most for container data: the UID inside a container, like 999 for a database, often has no name on the host or a different one, and the files must keep that exact number to work again.

-p keeps the permission bits exactly as stored instead of applying your umask. When root extracts, tar already restores owners by default, and as a normal user it can’t give files away, so run restores with sudo.

I’d still check the result. If the UIDs differ on purpose between the two machines, say www-data has another number on the new server, numeric restore gives the files to whatever account owns that number there, or to nobody. Run ls -n on the result and compare it with id for the accounts that should own the files before starting the service.

Further reading: GNU tar manual: option summary

More Permissions tips sudo su -s /bin/bash www-data sudo setfacl -R -d -m g:media:rwX /srv/media sudo cp -r /srv/site /srv/site.r ls -ln /srv/data newgrp media All Permissions tips (30) All topics

Link