Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bash script with downloads some files, and then needs to set the owner to apache, and the group to a particular user group (that apache is not a member of).

Is there a way to create those files with those ownerships as their being downloaded, rather than having to go through and change them? I have to do this for multiple user groups, so I shouldn't be adding apache to these groups, and certainly can't make all of them the default group.

I've tried using runuser, but I'm unable to set the group (presumably because apache doesn't belong to the group).

share|improve this question

As long as you run the script with elevated privileges, there should be no problem. For example:

#!/usr/bin/env bash 
touch foobar
chown www-data:sudo foobar

If you run as a normal user, it will fail:

$ ./foo.sh 
chown: changing ownership of `foobar': Operation not permitted

But if you run it as root, it works fine:

$ sudo foo.sh
$ ls -l foobar
-rw-r--r-- 1 www-data sudo 26M Sep 17 17:34 foobar
share|improve this answer

If you have a bash script, run it as root and fix the ownership inside the script himself.

share|improve this answer

You can use 'setuid' on a c wrapper to chmod it, but this is NOT recommended as it's a very HUGE security hole unless it's extremely restricted in programming.

share|improve this answer

Your Answer


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.