Score:0

How to grep on a file owned by nobody?

km flag

I have a /etc/openvpn/test.txt file with the following status and a single word content "test":

-rw-r--r-- 1 nobody root  173 Jan 25 21:48 test.txt

When I try to grep on it I get a permission denied error.

/etc/openvpn/test.txt | grep -c "test"

results in:

-bash: /etc/openvpn/consumption.txt: Permission denied
0

If I try to run grep with sudo as nobody I still get the same error:

/etc/openvpn/test.txt | sudo -u nobody grep -c "abs"

results in:

-bash: /etc/openvpn/consumption.txt: Permission denied
0

When I change the owner of the test.txt to root, grep performs as expected.

But I have to keep it as owned by nobody because it is supposed to be read and written to from a client-disconnect openvpn script. I am adviced to run openvpn as nobody for security reasons. Pleaes advise me to handle the situation.

This /etc/openvpn/test.txt | grep -c "test" command has to be run in client disconnect script.

Score:2
cn flag
raj

The file in question has rw-r--r-- permissions, which means it can be read by everyone. Only nobody can write it, but that's irrelevant in this case, as you don't need to write to the file.

The message Permission denied appears because you use the wrong command.

/etc/openvpn/test.txt | grep -c "test" means that you tell the system to execute the file /etc/openvpn/test.txt as a program, and feed the output of that program to the grep command.

Because the file /etc/openvpn/test.txt lacks the execute permission (it has only read permission for everybody and write permission for nobody), the system can't execute it and you get the message Permission denied. So there's no output from this command (the error message goes to standard error and not standard output, therefore you see it on the screen) and grep receives an empty input, so it displays 0 as a result.

This command is wrong. I assume the file /etc/openvpn/test.txt is not a program that can be executed, but it's just a text file which you want to be read by grep. In that case, you should use the following command:

grep -c "test" /etc/openvpn/test.txt

It means run the program grep, and have that program read the file /etc/openvpn/test.txt.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.