2

I have a file

-rw-rw-r-- 1 root mygroup 0 Sep 10 14:54 /etc/nginx/mygroup.txt

The access rights are set so that the group mygroup has read and write permissions for the file. Writing the file with a member of mygroup works when editing with vi /etc/nginx/mygroup.txt. However, when I try to append a text to the file with the same user using

echo "blaa" >> /etc/nginx/mygroup.txt

I get

-sh: 3: cannot create /etc/nginx/mygroup.txt: Permission denied

The directory permission for the containing directory are

drwxr-xr-x  6 root root    4096 Sep 10 16:07 nginx

The user is able to list the contents of the directory.

Why does this happen and how can I add a line to the text file from bash without using a text editor?

Additional information based on comments:

The user has no sudo rights.

Is this a duplicate of Why can't sudo redirect stdout to /etc/file, but sudo 'nano' or 'cp' can? ? I don't think so, because running

printf "blaa" | tee /etc/nginx/

produces also

tee: /etc/nginx/whitelist-jutik.txt: Permission denied

Also all the other suggested duplicate questions are for users with sudo rights, so I think this question is not a duplicate of any of them.

Edit: finding this question solved the problem: Group member cannot write even with write permission

Apparently the group permissions were not completely in effect. Logging in again with the user solved the problem, he can write the file with both vi and echo.

simon
  • 289
  • What are the directory permissions? – glenn jackman Sep 10 '18 at 14:18
  • I edited the answer to display directory permissions. – simon Sep 10 '18 at 14:21
  • 1
    You should also edit the question to demonstrate that this is not https://unix.stackexchange.com/questions/19707/ , https://unix.stackexchange.com/questions/42801/ , https://unix.stackexchange.com/questions/1416/ , or https://unix.stackexchange.com/questions/31322/ yet again. (-: – JdeBP Sep 10 '18 at 14:23
  • "doing it with vi". Do you use vi /etc/nginx/mygroup.txt or sudo vi /etc/nginx/mygroup.txt? – Chris Davies Sep 10 '18 at 14:24
  • The user has no sudo rights, so I use vi without sudo. – simon Sep 10 '18 at 14:25
  • Why would the error say "create"? Doesn't the file exist yet? – Benjamin W. Sep 10 '18 at 14:27
  • Well, that's what I'm curious about too. – simon Sep 10 '18 at 14:27
  • 1
    When you were using vi, did the file already exist? Similarly: when you were using echo did the file already exist? Your user doesn't have permission to create a file in that directory but it does have permission to modify the existing file as shown to us. – Chris Davies Sep 10 '18 at 14:30
  • The file existed as a 0-byte file (created a bit earlier with touch). – simon Sep 10 '18 at 14:32
  • 1
    In both cases? Or just for vi? Or just for echo? – Chris Davies Sep 10 '18 at 14:33
  • Hard to say. I recreated the file and all the permissions, but cannot reproduce the vi part anymore. :/ – simon Sep 10 '18 at 14:44
  • Ok, found the solution and edited question accordingly (see last part). Thanks everyone. – simon Sep 10 '18 at 14:49
  • 1
    Simon, please write an answer summarising your findings rather than updating the question. You can then accept it (tick-mark alongside the voting buttons on the left) so we can (easily) see the question's been answered. – Chris Davies Sep 10 '18 at 15:42

0 Answers0