LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-09-2011, 02:41 PM   #1
debsys07
Member
 
Registered: Mar 2007
Posts: 46

Rep: Reputation: 16
Question Root (sudo) can't write to file it created


Debugging some of my scripts after upgrading from Debian Lenny to Ubuntu 10.04. In so doing, I tripped over this "problem," the solution to which may give me a clue to others.

On a bash shell command line I created a file thusly:

sudo touch zero_file

and it lists as expected with default permissions 0644:

-rw-r--r-- 1 root root 0 2011-03-09 11:18 zero_file

But then this command fails

sudo echo abcdef >>zero_file
-bash: zero_file: Permission denied

I can place the command (minus the "sudo") in a script & run it under the auspices of sudo & it works. Am I missing something re the stdin redirection when using sudo?

Thanks,
Ron
 
Old 03-09-2011, 02:54 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
You need to change the permissions for the file to be **6 - you're not root, and only the owner of the file (root) can write to it. Sudo carries over the user and user's group (ie. you don't actually become root). From a bit of experimentation, if you wanted to do the same as "touch" but creating it with your own user, you could use
Code:
sudo echo "" >> zero_file
Hope this helps,
 
Old 03-09-2011, 02:57 PM   #3
debsys07
Member
 
Registered: Mar 2007
Posts: 46

Original Poster
Rep: Reputation: 16
Ah! Never mind -- more googling revealed this hint:

sudo sh -c "echo abcd >>zero_file"
 
1 members found this post helpful.
Old 03-09-2011, 03:11 PM   #4
debsys07
Member
 
Registered: Mar 2007
Posts: 46

Original Poster
Rep: Reputation: 16
Snark, Thanks for the input. However, your command suggestion fails as well. I do need to understand the subtleties of sudo & shells better. And if sudo doesn't make me root, effectively, why did the file created with "sudo touch zero_file" end up with root as owner (and group)?

As I understand it so far, the shell I'm in (and not logged in as root) does the redirection. So, the first part of the command ("sudo echo abcd") executes with root privilege, but then the result devolves back to my non-root shell which can't append it to the file.

I can see myself making this mistake in the future also as I think this will remain something of a mind-screwer for me.

-Ron
 
Old 03-09-2011, 03:28 PM   #5
EDDY1
LQ Addict
 
Registered: Mar 2010
Location: Oakland,Ca
Distribution: wins7, Debian wheezy
Posts: 6,218

Rep: Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578
You have to be part of sudoers group to sudo
 
Old 03-09-2011, 03:31 PM   #6
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
sudo spawns a new context as a particular user. Snarks post is incorrect. If I sudo -u root (or omit the -u parameter) and execute a command, I am root executing that command. sudo bash = root shell. sudo vi /etc/passwd = root editing /etc/passwd. sudo id should be sufficient to convince you.

I/O redirection applies to the current shell context (in which, your current user is the user doing the writing). As such, if you type "echo 'asdf' > file" the shell will attempt to open file for writing and put asdf into it. Clearly, if file is owned by root and only writable by the owner, and you are not root, you don't have permission.

To do what you want requires a bit of thought the sudo sh -c "xxx" is one way, since it tells sudo to spawn a shell which executes only those things which are in the quotations. Additonally, you could write a shell script like (untested):

Code:
#!/bin/bash
FILENAME=$1
shift
echo "$1" > $FILENAME
and execute like:
sudo /path/to/script_writer /path/to/file "all this blah"

Just a heads up.
 
2 members found this post helpful.
Old 03-09-2011, 03:38 PM   #7
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,245

Rep: Reputation: 309Reputation: 309Reputation: 309Reputation: 309
It is priority of execution issue.

Code:
sudo echo abcdef >>zero_file
( (sudo (echo abcdef)) >> zero_file ) - added brackets for clarification
Here the shell executes "sudo" which execute "echo abcdef" (nothing more) and then redirects output to zero_file (without sudo in effect).

Code:
sudo sh -c "echo abcd >>zero_file"
( sudo (sh -c "(echo abcd >>zero_file)") ) - added brackets for clarification
And here shell executes the sudo which executes another shell which execute "echo abcd >>zero_file"

Other words redirect operator ">>" is executed in shell after all commands, has least priority. The quotes can change priority, but unfortunately they also switched off special meaning of ">>" symbol. So trick with another shell was needed.

Last edited by eSelix; 03-09-2011 at 03:45 PM.
 
1 members found this post helpful.
Old 03-09-2011, 03:49 PM   #8
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 249

Rep: Reputation: 31
To clear up some confusion about the "priority" of the > and >> operators, here is a description of what happens:

After you type
Code:
sudo echo "abcdef" >zero_file
your shell parses the line, and then forks a child process. The child process sets its standard output to "zero_file" (in append mode if you use >>), and then it executes the command. Because the file is opened for writing by the shell before sudo is executed, the command fails (since you as non-root don't have permission to write to the file).
 
2 members found this post helpful.
Old 03-09-2011, 03:55 PM   #9
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Lightbulb

I'm not completely sure about this, but I have some ideas why this might be happening. Hopefully someone who knows better will reply to correct any misconceptions I may accidentally convey here.

On my system (Kubuntu) I have a program /bin/echo.

Code:
~$ which echo
/bin/echo
But Bash also has it's own built-in function echo.

http://www.gnu.org/software/bash/man...#Bash-Builtins

Bash built-ins normally take precedence over external programs. When I type echo on the command line, I am normally talking to Bash's built-in echo function. This can be demonstrated by a simple test.

Code:
~$ echo --help
--help
~$ /bin/echo --help
Usage: /bin/echo [OPTION]... [STRING]...
Echo the STRING(s) to standard output.

  -n             do not output the trailing newline
  -e             enable interpretation of backslash escapes
  -E             disable interpretation of backslash escapes (default)
      --help     display this help and exit
      --version  output version information and exit
. . .(goes on for many more lines). . .
  • echo by itself invokes the Bash built-in function. The Bash built-in function does not honor the --help option, and instead treats it as a string to be printed to stdout.
  • /bin/echo invokes the external program which honors the --help option. The help text is printed to stdout.

Here's the part where I tie this into OPs question. Which echo command do I get when I invoke sudo echo?

Code:
~$ sudo echo --help
Usage: /bin/echo [OPTION]... [STRING]...
Echo the STRING(s) to standard output.

  -n             do not output the trailing newline
  -e             enable interpretation of backslash escapes
  -E             disable interpretation of backslash escapes (default)
      --help     display this help and exit
      --version  output version information and exit
. . .(goes on for many more lines). . .
It looks like sudo is invoking the external echo command. If your system is configured similarly to mine, then perhaps you are also getting /bin/echo. I'll assume that is the case.

Quote:
Originally Posted by debsys07 View Post

sudo echo abcdef >>zero_file
-bash: zero_file: Permission denied
  • sudo invokes /bin/echo.
  • The output of /bin/echo is then redirected to zero_file which is owned by root.
  • Permissions -rw-r--r-- means that only processes owned by root may write to the file.

Now let's add your redirection into the mix. Redirection is done by the Bash (I think?), not by external programs. That leads me to some important questions.
  • Which process is performing the redirection?
  • Who owns the process performing the redirection?
  • Does that owner have permission to write to the file?

Here's where I make a few assumptions while attempting to answer those three questions.
  • Bash performs the redirection.
  • Bash is owned by the user sitting at the terminal, debsys07.
  • User debsys07 does not have permission to write to the file.

Now let's look at that solution Google found for you.

Code:
$ sudo touch bar
[sudo] password for me:
$ ls -l bar
-rw-r--r-- 1 root root 0 2011-03-09 15:45 bar
$ sudo sh -c "echo abcd >>bar"
$
  • bash owned by debsys07 invokes sudo.
  • sudo invokes sh for a single command as root.
  • sh (owned by root) performs the redirection.
  • sh (owned by root) has permission to write to the file, and does so.
  • sh (owned by root) exits.
  • Control returns to bash (owned by debsys07).

And that's what I think is going on here. Is there anything I've missed or gotten wrong?

Last edited by Telengard; 03-09-2011 at 04:03 PM. Reason: fix code tag
 
Old 03-10-2011, 10:32 AM   #10
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by orgcandman View Post
If I sudo -u root (or omit the -u parameter) and execute a command, I am root executing that command. sudo bash = root shell. sudo vi /etc/passwd = root editing /etc/passwd. sudo id should be sufficient to convince you.

I/O redirection applies to the current shell context (in which, your current user is the user doing the writing). As such, if you type "echo 'asdf' > file" the shell will attempt to open file for writing and put asdf into it. Clearly, if file is owned by root and only writable by the owner, and you are not root, you don't have permission.
Thanks for the correction
 
  


Reply

Tags
bash, permissions, redirection, sudo


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sudo cd /root gives 'sudo: cd: command not found'. stf92 Linux - Newbie 4 03-03-2012 10:05 AM
[SOLVED] how can i remount root filesystem as read/write after modify readonly-root file jcwkyl Linux - Newbie 3 12-21-2010 11:40 PM
can't write to a file as non root having setuid sathishpy Linux - General 7 02-23-2009 08:48 AM
Deleting a file created by root statman Linux - Security 2 06-17-2005 11:22 AM
cannot edit file as root with write permissions set? Mishley Red Hat 4 08-05-2004 01:23 PM


All times are GMT -5. The time now is 06:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration