How do you make a file immutable in Linux?

Making a file immutable or not deletable is pretty easy in Linux. It can be done easily with the chattr command or the chmod command. Here is how –

1. Use chmod Command

In Linux, you can make a file not deletable by changing its permissions. You can do this using the chmod command.

To make a file not deletable, you need to remove the write and execute permissions for the owner of the file. You can do this by using the chmod command with the u-w option, which removes write permission for the owner, and the u-xoption, which removes execute permission for the owner.

For example, to make a file called myfile not deletable, you can use the following command:

chmod u-w,u-x myfile

This will remove write and execute permissions for the owner of myfile, making it not deletable. However, note that the file will still be readable by the owner, and other users may still have permission to delete the file, depending on the file’s permissions.

Read Also: How to Delete Directory in Linux?

2. Use chattr Command

An alternative method for making a file not deletable in Linux is to use the chattr command. This command allows you to change the attributes of a file, including the ability to make it immutable.

To make a file immutable, you can use the chattr +i command followed by the name of the file. For example:

chattr +i myfile

This will make the file myfile immutable, which means it cannot be deleted or modified. However, note that the rootuser can still delete or modify the file, as the root user has special privileges.

It’s also worth noting that the chattr command only works on certain file systems, such as ext2, ext3, and ext4. It may not work on other file systems.

3. Use setfacl Command

To make a file not deletable in Linux, you can use the setfacl command to set the d (delete) permission to “deny” for all users. Here’s an example of how to do this:

  • First, make sure you have the acl package installed on your system. You can check if it is installed by running the following command:

dpkg -l acl

If the acl package is not installed, you can install it by running the following command:

sudo apt-get install acl

  • Next, navigate to the directory containing the file you want to make not deletable.
  • Use the setfacl command to set the d (delete) permission to “deny” for all users:

setfacl -m d:u::- /path/to/file

This will make the file not deletable for all users. To make the file not deletable for specific users or groups, you can specify them in place of u::- in the command above. For example, to make the file not deletable for the user john and the group users, you can use the following command:

setfacl -m d:u:john:-,d:g:users:-,u::- /path/to/file

Note that the setfacl command only works on file systems that support extended attributes, such as ext2, ext3, ext4, and NTFS. If the file is located on a file system that does not support extended attributes, the setfacl command will not work.

It’s also worth noting that the root user has special privileges and can always delete any file, regardless of its permissions.

Summary
How do you make a file immutable in Linux?
Article Name
How do you make a file immutable in Linux?
Description
Learn how to make a file immutable in Linux, a valuable security measure that prevents unauthorized changes to important files.
Author

Leave a Comment