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

BashShellSudo

Bash Problem Overview


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 ?

Bash Solutions


Solution 1 - Bash

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'

Solution 2 - Bash

Another option is tee.

cat << EOF | sudo tee -a /etc/yum.repos.d/some-name.repo
...
EOF

Solution 3 - Bash

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.

Solution 4 - Bash

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

Solution 5 - Bash

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniamauserView Question on Stackoverflow
Solution 1 - BashYuriy NazarovView Answer on Stackoverflow
Solution 2 - BashMarkView Answer on Stackoverflow
Solution 3 - BashStephen QuanView Answer on Stackoverflow
Solution 4 - BashPJBView Answer on Stackoverflow
Solution 5 - BashAlp AltunelView Answer on Stackoverflow