LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-09-2016, 10:15 AM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Rep: Reputation: Disabled
Using sudo with cat and redirect?


How can the following be performed using sudo?

Code:
cat >> /etc/init.d/foo <<'EoT'
Hello
EoT
 
Old 12-09-2016, 10:21 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,235

Rep: Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192
Quote:
Originally Posted by NotionCommotion View Post
How can the following be performed using sudo?

Code:
cat >> /etc/init.d/foo <<'EoT'
Hello
EoT
did you try this?
Code:
sudo cat >> /etc/init.d/foo <<'EoT'
Hello
EoT
 
Old 12-09-2016, 10:23 AM   #3
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
did you try this?
Code:
sudo cat >> /etc/init.d/foo <<'EoT'
Hello
EoT
Yes, I did so before asking the question. I think it doesn't work because I am applying sudo to cat and not the file in question?

Code:
michael@raspberrypi:~ $ sudo cat >> /etc/init.d/foo <<'EoT'
> Hello
> EoT
-bash: /etc/init.d/foo: Permission denied
michael@raspberrypi:~
 
Old 12-09-2016, 10:32 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,235

Rep: Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192
Quote:
Originally Posted by NotionCommotion View Post
Yes, I did so before asking the question. I think it doesn't work because I am applying sudo to cat and not the file in question?

Code:
michael@raspberrypi:~ $ sudo cat >> /etc/init.d/foo <<'EoT'
> Hello
> EoT
-bash: /etc/init.d/foo: Permission denied
michael@raspberrypi:~
You're doing it right, the sudo comes before the command.

1. did you make sure the user had sudo privileges in the /etc/sudoers file, and has been added to the group, wheel or sudo, (one or the other) ?

Don't forget to logout then log back in so the group changes will take effect.

Last edited by BW-userx; 12-09-2016 at 10:35 AM.
 
Old 12-09-2016, 10:34 AM   #5
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
You're doing it right, the sudo comes before the command.

1. did you make sure the user had sudo privileges in the /etc/sudoers file, and has been added to the group, wheel or sudo, (one or the other) ?
This works:
Code:
michael@raspberrypi:~ $ sudo touch /etc/init.d/foo
[sudo] password for michael:
michael@raspberrypi:~ $
 
Old 12-09-2016, 10:37 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,235

Rep: Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192
Quote:
Originally Posted by NotionCommotion View Post
This works:
Code:
michael@raspberrypi:~ $ sudo touch /etc/init.d/foo
[sudo] password for michael:
michael@raspberrypi:~ $
try going su
sometime even on my system it will not allow me to do certain things using sudo, so I just go su then do it.
 
Old 12-09-2016, 10:54 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,716

Rep: Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191
The problem with your redirection is that the file is opened by your current shell prior to invoking sudo. If you want the opening to occur in the privileged environment, you have to write the command line that way:
Code:
sudo sh -c "cat >> /etc/init.d/foo" <<'EoT'
> Hello
> EoT
Now it's a privileged shell that is opening the output file.
 
Old 12-09-2016, 11:02 AM   #8
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Thanks rknichols. I think you had a small typo, and this works perfect.

Code:
sudo sh -c "cat >> /etc/init.d/foo <<'EoT'
> Hello
> EoT"
Do you mind explaining why it works? man says the following about sh's -c flag. Also, what are the two > before Hello and EoT all about?
Quote:
Read commands from the command_string operand instead of from the standard input. Special parameter 0 will be set from the command_name operand and the positional parameters ($1, $2, etc.) set from the remaining argument operands.
 
Old 12-09-2016, 11:15 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,235

Rep: Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192
Code:
> # that one adds to a file, over writing everything in that file, replacing
it with whatever you do , whereas,
>> # adds the next line underneath whatever is already there. Appending.

To redirect standard output, use the > symbol. 
Placing > after the cat command (or after any utility or application
that writes to standard output) directs its output to the file name 
following the symbol. Press [Enter] to go to an empty line
 and use the [Ctrl] - [D] key combination to quit cat.
more on that here
https://www.cyberciti.biz/faq/unix-l...ext-to-a-file/

Last edited by BW-userx; 12-09-2016 at 11:18 AM.
 
Old 12-09-2016, 11:22 AM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,716

Rep: Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191
Quote:
Originally Posted by NotionCommotion View Post
Thanks rknichols. I think you had a small typo, and this works perfect.

Code:
sudo sh -c "cat >> /etc/init.d/foo <<'EoT'
> Hello
> EoT"
No, I did not make a typo. The command is correct as I wrote it.

Code:
sh -c "cat >> /etc/init.d/foo"
"sh -c" spawns a shell that runs the command line that is in the quoted string that follows, in this case "cat >> /etc/init.d/foo". Running that shell under sudo gives it the elevated privilege it needs to open /etc/init.d/foo.

There is no need for elevated privilege for the stdin redirection from the HERE document. That can be done by the current shell, and the new shell will inherit that stdin.

The "> " ahead of "Hello" and "EoT" are not something you type. They are the prompt that the shell displays when reading the HERE document from the terminal. I suppose to be consistent I should have shown the "$ " prompt for the full command.

Last edited by rknichols; 12-09-2016 at 11:24 AM.
 
Old 12-09-2016, 11:25 AM   #11
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
The "> " ahead of "Hello" and "EoT" are not something you type. They are the prompt that the shell displays when reading the HERE document from the terminal. I suppose to be consistent I should have shown the "$ " prompt for the full command.


I should have known that. What is funny is that it actually worked as I showed it.
 
Old 12-09-2016, 11:33 AM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,716

Rep: Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191
Quote:
Originally Posted by NotionCommotion View Post
What is funny is that it actually worked as I showed it.
It will work that way, too, with the privileged shell doing the HERE document redirection. It just gets messy trying to include all that in the quoted string, and if that "EoT" were not quoted there would security implications with the privileged shell doing the parameter expansion, command substitution, and arithmetic expansion of the HERE document lines. You generally should do only those things that need extra privilege in the elevated shell.
 
Old 12-10-2016, 10:49 AM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,235

Rep: Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192
Quote:
Use output redirection again for another file and call it home.txt.
For this example, type the command cat > home.txt, then [Enter] ,
followed by: bring the coffee home take off shoes put on sneakers make
some coffee relax! Now, on an empty line,
use the [Ctrl] - [D] key combination again to quit cat.

Code:
userx@voided1.what~>> cat >> test2>>catRedirect <<'EoT'
> g
> g
> g
> g
> bash: warning: here-document at line 21 delimited by end-of-file (wanted `EoT')
userx@voided1.what~>> cat >> test2>>catRedirect <<'EoT'
> EoT


userx@voided1.what~>> cat catRedirect
g
g
g
g
with sudo
Code:
userx@voided1.what~>> sudo sh -c "cat >> testCAtWithSuDo"
hey your whata yo


userx@voided1.what~>> sudo sh -c "cat >> testCAtWithSuDo <<'HO'"
userx@voided1.what~>> 

userx@voided1.what~>> cat testCAtWithSuDo
hey your whata yo
by you doing this experiment by hand yourself, I hope you will better see what is the end point is with what you are trying to do. To gain you your actual needed results.

Last edited by BW-userx; 12-10-2016 at 10:54 AM.
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to redirect all sudo messages to /var/log/sudo driftwood Linux - Server 2 10-18-2012 04:34 AM
[SOLVED] How to redirect cat into script variable sunnieside-up Linux - General 6 09-09-2011 02:49 PM
[SOLVED] bash: for var in $(cat file): via redirect possible??? NOT a while loop jtwdyp Linux - General 5 04-12-2011 06:20 AM
[Question] Cat command with sudo hbinded Programming 4 12-18-2006 01:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:00 PM.

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
Open Source Consulting | Domain Registration