Permission denied when trying to append a file to a root owned file with sudo
Run bash
as sudo
:
$ sudo bash -c "cat add_file >> /etc/file"
$ whoami;sudo bash -c "whoami";whoami
iiSeymour
root
iiSeymour
Why sudo cat gives a Permission denied but sudo vim works fine?
The problem is that the redirection is being processed by your original shell, not by sudo
. Shells are not capable of reading minds and do not know that that particular >>
is meant for the sudo
and not for it.
You need to:
- quote the redirection ( so it is passed on to
sudo)
- and use
sudo -s
(so thatsudo
uses a shell to process the quoted redirection.)
How do I use sudo to redirect output to a location I don't have permission to write to?
Your command does not work because the redirection is performed by your shell which does not have the permission to write to /root/test.out
. The redirection of the output is not performed by sudo.
There are multiple solutions:
Run a shell with sudo and give the command to it by using the
-c
option:sudo sh -c 'ls -hal /root/ > /root/test.out'
Create a script with your commands and run that script with sudo:
#!/bin/sh
ls -hal /root/ > /root/test.outRun
sudo ls.sh
. See Steve Bennett's answer if you don't want to create a temporary file.Launch a shell with
sudo -s
then run your commands:[nobody@so]$ sudo -s
[root@so]# ls -hal /root/ > /root/test.out
[root@so]# ^D
[nobody@so]$Use
sudo tee
(if you have to escape a lot when using the-c
option):sudo ls -hal /root/ | sudo tee /root/test.out > /dev/null
The redirect to
/dev/null
is needed to stop tee from outputting to the screen. To append instead of overwriting the output file
(>>
), usetee -a
ortee --append
(the last one is specific to GNU coreutils).
Thanks go to Jd, Adam J. Forster and Johnathan for the second, third and fourth solutions.
Append content to a ROOT file with bash?
From sh
man page:
Enclosing characters in single quotes preserves the literal value of
each character within the quotes. A single quote may not occur
between single quotes, even when preceded by a backslash.
Try:
sudo sh -c "echo -e \"foo\nbar\" >> toto"
Use sudo to change file in root directory
The sudo
command executes the command you give it under the root
account. In its simplest form, the syntax is:
sudo command args...
For example:
sudo whoami
prints root
.
If you type, as you did in your question:
sudo "nameserver 8.8.8.8" >> /etc/resolv.conf
then it's not going to work; it will try to execute a command named "nameserver 8.8.8.8"
, which doesn't exist. The problem there is that you're missing the echo
command.
This:
sudo "echo nameserver 8.8.8.8" >> /etc/resolv.conf
still won't work because there's no command called "echo nameserver 8.8.8.8"
. That entire string is passed to sudo
as a single argument. It needs to see the command and each of its arguments as a separate argument.
So this:
sudo echo nameserver 8.8.8.8 >> /etc/resolv.conf
is getting closer -- but it still won't work. It executes the echo
command as root
-- but echo
requires no special privileges, so there's no point in executing it as root
. The >> /etc/resolv.conf
redirection is executed by your shell, which is running as you, not as root
. Since you don't have permission to write to /etc/resolv.conf
, the command fails. The sudo
command never sees the redirection.
You need the redirection to be executed under the root
account, which means that you need a shell process running as root. So the solution is:
sudo sh -c 'echo nameserver 8.8.8.8 >> /etc/resolv.conf'
This launches a shell as a root
process. That shell executes the command line echo nameserver 8.8.8.8 >> /etc/resolv.conf
. Since you have a root shell executing both the echo
and the output redirection, it should work.
(I suggest grabbing a copy of your /etc/resolv.conf
file before doing this, just to make sure you can recover if you accidentally clobber it.)
Bash / Linux Permissions denied as SUDO
The second line of your "working" example is executed in a new shell that already runs with root privileges, since sudo -s
starts a new shell.
But the :>
in your shell script will be executed by the shell and not by sudo
, so it will run with the original (presumably lower) privileges.
The workaround is to pipe the output of something like echo
to a command using sudo: echo | sudo tee "$DOCKER_LOG_PATH"
.
An even simpler and more explicit solution is to use a command that's explicitly built to truncate files: sudo truncate -s0 "$DOCKER_LOG_PATH"
.
`sudo` to append to file with root permissions (inside `system(3)` from C)
Structured as you have it, the redirection is evaluated outside the sudo
operation, and therefore does not have root privileges and cannot open /etc/network/interfaces
for writing. You need to do it like this instead:
system("sudo /bin/sh -c \"echo 'iface wlan0 inet dhcp' >> /etc/network/interfaces\"");
so that the redirection is evaluated by the shell inside the sudo.
(Also, you can't nest single quotes inside single quotes.)
How to write a file with sudo privileges in Python?
Following solution worked for me finally. I created a new file called etcedit.py
which will write to the file.
os.system("echo %s| sudo -S python etcedit.py %s" % ('rootpassword', 'host_name'))
My etcedit.py file
import os, subprocess
import sys
from sys import argv
def etc_update(host_name, *args):
path = "/etc/hosts"
host_name = host_name[0]
fw = open(path,'w')
fw.write(host_name)
etc_update(sys.argv[1:])
This works!
Related Topics
How to Configure a Systemd Service to Restart Periodically
How to Put the Current Running Linux Process in Background
How to Run Multiple Tor Processes at Once with Different Exit Ips
How to Set Up Curl to Permanently Use a Proxy
Amazon Linux: "Apt-Get: Command Not Found"
Compile/Run Assembler in Linux
How to Check in Bash Whether a File Was Created More Than X Time Ago
How to Map a Hostname *And* a Port with /Etc/Hosts
How to Tar Certain File Types in All Subdirectories
How to Find Files Recursively by File Type and Copy Them to a Directory
How to Convert an Image to Grayscale via the Command Line
Redirecting Tcp-Traffic to a Unix Domain Socket Under Linux
How to Append Contents of Multiple Files into One File
Determining the Path That a Yum Package Installed To
Why Does '/Proc/Meminfo' Show 32Gb When Aws Instance Has Only 16Gb