Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way to run a command as root (using sudo perhaps) but have the files created by that command be owned by someone else?

Some context: I have a web server that I run as root in order to have access to port 80. That web server creates files, which are then owned by root. But, when I do a make (using Maven actually) as my normal user, I get a bunch of errors because it can't remove some of the root-owned files. So, it would be nice if I could run the web server as root, but the files it created were owned by me.

I made a script to "clean out" the files that interfere with the make, so I'm not really interested in solutions to that problem ... I'm just curious if there's a way to run as root but create files as someone else.

share|improve this question
    
Can't you just wrap the parts that create the files in a sudo or something similar? –  Daniel Beck May 18 '12 at 18:18
    
Why not use sudo -u username command, to run a command in that user's context? –  Zoredache May 18 '12 at 18:31
2  
I'm puzzled, normally webservers like Apache are started by root (e.g. from rc2.d) but switch to a non-privileged user after they have bound to port 80. So any files created in response to HTTP requests are owned by the non-privileged user. What have I missed? –  RedGrittyBrick May 18 '12 at 19:06
1  
If you're using Apache, the httpd.conf directives are User and Group, to add to what @RedGrittyBrick writes. Running a web server as root is a really bad idea. –  Daniel Beck May 18 '12 at 19:18
    
It's not Apache, or any other publicly available web server, it's private code that provides web services. However, I didn't write it, so I don't particularly want to dive in and change how files are written. Again, I more or less solved my particular problem, but want to know if there's an easy way to run commands as root but create files as someone else. –  shawkinaw May 18 '12 at 20:13

2 Answers 2

There are a few ways to achieve this...

  1. The files have the flags for others (o, last set of permissions from left to right) to be able to write to them. Like that you can modify your files without being root.

  2. Your user is being part of group common with the root user. Like this, if you have the group permissions (g, middle set of permissions) to write the files, you can modify them without being root.

EDIT: Also, as Daniel stated, in order to be able to delete the files, you'll need to have permissions over the parent folder too.

share|improve this answer
1  
Deleting files requires write privileges to the parent directory. –  Daniel Beck May 18 '12 at 18:24
    
Yes you are right. He has to take into consideration the permissions of the parent directory. –  Silviu May 18 '12 at 18:27

Exactly because of the additional privileges often required to start listening on the http port, most web servers have the built-in feature of assuming a different identity once started. In Apache, for example, the httpd.conf file contains the entries 'user' and 'group' that control this feature.

Usually, web servers get their own dedicated user and group. Browse through your configuration and check whether you can set this up.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.