Locking files at the system level

Enter Your Query:
Use '%' for wildcards and quotes for "exact phrases"

Top Level » System Level » File System

Locking files at the system levelLast Modified: Oct 27, 2018, 2:25 pm
Permissions can only go so far to prevent a file from being written.
There are some cases where you just want a file to be locked, preventing anything else from changing it.
Most file systems offer tools to do just that.

Let's use a filename example:  

  1. Linux

    For CentOS and Debian, the most command is chattr.  To lock a file, type:

    chattr +i /path/to/file.txt

    and to unlock it, use:

    chattr -i /path/to/file.txt

    To check if a file is locked or not, type:

    lsattr /path/to/file.txt

    The "i" in this case refers to the word "immutable", to be unchangeable over time, so you'd see the 'i' in the list of other flags (usually just many ---- characters) if it's locked.
  2. FreeBSD

    FreeBSD has it's own tools, as chattr is not available.  To lock a file, you'd use:

    chflags schg /path/to/file.txt

    To unlock the file:

    chflags noschg /path/to/file.txt

    and to view if it's locked:

    ls -lo /path/to/file.txt

    where the "schg" will show up in the list if it's locked.

© 2018 JBMC Software, Suite 173  3-11 Bellerose Drive, St Albert, AB  T8N 1P7  Canada.  Mon-Fri 9AM-5PM MST