`sudo echo "bla" >> /etc/sysctl.conf` permission denied
Solution 1
You can't use sudo
to affect output redirection; >
and >>
(and, for completeness, <
) are effected with the privilege of the calling user, because redirection is done by the calling shell, not the called subprocess.
Either do
cp /etc/sysctl.conf /tmp/
echo "net.ipv4.ip_forward = 1" >> /tmp/sysctl.conf
sudo cp /tmp/sysctl.conf /etc/
or
sudo /bin/su -c "echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf"
Solution 2
You might find it simpler to use this command:
echo net.ipv4.ip_forward = 1 | sudo tee -a /etc/sysctl.conf
Solution 3
sudo
runs only your command, not the redirect, as root. You'll need to wrap it all in a command where the whole thing runs as root:
sudo sh -c 'echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf'
Solution 4
The command sudo echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
is interpreted as that you (nonroot) write the result of sudo echo "net.ipv4.ip_forward = 1"
into /etc/sysctl.conf.
Run
sudo -s 'echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf'
or
sudo su -c 'echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf'
to run echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
as root.
Solution 5
sudo sed -i "$ a <text>" <file>
-i
: edit file in place.$ a
: append text to the last line
Using sed
command avoids you the hassle of redirections and pipelines.
In your case: sudo sed -i "$ a net.ipv4.ip_forward = 1" /etc/sysctl.conf
Related videos on Youtube
bevacqua
Principal Software Engineer at Elastic, helping lead the Elastic Cloud UI team. ● Pre-IPO employee at Elastic, helping drive key initiatives in Elastic Cloud ● Authored 3 books on JavaScript/Node.js application architecture ● Organized first ever Node.js conference in Buenos Aires (NodeConf Argentina) ● Author dragula drag & drop library (20k stars) and prolific open source contributor ● Wrote my own MVC frameworks, clones of async, jQuery, Markdown parsers, and more (purely as a way of learning) ● Grew newsletter on JavaScript to 17k subscribers (Pony Foo Weekly) ● 40k karma on StackOverflow 😅 ● Avid reader Published author of several software development books: Mastering Modular JavaScript (O'Reilly, 2018), Practical Modern JavaScript (O'Reilly, 2017), and JavaScript Application Design (Manning, 2015). Nicolás has vast experience in tackling challenging technical problems, as well as in helping manage teams, driving technical innovation, collaborating across areas, and sharing his applied learning.
Updated on September 18, 2022Comments
-
bevacqua over 1 year
Disclaimer: I'm pretty novice at sysadmin stuff.
I'm trying to set up port forwarding in an AWS EC2 instance, this has to be done in the command-line because I don't want to go in and edit anything, it has to be automatic (it's part of a build process).
sudo echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
Permission denied
The weird thing is I've been (successfully) using
sudo
for pretty much every command that requiredsu
privileges. If I dosudo su
before the command (trying it out by hand in anssh
session), then it works.Reasons behind this? Possible solutions that don't involve
sudo su
or manual edits?-
MirroredFate over 10 yearsSame problem here:stackoverflow.com/questions/82256/… -- Also, if you want to be really risky:
sudo -i
-