Problem
To me, there is no simple solution to your problem: even if you postpone the ownership change, you will need to swap it twice again the next time you want to edit/maintain a file.
That results in a two-headed environment: sometimes developers are allowed to edit stuff, sometimes they are not... Why is that so? If it is a production server, they should not use it as a playground, if it is a development server, they should never be restricted editing content!
Proposed solution
I would suggest you need to rethink how your setup is built:
- Use a production environment where only automated publishing tools (Git? rsync?) have the means and access rights to change (publish) content
- Use (a) development environment(s) where developers are happy to mess things up
- Additionally, one usually use a 'staging' environment where test content is used in a setup imitating production, just to be sure everything works well and where to run test simulating the real-world
To solve the problem of multi-access to files, I would recommend using the access control mechanisms built in the Linux filesystem mangement, with the default rights:
user
(owner) has rw
rights --> Use it for content modifier(s), ie developers in development environment, publishing tool in staging/production
group
has r
rights --> Use it for content accesser(s) (ie Web server, backend applications, etc...), making sure all those belong to group
Special cases such as directories where Web server/applications need write access, such as an upload directory, make an exception by adding the w
for group
permission to this directory (or make this specific directory locked to anyone else by making it owned by group
)
Now, to automatically make new files being readable by the Web server/applications, you can use the setgid
flag from the filesystem permissions. This will automatically change the group of any added file to group
.
Sum up
Here is a quick example so sum up everything:
Your web server and any backend application belong to the www-data
group.
You have a /srv/www/
Web root on 2 environments:
- Development, with owners
dev:www-data
and rights 4755 rwxr-sr-x
- Production, with owners
git:www-data
and rights 4755 rwxr-sr-w
To update content in Development, use the dev
account or any tool using that user.
To update content in Production, use the git tool rightly configured to push data at the corretc location, using the git
user.