90

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 ?

iamauser
  • 11,119
  • 5
  • 34
  • 52

5 Answers5

126

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'
MartyMacGyver
  • 9,483
  • 11
  • 47
  • 67
Yuriy Nazarov
  • 1,536
  • 1
  • 13
  • 16
  • The part I don't understand is that I am already using a bash prompt, then why I need a`bash -c`. – iamauser Sep 16 '13 at 20:48
  • 1
    Because in this case redirection will be performed by bash started with "root" uid that have enough rights to write to /etc/yum.repos.d/. – Yuriy Nazarov Sep 16 '13 at 20:51
  • 2
    You can use any other utility that redirect output. For example "cat << EOF | sudo tee /etc/yum.repos.d/some-name.repo" – Yuriy Nazarov Sep 16 '13 at 20:53
71

Another option is tee.

cat << EOF | sudo tee -a /etc/yum.repos.d/some-name.repo
...
EOF
Daniel Böhmer
  • 14,463
  • 5
  • 36
  • 46
Mark
  • 821
  • 6
  • 2
39

As a variation to @Yuriy Nazarov's answer, only the piped output needs to be elevated thru sudo. The piped input can stay un-elevated:

sudo bash -c 'cat > /etc/yum.repos.d/some-name.repo' << EOF
line1
line2
line3
EOF

This means a much smaller portion of the command needs to be quoted and sent thru to sudo.

Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
  • 3
    This was a huge improvement over the accepted answer in my case, since my piped output was full of variables and shebangs and stuff. – Chuck Wilbur Apr 01 '20 at 18:17
12

As others have pointed out the shell redirection is done by the current shell not by cat. sudo only changes the permission of the program that is executed not of the shell doing the redirect. My solution to this is to avoid the redirect:

sudo dd of=/etc/yum.repos.d/some-name.repo << EOF
PJB
  • 121
  • 1
  • 3
5

if you are using ' inside the text then you may use:

$ sudo bash -c "cat > /etc/postfix/mysql-virtual_forwardings.cf << EOF
user = mail_admin
password = password
dbname = mail
query = SELECT destination FROM forwardings WHERE source='%s'
hosts = 127.0.0.1
EOF
"

this is tested on google cloud's virtual server centos 7.0

Alp Altunel
  • 3,324
  • 1
  • 26
  • 27