Sudo script permission denied
This is a pretty simple question, at least it seems like it should be, about sudo permissions in Linux. Is there someway to make this work without having to su or sudo su into root? The problem is that the shell does output redirection, not sudo or echo, so this is being done as your regular user. The issue is that it's your shell that handles redirection; it's trying to open the file with your permissions not those of the process you're running under sudo.
A more complete version which allows you to pipe input in or redirect from a file and includes a -a switch to turn off appending which is on by default :. You can also use sponge from the moreutils package and not need to redirect the output i. Learn more. Asked 11 years, 7 months ago. Active 7 months ago. Viewed k times. David David Active Oldest Votes. Use tee --append or tee -a. Yoo Yoo I absolutely prefer this one. It's just the simplest and it tought me about tee, which comes in handy in other scenarios as well.
I agree. Seems neater than start a new sh too, especially with potentially to do things with environment etc. Under OS X, this should be tee -a instead of tee --append. Matt P Matt P 4, 4 4 gold badges 20 20 silver badges 20 20 bronze badges.
What are "sudo permission boundaries"? Using tee is more popular and more compatible to newer distros. Aug 29 '16 at This is the only one which works as-is as an SSH command which can be executed on a remote node. I agree with other posts here. This uses a shell and only shell so another program like tee is not required here. Yes I know tee is installed already, but maybe not depending on what micro distro you're using like bare bones alpine.
It only takes a minute to sign up. I'm using Amazon Linux. I'm trying to append some text onto a file. The file is owned by root.
I thought by using "sudo", I could append the needed text, but I'm getting "permission denied", see below. You have to use tee utility to redirect or append streams to a file which needs some permissions, like:. The explanation given in the previous answer is basically correct. The command before the redirection is run with elevated privileges, but not the redirection itself. Here is another method which will do what you want.
It has not been mentioned here or in the answers to the question that this is a duplicate of. I include it here for completeness. Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Asked 2 years, 8 months ago.
Active 2 years, 8 months ago. Viewed 9k times. Dave Dave 1, 8 8 gold badges 31 31 silver badges 59 59 bronze badges. Active Oldest Votes. Ravexina Ravexina 1, 1 1 gold badge 11 11 silver badges 25 25 bronze badges. This does not add anything of value that the accepted answer does not already have. Ravexina included a bash -c example in their answer which pretty much does the same thing as su -c so this one is really completely unnecessary. It shows another way of completing this task that is not mentioned in any of the other answers.
Furthermore, the other answer doesn't use echo s escape option not that the OP would miss that. That said, this whole question doesn't add anything. Hence, I flagged it as a duplicate.
However, it fails like this Why is this so? And how do I get sudo echo to work? The problem is that the redirection is being processed by your original shell, not by sudo. As geekosaur explained, the shell does the redirection before running the command. When you type this:. If you're allowed sudoer configs often preclude running shellsyou can do something like this:. That's especially useful if the redirection is the only reason I need sudo in the first place; after all, needlessly running processes as root is precisely what sudo was created to avoid.
And running echo as root is just silly. The tee command acts like a "T" connector in a physical pipeline, which is where it gets its name.
And I switched to single quotes ' So that takes care of writing to files as root using sudo.
Now for a lengthy digression on ways to output newline-containing text in a shell script. First, you can just group all of the echo 's together in a subshell, so you only have to do the redirection once:. My preferred way of doing this would be to use a here-document and avoid the need for echo or printf entirely:.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up. I'm currently studying shell commands, one of the exercises consists in creating a structure with various directories and subdirectories. However, I get the following message bash: hello: permission deniedeven when I type sudo in the beginning. The command worked in another Linux distribution. Also, I used a few minutes ago ls -la in the directory where I want to create the file and it gave me this:.
Moreover, you will get exactly the same error when you run the same command using sudo in front of it. To prevent this, you have to login as root:. You don't have to use in this case sudo for ls command because users from the same group with root and all other users have read and execution permission in that directory. Ubuntu Community Ask!
Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Bash return permission denied error when I redirect the output of ls to a file [duplicate] Ask Question. Asked 6 years, 4 months ago.How to get sudo (root) permission in any termux Android app 2018
Active 6 years, 4 months ago. Viewed 66k times. Also, I used a few minutes ago ls -la in the directory where I want to create the file and it gave me this: drwxr-xr-x 2 root root Nov 26 According to the first triad I have writing privileges? Maybe reason is in place where you are trying to create that files? I tried this on my Xubuntu What path you are? Is the partition mounted as read-only? Rinzwind I posted the directory permissions.
Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I've been given sudo access on one of our development RedHat linux boxes, and I seem to find myself quite often needing to redirect output to a location I don't normally have write access to.
The redirection of the output is not performed by sudo. Run sudo ls. See Steve Bennett's answer if you don't want to create a temporary file.
Thanks go to JdAdam J. Forster and Johnathan for the second, third and fourth solutions. This could also be used to redirect any command, to a directory that you do not have access to.
The problem is that the command gets run under sudobut the redirection gets run under your user. This is done by the shell and there is very little you can do about it. For example:. Assuming you have appropriate permission to execute the command that creates the output, if you pipe the output of your command to tee, you only need to elevate tee's privledges with sudo and direct tee to write or append to the file in question.
In each of these examples you are taking the output of a non-privileged command and writing to a file that is usually only writable by root, which is the origin of your question. It is a good idea to do it this way because the command that generates the output is not executed with elevated privileges. It doesn't seem to matter here with echo but when the source command is a script that you don't completely trust, it is crucial.
A simpler solution is to just use cat like this:. Notice how the redirection is put inside quotes so that it is evaluated by a shell started by sudo instead of the one running it.
This is based on the answer involving tee. As shown in the USAGE in the code, all you have to do is to pipe the output to this script followed by the desired superuser-accessible filename and it will automatically prompt you for your password if needed since it includes sudo. Note that since this is a simple wrapper for teeit will also accept tee's -a option to append, and also supports writing to multiple files at the same time. Then there is no way to do what you want.
Learn more. How do I use sudo to redirect output to a location I don't have permission to write to? Ask Question.
Subscribe to RSS
Asked 11 years, 7 months ago. Active 1 year, 2 months ago. Viewed k times. Jonathan Jonathan DombiSzabolcs You are suggesting that I create the file as sudo first, then give myself permission? Good idea. In many situations you end up here because you asked "why do I get Permission denied? After struggling with these answers I finally choose to redirect to a temp file and sudo move it to destination.Welcome to the most active Linux Forum on the web. Welcome to LinuxQuestions.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration.
This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free. I ran into this interesting problem today. I have the following directory: drwxr-xr-x 2 mcd mcd Sudo's man page doesn't help me understand this any better.
Does anyone have any insight into what's actually going on here? Thanks, McD. Actually the man page DOES say you have to run it in a subshell to allow redirection to work or gives an example that tells you that : Quote:. Thus the permission denied. Hopefully this made a tiny bit more sense to you.
I need to sudo to go thru. If you don't supply that password it won't work, sudo or no sudo. So, if the Linux root user has a. However, if the mysql root user has a password and it's not configured in any. You could add the mysql root user and password to your own user's.
As the other answers point out as well, you might need sudo to write the output somewhere that root Linux, not mysql owns.
Lastly, you don't tell us which user is executing the script, if it's in root 's crontab for examplethen sudo will not be necessary, although you may still need a. If your mysql root user has no password set, then the only issue is writing the output, in which case sudo is required if you don't run the script as root. So, you need to run your script using sudo. This is because the output redirection is still not running as root, but only the dump part.
Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Asked 7 years, 11 months ago. Active 7 years, 11 months ago. Viewed 11k times. I am trying to write mysql backup script for cron. Using Ubuntu server. Current code is as below:! So does the command in script also require sudo? Active Oldest Votes. The script is running as root.