Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sudo cat << EOF > File doesn't work, sudo su does

Tags:

bash

shell

sudo

I tried the following on the command prompt in bash:

sudo cat << EOF > /etc/yum.repos.d/some-name.repo #Content #.... #... EOF 

It complained :

-bash: /etc/yum.repos.d/some-name.repo: Permission denied 

Then I did sudo su and tried the exact same thing except the sudo before cat, and it worked without any problem. What am I missing in the above ?

like image 405
iamauser Avatar asked Sep 16 '13 20:09

iamauser


1 Answers

Output redirection (e.g., >) is performed by bash, not by cat, while running with your UID. To run with root's UID use sudo:

sudo bash -c 'cat << EOF > /etc/yum.repos.d/some-name.repo line1 line2 line3 EOF' 
like image 113
Yuriy Nazarov Avatar answered Sep 18 '22 01:09

Yuriy Nazarov