There are a few ways you can write protect a file on a Linux system:
chmod
command: You can use the chmod
command to change the file's permissions and remove the write permission for the file. For example, to remove write permission for the file myfile
, you can run the following command:chmod a-w myfile
This will remove write permission for all users (a
) for the file myfile
. To add write permission back, you can use the chmod
command with the +w
option, like this:
chmod a+w myfile
chattr
command: The chattr
command allows you to set attributes for a file, including the i
attribute, which makes the file immutable. This means that the file cannot be modified or deleted, even by the root user. To set the i
attribute for the file myfile
, you can run the following command:chattr +i myfile
To remove the i
attribute, you can use the chattr
command with the -i
option, like this:
chattr -i myfile
Keep in mind that these methods will only write protect the file on the current system. If you need to write protect the file on other systems, you will need to use additional measures, such as encryption or storing the file on a read-only medium.