1

I try to put value "1" into some virtual file of /proc, and I get "permission denied" despite using sudo:

name@comp-name:/etc/dhcp$ sudo echo 1 > /proc/sys/net/ipv4/ip_forward 
bash: /proc/sys/net/ipv4/ip_forward: Permission denied

I guess that echo command runs with root permissions, but redirection (>) maybe runs without? Can I run both of them with root permissions?

1
  • have you tried using sysctl to write the values in these /proc files?
    – l1zard
    Nov 3, 2012 at 14:18

3 Answers 3

4

In this case, sudo will only apply to echo, as you said.

Instead, you could run an elevated shell like this:

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

Or even shorter:

sudo -s 'echo 1 > /proc/sys/net/ipv4/ip_forward'
0

the recommended way to do that is piping to the program tee

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

0

The problem is that the redirection is done by your shell, not sudo. Ordinarily the program you run gets exec()ed after the redirection. Since you don't have permissions to write to the file, the redirection fails. As mentioned above, the recommended way is to use tee, as in

echo stuff | sudo tee /some/file/path

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .