Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
have you tried using sysctl to write the values in these /proc files? –  l1zard Nov 3 '12 at 14:18
add comment

3 Answers 3

up vote 4 down vote accepted

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'
share|improve this answer
add comment

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
share|improve this answer
add comment

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

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.