Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

3 Answers

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
add comment

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

share|improve this answer
add comment

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
add comment

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.