4

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.

6
  • Can't you just wrap the parts that create the files in a sudo or something similar?
    – Daniel Beck
    May 18, 2012 at 18:18
  • Why not use sudo -u username command, to run a command in that user's context?
    – Zoredache
    May 18, 2012 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? May 18, 2012 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, 2012 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, 2012 at 20:13

2 Answers 2

1

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.

2
  • 1
    Deleting files requires write privileges to the parent directory.
    – Daniel Beck
    May 18, 2012 at 18:24
  • Yes you are right. He has to take into consideration the permissions of the parent directory.
    – Silviu
    May 18, 2012 at 18:27
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .