3

I was trying to install SSL certificate (GoDaddy), following their instructions for nginx on Centos. The zip that I downloaded from GoDaddy contains 3 files - A Primary cert, an intermediate cert and a PEM file.
I have root access for my Ubuntu VPS.

But when I use

sudo cat f84e19a2f44c6386.crt gd_bundle-g2-g1.crt >> coolexample.crt

I get

bash: coolexample.crt: Permission denied.  
Zanna
  • 70,465

2 Answers2

4

When sudo cat a is run, file a's contents were read and printed on the stdout with sudo privileges. The redirection of the text was handled by the shell and would be appended to file b (since >> was used). In this particular case it seems that the file didn't have permission to be modified by non-superuser.

Although running shell as root using sudo su, sudo -i or sudo -s seems to be a good idea to overcome this limitation, but it is not suggested. It might happen that shell was running with sudo privileges and unknowingly without exiting the root shell, some other command is used. And if that command is mistyped, it can cause serious harm to the system.

In such cases, cat's output can be piped and tee can be used. It is a utility which read the input from standard input and outputs to file and standard output. Since you want to append the file, -a option can be used. Therefore, the command will look like:

sudo cat f84e19a2f44c6386.crt gd_bundle-g2-g1.crt | sudo tee -a coolexample.crt

I assumed that f84e19a2f44c6386.crt and gd_bundle-g2-g1.crt didn't have permissions to be read by normal users.

Kulfy
  • 17,696
3

Try logging in as superuser with

sudo -s

and then try your command again without sudo in the beginning.

The reason why it doesn't work is because the redirection is done by the shell and not by cat. See this answer for more information: https://askubuntu.com/a/230482

slashleo
  • 93
  • 1
  • 8