[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

sudo echo 1 > /proc/sys/net/ipv4/ip_forward [was: How to run automatically a script as soon root login]



Since this happens so often, I'm trying to offer a recap.

As others have noted, the above

  sudo echo 1 > /proc/sys/net/ipv4/ip_forward

won't work, since it runs echo under sudo, but the file opening
(that pesky ">") happens in your shell, which is probably running
unprivileged (otherwise, what do you need the sudo for, anyway?)

Others have rightfully noticed that, in this case, sysctl possibly
is the more appropriate tool for the job.

Barring that -- what is to do?

One solution put forward was to wrap a whole shell in the sudo:

  sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Of course, you can (gasp!) wrap a whole xterm which invokes a
shell (why not go the full thing and wrap LibreOffice Calc? It
surely has a shell escape hidden somewhere ;-)

Another solution is to find a program which would be willing
to open a named file for you and sudo that one.

A common choice would be tee (which in this case has the side
effect of dumping the desired output *also* to stdout:

  echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

(also outputs 1 to stdout). Less common, but my personal favourite
is dd, which is free of that side effect (it's harmless enough
when it's just an "1", but if longer data is involved...):

  echo 1 | sudo dd of=/proc/sys/net/ipv4/ip_forward

(note that dd's default if is stdin).

Now share your ideas :-)

Cheers
-- 
t

Attachment: signature.asc
Description: PGP signature


Reply to: