19

I tried to run

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

It says Access denied from bash, I then did:

sudo chmod 1+x /proc/sys/net/ipv4/ip_forward

and now it says Operation not permitted.

How can I gain access?

3
  • 5
    Try this: echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward Jun 5, 2016 at 5:09
  • That worked, can you explain what tee does? Jun 5, 2016 at 23:56
  • 3
    The problem with sudo echo 1 > /proc/sys/net/ipv4/ip_forward is that the redirection of the output is done not as sudo, so it doesn't work. From the man page: tee - read from standard input and write to standard output and files, and if done as sudo it does have sufficient privileges to write to the file. Jun 6, 2016 at 3:34

2 Answers 2

25

The permission bits for the file /proc/sys/net/ipv4/ip_forward is:

-rw-r--r-- 

with owner:group being root:root.

So only root can write to the file.

When you do:

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

as a normal user, you won't be able to write to the file due to insufficient permission.

You can do:

  • Use sudo and bash:

    sudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
    
  • Use tee:

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

Note that, you should use /etc/sysctl.conf for persistent operations on the /proc/sys subdirectories.

In a nutshell, to enable IP forwarding, you can just put the following in /etc/sysctl.conf:

net.ipv4.ip_forward = 1

Then run:

sudo sysctl -p

to have immediate effect.

This has the same effect as editing the file /proc/sys/net/ipv4/ip_forward directly, and not to mention far cleaner and of course persistent.

6
  • Doing echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward worked for me, can you explain what tee is? Jun 5, 2016 at 23:57
  • 1
    @ChrisEthanFox tee is command to send whatever it receives to both on the standard output and to files. Check man tee
    – heemayl
    Jun 6, 2016 at 10:29
  • I'm logged in as root but still can not use rm command (rm /proc/sys/net/ipv4/icmp_echo_ignore_all) to remove such file! I even checked immutable bit and its not set on that file... I get the same error "rm: cannot remove ‘/proc/sys/net/ipv4/icmp_echo_ignore_all’: Permission denied". But magically i can edit file as you described! I can't realize whats the problem with rm! Oct 22, 2019 at 14:44
  • @MojtabaRezaeian It's the kernel that's preventing the removal. /proc i.e. procfs lives on the memory and is exposed by the kernel. So you're getting a view of the kernel, and whatever kernel allows you to do to the files on such a FS is totally upto the kernel.
    – heemayl
    Oct 22, 2019 at 17:04
  • @heemayl So how can I remove that file because creating that file was an option to permanently disable icmp ping requests to my server but after creating that file now i changed my mind but there is no way to remove that option(although now its not option when it can not be changed!) do you have any idea how to do that? or maybe it requires to open another question? Oct 23, 2019 at 10:42
1

A simpler solution is using:

sudo su

by this call you will login to your root user and you will be able to edit the file.

N.B: use this at your own risk.

You must log in to answer this question.

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