Ubuntu 20.04 on WSL2: Folder owner & group are being reset after each start
Usually we do suspect the metadata
support for drvfs filesystems when this type of "permissions or ownership" issue occurs (as noted in the existing answer).
However, in this case I don't believe that's what you are seeing, for multiple reasons:
-
First, the
metadata
option is only applicable to Windows drive mounts. Unless there's a symlink in your/var/www/...
path that you didn't mention, that doesn't appear to be the case here. -
But more than that, if that's the problem you were having, then
chown
wouldn't work at all. Withoutmetadata
support, the files and directories on the Windows drive are always owned by the default user. Achown
would simply have no effect. -
And you mention that permissions are not reset, so I'm assuming you have been able to set some non-default permissions. This also wouldn't be possible on a Windows drive without the
metadata
option.
So with that ruled out (I believe), I'd look at two other possible culprits:
-
My next "normal" suspicion (if this wasn't a web app) would be something in your shell startup. However, I don't see that being the case here. It's easy to eliminate startup config by running with
wsl -e bash --noprofile --norc
if you'd like to test it. -
But what I really think you are seeing here is something like this. It's really "normal behavior" -- The web-server, running as
www-data
, is creating files/directories. These files are naturally owned by the user/process that created them. I don't think that ownership is being reset when WSL restarts. I think they are just being written with that ownership in the first place.If that's causing issues for your development, then add your user to the
www-data
group withsudo usermod -aG www-data $USER
. Or see the linked answer for additional options.
From Stack Overflow:
To enable changing file owners & permissions, you need to edit
/etc/wsl.conf
and insert the below config options:[automount] options = "metadata"
This may require restarting WSL (such as with
wsl --shutdown
) or the host machine to take effect. This has been possible since 2018.
Please visit the link above for the full answer plus the two other answers which you may prefer.
Also consider new method announced by Microsoft in 2018. This method goes into even more depth and involves the mount
command.