bash - Use sudo to change file in root directory -


i'm trying write script configure resolv.conf , /etc/network/interfaces automatically. i'm running commands "sudo", i'm getting "permission denied" errors.

sudo apt-get --assume-yes install vsftpd sudo "nameserver 8.8.8.8" >> /etc/resolv.conf sudo python setinterfaces.py sudo chattr +i /etc/network/interfaces sudo apt-get --assume-yes install lamp-server^ 

lines 2 , 3 permission denied errors, lines 1 , 5 did run. setinterfaces.py supposed overwrite /etc/network/interfaces'.setinterfaces.pyworks when pointed @ home folder not theinterfaces` file.

any idea? have changing ownership? ideally i'd 1 command script, can call , run. i'm writing script people not experienced in *nix.

the sudo command executes command give under root account. in simplest form, syntax is:

sudo command args... 

for example:

sudo whoami 

prints root.

if type, did in question:

sudo "nameserver 8.8.8.8" >> /etc/resolv.conf 

then it's not going work; try execute command named "nameserver 8.8.8.8", doesn't exist. problem there you're missing echo command.

this:

sudo "echo nameserver 8.8.8.8" >> /etc/resolv.conf 

still won't work because there's no command called "echo nameserver 8.8.8.8". entire string passed sudo single argument. needs see command , each of arguments separate argument.

so this:

sudo echo nameserver 8.8.8.8 >> /etc/resolv.conf 

is getting closer -- still won't work. executes echo command root -- echo requires no special privileges, there's no point in executing root. >> /etc/resolv.conf redirection executed shell, running you, not root. since don't have permission write /etc/resolv.conf, command fails. sudo command never sees redirection.

you need redirection executed under root account, means need shell process running root. solution is:

sudo sh -c 'echo nameserver 8.8.8.8 >> /etc/resolv.conf' 

this launches shell root process. shell executes command line echo nameserver 8.8.8.8 >> /etc/resolv.conf. since have root shell executing both echo , output redirection, should work.

(i suggest grabbing copy of /etc/resolv.conf file before doing this, make sure can recover if accidentally clobber it.)


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -