I am attempting to use sudo
to write to a file in /etc
that is writable only by root
, from inside an unprivileged C program.
system("sudo /bin/sh -c 'echo 'iface wlan0 inet dhcp' >> /etc/network/interfaces'");
This doesn't appear to fail, but the file is unmodified. What is wrong?
For the sake of completeness, here is how I'd do that "the C way":
Structured as you have it, the redirection is evaluated outside the
sudo
operation, and therefore does not have root privileges and cannot open/etc/network/interfaces
for writing. You need to do it like this instead:so that the redirection is evaluated by the shell inside the sudo.
(Also, you can't nest single quotes inside single quotes.)