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.