Permission denied editing .txt file

I am trying to add text to an already existing txt file in a system folder.

When I try sudo nano (or sudo su nano) I get a permission denied error. How can I do that?

What is the filename, and what is the full command you are typing?

Can’t remember the file name but it shouldn’t change much. Let’s call it file.txt. I’m tryin g to change it by using either sudo echo or sudo nano > ctrl-o > ctrl-x.
I’m trying to disable transparent huge pages in this case, but there are other cases where I need the same permission.

Sudo doesn’t automatically allow you to change any file. Read-only files stay read-only, and some editors temporarily add write permission when used as root.

Files that are interfaces to kernel objects can report permission denied if the command you’re trying to write is not possible. So in this case the file path matters a lot.

3 Likes

sudo nano file.txt should work, but as Dorota said if the file isn’t writable then you won’t be able to save it.
Something like ls -l file.txt will print out it’s permissions.

To elaborate on that … if the file that the OP is trying to hack is in /sys then it’s not even a ‘real’ file system so all bets are off, and a regular text editor may be a bad idea. You may even get the situation that the ‘file’ is write-only.

So

:+1:

And sudo echo >... won’t work anyway.

For the OP (@vkslputll ), the internet suggests

echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled

but that might not be right since you haven’t confirmed exactly what you are trying to do - and messing around with systemy things can cause breakage. So care is advised.

I’m getting permission errors doing these:

sudo echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
bash: /sys/kernel/mm/transparent_hugepage/defrag: Permission denied

sudo echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
bash: /sys/kernel/mm/transparent_hugepage/enabled: Permission denied

Same with sudo nano.

I will try your suggestion soon
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled

Yes.

See my previous comment:

And sudo echo >... won’t work anyway.

sudo echo >... will never work in this situation because the file is opened for output before the sudo is executed. The file is opened for output in the context of your shell process with only the access that you normally have, not the elevated access that sudo gives you temporarily.

That’s why the | sudo tee hack is needed.

1 Like

Sorry to necro this topic but now I’m trying to edit

/sys/module/kvm_intel/parameters/preemption_timer

It doesn’t allow me to edit it like the previous one:

echo N | sudo tee /sys/module/kvm_intel/parameters/preemption_timer

(I’m trying to turn off preemption timer from Y to N)

I can’t find docs on it, how did you find that you could write to it?

A guide.

Linux is just a headache. I’m sure bad actors have a million easy ways to manipulate these files (when they have root access) but I have to do hours of research to just change it from “Y” to “N”.

1 Like

Once upon a time, they had to do the same research.

Maybe: https://www.cyberciti.biz/faq/slow-performance-issues-of-openbsd-or-freebsd-kvm-guest-on-linux/

I’m asking what source you had because I suspect you’re trying to do somthing impossible. As I wrote before, kernel objects may not be modifiable at all. ‘/sys/’ contains only kernel objects.

Granted, Linux could be more descriptive about errors, but what you’re trying to do is not exactly noob level.

Either way, I can help you only if you cooperate.

I’m not sure but maybe just ls -l /sys/... for whatever you are trying to change. If the permissions start -r-- then the parameter is read-only. If the permissions start -rw- then the parameter is read-write. That would be sensible and straightforward.