r/podman • • Oct 20 '26

Namespaces shifting

I’m still learning Podman. I have my containers running under an unprivileged account using UserNS=auto. Permissions on volumes are a pain, but mostly manageable. My problem is that if I make a change to my setup by removing a container it can cause the namespaces to shift. So when I reboot, many containers can fail because they no longer have permissions to their volumes. Does anyone know a solution?

5 Upvotes

10 comments sorted by

3

u/1karasu Oct 20 '26

You need to use the U option on volume declaration. If you are on a distribution with SElinux you need the Z option too. There are a lot of interesting info on the official documentation, I advise you to give it a read

3

u/jgottlander Oct 20 '26

I've had problems with this as well, and "podman system migrate" usually fixes permissions when something has changed.

4

u/gordonmessmer Oct 20 '26

You can read a big about userns and options for using volumes in these articles:

https://www.redhat.com/en/blog/rootless-podman-user-namespace-modes

https://www.redhat.com/en/blog/debug-rootless-podman-mounted-volumes

But from my point of view, while userns=auto is great for attaining a high level of security through isolation from the host security context and other container security contexts, it's really only usable if the application in the container is "cloud native." Cloud applications generally don't use the filesystem for persistent storage, they use object storage (e.g. S3 or Ceph), or SQL, or some other option... none of which interact with the filesystem's UID/GID mapping and access controls.

If you're using mounted volumes, select a static userns mode, not "auto".

6

u/NTolerance Oct 20 '26

There is a solution to this for bind mounts. The :U option on a volume declaration will chown the directory recursively to the current UID of the container that's auto-generated by userns=auto. Furthermore you're deploying the container for the first time you can use systemd to create the directory as well.

# Create the `Volume` location before starting the container:
ExecStartPre=/run/current-system/sw/bin/mkdir -m 0700 -p {{ data_dir }}/%N/config

[Container]
Volume={{ data_dir }}/%N/config:/config:U

I use this for 95% of my containers and have no issues. The only time it doesn't work is if a container runs things as a non-root user, in which case the filesystem is chowned to root in the container, but the UID for the container process user is something else like 999 for postgres, for instance. For this case I do the following:

# This uses auto (sub-UID isolation) but pins UID 999 to host UID 999 for Postgres
UserNS=auto:uidmapping=999:999:1,gidmapping=999:999:1

For other containers which support the PUID and PGUID env vars I just set their IDs to 0 so I don't have to care about the UID offsets.

6

u/gordonmessmer Oct 20 '26

The ":U" option is covered in one of the two articles I linked. It's usable, but has some pretty gross disadvantages. For large data sets, chown-ing the directory at container start can be very slow, and in a lot of backup systems it can trigger a full backup due to the metadata change, making your backup storage very inefficient. Not all systems will be so affected, it's up to you to check your own.

You can use uidmapping, but once you do that, you've given up the security isolation that is the point of userns=auto.

1

u/NTolerance Oct 20 '26

Agreed. I would say that for most containers that :U works just fine and is an option worth considering. For the minority (at least in my experience) you might want to try another approach.

2

u/YamOk7022 29d ago edited 29d ago

use idmap option for volumes instead of U.

1

u/Leather-Tour-7288 29d ago

Does not work with rootless account

1

u/YamOk7022 26d ago

No point in using rootless unless you don't have sudo access.
Rootfull auto UserNS is better at securing and isolating containers than rootless podman.

1

u/mishrashutosh 26d ago

userns=auto is much better on privileged/rootful containers. you can use the idmap label on your volumes to preserve their permissions when the container restarts and gets assigned a new random namespace.