Bash: permission denied for file write

6

2

I followed this tutorial guide on "How to Create Linux Proc Files in C Program using LKM".

I've successfully made my module and loaded it in. Now I want to echo to my proc file to make sure the method gets called that's supposed to be called.

I have tried:

$ echo "hello" > /proc/procEntry123       # But it says permission is denied!
$ sudo echo "hello" > /proc/procEntry123  # Same error message.

How can I elevate privileges to echo to this file? I am the sole user and admin on this system.

Sam

Posted 2012-09-28T22:13:35.227

Reputation: 63

"but I'm sudo", no actually you're su the super user. It stands for something like "Take this action as the super user" since the second half is do as in "do this, do that" – TheZ – 2012-09-28T23:58:55.190

Answers

13

But it says permission is denied!

It probably says so because you set restrictive permissions when calling create_proc_entry(). (0644 translates to "u=rw,go=r", which only gives write permissions to the owner, which defaults to root.)

I put "sudo" in front of it - same message.

Redirections such as > or | are performed by the running shell, before it invokes sudo.

You have to either use sudo sh -c "echo blah > /proc/blah", or run a root shell with sudo -s.

I am the only user - this is my own personal machine!

This does not matter in Linux. File permissions will be enforced regardless of who uses the computer.

If you don't want that, either log in as root, or use pam_cap to give yourself the cap_dac_override capability – but either method will cause troubles sooner or later.

user1686

Posted 2012-09-28T22:13:35.227

Reputation: 283 655

2No, don't suggest that the inexperienced log in as root. That's a recipe for disaster! – Darth Android – 2012-09-28T22:24:00.210

@DarthAndroid: If they can write a kernel module, they should have enough common sense not to do that. – user1686 – 2012-09-28T22:25:39.147

2But the problem isn't that you're suggesting this to an experienced user, it's that everyone, their brother, and their dog who visits this post will see that you suggested just throwing caution to the wind and logging in as root. There are already 23 views on this question, so essentially 23 people now might think this is okay. :) Just a thought... With that said, a lot of people will log in as root no matter what we tell them, so I guess it doesn't really matter... – jmort253 – 2012-09-29T09:03:54.173

Hi guys - I chmod 777'd the proc file and it was solved. This excercise was purely experimentation so it doesn't matter that the file is rw for all. Thanks for your help! – Sam – 2012-09-29T17:54:37.493