Howto: Linux Write protect a file
Q. How do I write protect a file under Linux? I’m using CentOS 5 server Linux operating system. I need prevent accidental changes to my files.
A. There are two ways to write protect a file under Linux.
Method #1: You can make file readonly by removing users’ write permission for a file. Under Linux and UNIX user cannot remove or modify file if they don’t have a write permission. You can use normal chmod command for this purpose.
Method #2 : You need to use chattr command which changes the file attributes on a Linux second extended (ext2 / ext3) file system. You need to setup i attribute. A file with the i attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser (root) or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.
Write protecting a file using chmod command
Let say you want to write protect the file called data.txt so that no other users can change it, enter:
$ chmod go-w data.txt
To provide back permission use:
$ chmod go+w data.txt
Write protecting a file using chattr command
Let say you want to write protect the file called data.txt so that no other users can change it including root user, enter (you must login as the root user to use chattr command):
# chattr +i data.txt
To remove i attribute, enter:
# chattr -i data.txt
E-mail this to a friend
Printable version
Related Other Helpful FAQs:
- How to use dd command to make a diskette
- How Linux file permissions work
- Set Apache Password Protected Directories With .htaccess File
- Linux tar: /dev/st0: Cannot write: Invalid argument error and solution
- How to use chmod and chown command
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: chattr_command, chmod_command, Linux, linux unix, operating system, root user, unix user, write protect file, write_protect




May 3rd, 2008 at 12:13 pm
incorrect syntax
# chattar +i data.txt
To remove i attribute, enter:
this is the correct:
# chattr +i data.txt
To remove i attribute, enter:
May 3rd, 2008 at 12:19 pm
The faq has been updated. Thanks for pointing out typo