How do I make individual files read-only?

Hello.

As you know Windows allows to right_click > properties > read_only and and all individual files and folders.

Is there an easy way to set the same option on Linux (Fedora KDE in particular, but also Linux in general)?
There are more and less important uses to this (in my case I have to set a game’s config.cfg as read only) but they are all as deserving of this feature.

In Dolphin, right click the file, select Properties, select Permissions tab

From the command line, chmod 444


How do we consider a program (like a game or a text editing software) in this case?

What should be changed here to not allow any software to edit the file?

Be aware that certain files WILL be overwritten despite being read only but a user will not be able to overwrite a read only file unless using an editor like vim.

So this


just doesn’t exist?

[Read Only checked]

You could make it Immutable (cannot be modified or deleted).

sudo chattr +i  config.cfg
1 Like

Could you please leave a short explanation about what

does?

You could have googled a further explanation

https://stackoverflow.com/questions/48578108/what-the-differences-between-chattr-i-file-and-chmod-w-file

I think you might need a little more background on Unix-like filesystem permissions to help with the suggestions in this thread.

Traditionally, in Unix-like systems, files have three different permissions (read, write, execute) for three different entities, user, group, and other. In Dolphin’s permissions dialog, read and write are represented as “view” and “modify” in the dropdown in the image. “Execute” is just a checkbox, but that is just a UI design decision. In addition, these permissions can be set for the owner of a file, the group of the file, and everyone else.

On the filesystem, these permissions are usually represented as a 3 digit number, like 755 or 444. In these, the first number is the permissions for the user, the second for the group and the third for everyone else. And each number is a sum of 4 for read permissions, 2 for write and 1 for execute. So, 7 = 4 + 2 + 1 = r(ead) + w(rite) + (e)x(ecute), 4 = r(ead), 6 = r(ead) + w(rite). This is sometimes also represented symbolically, like u=rwx,g=rx,o=rx.

This sets a file’s permission to u=r,g=r,o=r. In other words, any user apart from root can only read the file, not write, not execute.

Permissions are independent of the type of program, they depend only on which user is attempting an action. If your user iclarke runs a game and this game’s program code attempts to write a file, then effectively you are writing to this file because you have executed the program.

HTH

3 Likes

Thank you very much,
this is what I was asking for.

So I can just use the UI and put it in read-only (since it’s not an executable)!

Beware that you can rename a read-only file without sudo if the user still has write access to the directory that contains the file. You could use something like chmod a-w <path-to-directory> to also remove the user’s ability to rename or delete files from that directory (without sudo).

1 Like

Yes, you can just select “can view” in all three dropdowns and leave the “execute” checkbox alone. This effectively sets the permissions to 444 (u=r,g=r,o=r), i.e., read-only. (Or 555, if the “execute” flag was checked.)

Please note, there is one additional complication which @ernie-07 may have hinted at: There is a way to write to a file with, say, 444 permissions if the writing entity has write permissions on the directory where the file lives. To do this, you can copy the contents to a new, temporary file, making any changes you want in the process (you have read permissions on the file to get the contents and write permissions on the directory to create the new file). Then you remove the old file (again, allowed because you can write to the directory), and finally you rename the new temporary file to the old name. In the filesystem, this is a new file, with a new inode, but from a user’s perspective, who uses a path and filename to access the file, its contents have changed.

Just in case you come across this at some point and wonder why setting a file’s permission may not be enough to actually prevent changes to its contents.

3 Likes

I tested the file I needed to, and it seems to just work as desired.

It’s not as “easy” (quick and easy to understand) as on Windows, but it seems to do the same thing.