3

I'm trying to write a script to configure resolv.conf and /etc/network/interfaces automatically. I'm running the commands as "sudo", but 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 and 3 get permission denied errors, but lines 1 and 5 did run. setinterfaces.py is supposed to overwrite /etc/network/interfaces'.setinterfaces.pyworks when pointed at the home folder but not theinterfaces` file.

Any idea? Do I have to be changing ownership? Ideally I'd like this to be a one command script, where I can just call it and it will run. I'm writing this script for people who are not experienced in *nix.

jfa
  • 1,047
  • 3
  • 13
  • 39
  • Line 3 was failing because before I debugged `setinterfaces.py`, `sudo chattr` had already been run. – jfa May 29 '14 at 19:03
  • That second line isn't very likely to work, unless you actually have a program called `/usr/bin/nameserver\ 8.8.8.8`... – twalberg Jun 02 '14 at 19:44
  • Actually, I failed to notice that particular point was already addressed in the accepted answer (it's missing the presumed `echo`). Sorry for the extra noise... – twalberg Jun 02 '14 at 20:25
  • @KeithThompson I don't think it's a duplicate at all. The answer there debugs that line, while the answer here explains why it is necessary. – jfa Jun 30 '14 at 18:34

2 Answers2

4

The sudo command executes the command you give it under the root account. In its simplest form, the syntax is:

sudo command args...

For example:

sudo whoami

prints root.

If you type, as you did in your question:

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

then it's not going to work; it will try to execute a command named "nameserver 8.8.8.8", which doesn't exist. The problem there is that you're missing the 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". That entire string is passed to sudo as a single argument. It needs to see the command and each of its arguments as a separate argument.

So this:

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

is getting closer -- but it still won't work. It executes the echo command as root -- but echo requires no special privileges, so there's no point in executing it as root. The >> /etc/resolv.conf redirection is executed by your shell, which is running as you, not as root. Since you don't have permission to write to /etc/resolv.conf, the command fails. The sudo command never sees the redirection.

You need the redirection to be executed under the root account, which means that you need a shell process running as root. So the solution is:

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

This launches a shell as a root process. That shell executes the command line echo nameserver 8.8.8.8 >> /etc/resolv.conf. Since you have a root shell executing both the echo and the output redirection, it should work.

(I suggest grabbing a copy of your /etc/resolv.conf file before doing this, just to make sure you can recover if you accidentally clobber it.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Ok, thank you. So what if I ran the whole script as `sudo` instead of doing each and every line as `sudo`? Would that mitigate the redirect problem? – jfa May 29 '14 at 20:04
  • Ok thanks. I think that having `sudo` inside the script makes it less simpler to run, so I'll stick with it this way, but that's good to know next time I go to write a script like this. I played with both options and thought it wouldn't make a difference either way. – jfa May 29 '14 at 20:13
1

Second line would be like this,

sudo sh -c "echo 'nameserver 8.8.8.8' >> /etc/resolv.conf"
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 2
    It would be helpful to explain why (the `>> /etc/resolve.conf` in the question is handled by the current shell, not by `sudo`). – Keith Thompson May 29 '14 at 18:09
  • @KeithThompson Yea, I've read something about sudo not applying to the right thing, but I don't really understand. And actually I don't understand why I'm running `sh -c` *inside* a shell script – jfa May 29 '14 at 19:06
  • `sudo echo .... >> /etc/resolv.conf` fails because it gives elevated permissions to the echo command (which doesn't need it), but not to the >> redirection (which does, since the destination file is owned by root). Wrapping the whole command sequence in sudo sh overcomes that – Avinash Raj May 29 '14 at 19:10
  • @JFA: See the answer I just posted. – Keith Thompson May 29 '14 at 19:17