Why file is not created as owned by a specific user i designated

KevinZhou :

I have a php script that will collection data and write log into a file, the directory belongs to an user called 'ingestion' and a group called 'ingestion'. I was using the command

sudo -u ingestion php [script] &>> /var/log/FOLDER/adapter.log

The owner and group of FOLDER is ingestion. However, the created adapter.log still belongs to root user and root group, how is this possible?

petre :

Your file is created by the bash running as root, not by the process that you run via sudo as ingestion.

That's because the >> foo is part of the command line, not of the process started by sudo.

Here:

#foo.sh
echo foo: `id -u`

Then:

tmp root# sudo -u peter bash foo.sh > foo
tmp root# ls -l foo
-rw-------  1 root  staff  9 Mar  2 18:52 foo
tmp root# cat foo
foo: 501

You can see that the file is created as root but the foo.sh script is run as uid 501.

You can fix this by running e.g.:

tmp root# sudo -u peter bash -c "bash foo.sh > foo"
tmp root# ls -l foo
-rw-------  1 peter  staff  9 Mar  2 18:54 foo
tmp root# cat foo
foo: 501

In your case, of course, replace "..." with your php command.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174712&siteId=1