Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

I try to change ip_forward from 0 to 1 but fail, even with root permission in Xubuntu 11.10. I don't have such similar problem while using Ubuntu 11.10

chiaki@chiaki:~$ sudo echo 1 > /proc/sys/net/ipv4/ip_forward 
bash: /proc/sys/net/ipv4/ip_forward: Permission denied

any idea?

share|improve this question

marked as duplicate by Mitch Aug 3 at 9:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers 4

up vote 11 down vote accepted

You can not re-direct so easily with sudo. There are several potential solutions, including tee.

You can re-direct to files you own as the user calling sudo, such as files in your home directory, but not system files.

Example

# it works when re-direction to a location / file the user has permission to access
ubuntu@ubuntu:~$sudo echo "it works" > ~/file
ubuntu@ubuntu:~$cat file
it works

# But NOT if you do not have permission to access the target
ubuntu@ubuntu:~$sudo echo "it works" > /root/file
-bash: /root/file: Permission denied

Option one

use sudo bash -c and quote the entire command

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

Option two

Use tee

echo "1" | sudo tee /proc/sys/net/ipv4/ip_forward
share|improve this answer
    
This completely worked for me! Just a couple of extra questions though: i) can I make those changes somehow persist through reboots and ii) why do those two commands even work?! and iii) why can't I even change the permission or the owner of that file, even when logged in as root? –  George Apr 15 at 21:11
    
@George - see askubuntu.com/questions/311053/… . As to your other questions, best ask a question rather then ask in the comments. Good luck to you. –  bodhi.zazen Apr 15 at 22:59

If you want to change parameters in /proc/sys, the best thing to do is edit /etc/sysctl.conf and then run sysctl -p. That way your changes will persist across reboots.

share|improve this answer

you can use this:

user@ubuntu:~sudo -s 
enter password for sudo:
root@ubuntu:~echo 1 >/proc/sys/net/ipv4/ip_forward :)
share|improve this answer
    
While this probably will solve the problem, it would certainly be useful to know why this works in contrast to the use of sudo in the question. Namely, while echo will be run with superuser permissions, the output redirecton > will still run with the current shells permissions. –  Adaephon May 19 at 9:06

If you enter the following command, it will works if you don't reboot your computer.

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

once rebooted, the parameter will back to "0"

Try the following command, and it will work even after you reboot.

vim /proc/sysctrl.conf ' - uncommen "net.ipv4.ip_forward=1"

sysctl -p 

now, even, when you reboot your machine, IP forwarding will be always enabled.

share|improve this answer

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