Linux cat permission denied

Have root access, still permission denied upon using cat command [duplicate]

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 
bash: coolexample.crt: Permission denied. 

2 Answers 2

Try logging in as superuser with

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

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.

Источник

Receiving Permission denied when trying to use cat command to create a file [duplicate]

I am working with Ubuntu version 18.04.4 LTS. I tried to create a file using the following command: sudo cat > filename.yaml and received a «Permission denied error» I can create the file using the VIM editor and sudo. Please advise.

Читайте также:  Горячие клавиши клавиатуры линукс

2 Answers 2

I’m not an expert on cat but I suspect the command as you’ve entered it above will produce an error by design. Cat is used to print and concatenate files.

Example commands with cat are cat filename.yaml which will print the contents of filename.yaml to standard output (i.e. the terminal); while cat file1.yaml file2.yaml > filename.yaml will combine the contents of the existing files file1.yaml and file2.yaml into a new file called filename.yaml.

Aside from the «permission denied» error (which is from the shell, not from cat , and reflects the user’s lack of rights in the target directory), the command cat > filename will indeed create filename — however, it will hang waiting to read file contents from standard input. Simply hitting Ctrl-D will terminate the command, resulting in creation of an empty file, similar to touch filename or > filename

A good point, thank you; I’ve learnt something. As Ctrl-D means EOF it makes sense that this termination would result in an empty file. However having said that cat still probably won’t be the first tool out of the box next time I want to create an empty file 😉

Источник

permission denied while making a file without asking for password

It’s difficult to tell what is being asked here. This is vague and cannot be reasonably answered in its current form. Please be a little bit more specific or detailed about what you’re trying to accomplish, or take a look at What kind of questions should I not ask here?

4 Answers 4

sudo bash -c 'cat file1 >> file2' 
cat file1 | sudo tee -a file2 

The location you are writing into required super user privileges. However, the way you are writing into the file does not assume this. For this either log in as root using su and then write using cat > list or do sudo vi list .

AFAIK you can’t make a file using cat . You have to use echo instead. So just run this to make a file called «list»:

If you need root privileges you first have autenticate as root using
su —
now you can echo > list
now you can logout as root using logout

This because the redirect is done before sudo starts, that’s why you get «permission denied» without being asked for password.

Читайте также:  Линукс копирование файлов через терминал

cat is used to concatenate files and print on the standard output.
echo is used to display a line of text, using the «> file_name» you will redirect that line of text to «file_name» and if «file_name» doesn’t exist, the file will be created.

PS: There are tons of method to make a new file using the command line but, in my opinion, this’s the faster and easier to remember.

Источник

Why sudo cat gives a Permission denied but sudo vim works fine? [duplicate]

and quiting vim with :wq , everything works fine and my pacman.conf has been manually updated without «Permission denied» complaints. Why is this so? And how do I get sudo echo to work? (btw, I tried using sudo cat too but that failed with Permission denied as well)

6 Answers 6

As @geekosaur explained, the shell does the redirection before running the command. When you type this:

Your current shell process makes a copy of itself that first tries to open /some/file for writing, then if that succeeds it makes that file descriptor its standard output, and only if that succeeds does it execute sudo . This is failing at the first step.

If you’re allowed (sudoer configs often preclude running shells), you can do something like this:

But I find a good solution in general is to use | sudo tee instead of > and | sudo tee -a instead of >> . That’s especially useful if the redirection is the only reason I need sudo in the first place; after all, needlessly running processes as root is precisely what sudo was created to avoid. And running echo as root is just silly.

echo '[archlinuxfr]' | sudo tee -a /etc/pacman.conf >/dev/null echo 'Server = http://repo.archlinux.fr/$arch' | sudo tee -a /etc/pacman.conf >/dev/null echo ' ' | sudo tee -a /etc/pacman.conf >/dev/null 

I added > /dev/null on the end because tee sends its output to both the named file and its own standard output, and I don’t need to see it on my terminal. (The tee command acts like a «T» connector in a physical pipeline, which is where it gets its name.) And I switched to single quotes ( ‘ . ‘ ) instead of doubles ( » . » ) so that everything is literal and I didn’t have to put a backslash in front of the $ in $arch . (Without the quotes or backslash, $arch would get replaced by the value of the shell parameter arch , which probably doesn’t exist, in which case the $arch is replaced by nothing and just vanishes.)

Читайте также:  Amd geode lx800 linux

So that takes care of writing to files as root using sudo . Now for a lengthy digression on ways to output newline-containing text in a shell script. 🙂

To BLUF it, as they say, my preferred solution would be to just feed a here-document into the above sudo tee command; then there is no need for cat or echo or printf or any other commands at all. The single quotation marks have moved to the sentinel introduction

sudo tee -a /etc/pacman.conf >/dev/null  

But while that's how I'd do it, there are alternatives. Here are a few:

You can stick with one echo per line, but group all of them together in a subshell, so you only have to append to the file once:

(echo '[archlinuxfr]' echo 'Server = http://repo.archlinux.fr/$arch' echo ' ') | sudo tee -a /etc/pacman.conf >/dev/null 

If you add -e to the echo (and you're using a shell that supports that non-POSIX extension), you can embed newlines directly into the string using \n :

# NON-POSIX - NOT RECOMMENDED echo -e '[archlinuxfr]\nServer = http://repo.archlinux.fr/$arch\n ' | sudo tee -a /etc/pacman.conf >/dev/null 

But as it says above, that's not POSIX-specified behavior; your shell might just echo a literal -e followed by a string with a bunch of literal \n s instead. The POSIX way of doing that is to use printf instead of echo ; it automatically treats its argument like echo -e does, but doesn't automatically append a newline at the end, so you have to stick an extra \n there, too:

printf '[archlinuxfr]\nServer = http://repo.archlinux.fr/$arch\n \n' | sudo tee -a /etc/pacman.conf >/dev/null 

With either of those solutions, what the command gets as an argument string contains the two-character sequence \n , and it's up to the command program itself (the code inside printf or echo ) to translate that into a newline. In many modern shells, you have the option of using ANSI quotes $' . ' , which will translate sequences like \n into literal newlines before the command program ever sees the string. That means such strings work with any command whatsoever, including plain old -e -less echo :

echo $'[archlinuxfr]\nServer = http://repo.archlinux.fr/$arch\n ' | sudo tee -a /etc/pacman.conf >/dev/null 

But, while more portable than echo -e , ANSI quotes are still a non-POSIX extension.

And again, while those are all options, I prefer the straight tee

Источник

Оцените статью
Adblock
detector