When I use sudo to do some activities with files, these files change ownership.
How can I use commands with sudo without changing owner of the files?
Example file archivos35.sh is from apache but I use sed (with usr admin sudo)
$ ls -l
-rwxr-xrw-. 1 apache apache 181 Aug 5 11:56 archivos35.sh
User admin
with sudo —
sudo sed -i s/old/new/g archivos35.sh
But doing that command with sudo changes the owner of the file
$ ls -l
-rwxr-xrw-. 1 admin apache 181 Aug 5 11:56 archivos35.sh
How can I avoid using the command with sudo to change the owner of the file?
I just want to make changes to the file without modifying its owner.
Best Answer
If you need to use
sudo
to modify the file, then use it to switch to the right user. You don't need to switch to root, that's just the default. So, in your case, you'd want to do:That will run the
sed
command as the userapache
.