How to Lock a File for Renaming/Deleting in Linux

watch 1m, 35s
views 2

13:14, 08.07.2026

Article Content
arrow

  • Method 1: Prevent File Deletion with chattr
  • Follow these steps:
  • Method 2: Restrict Deletion Using File Permissions
  • Your steps:
  • Method 3: Protect Files by Changing Ownership with chown
  • Your next steps here:
  • Conclusion

Do you need to protect important files from accidental delete? Linux provides multiple ways to secure a file and protect it from unintended modifications. Linux offers several methods to lock a file and prevent unwanted changes. Whether you’re securing critical system files or just keeping your work safe, these techniques will help.

Method 1: Prevent File Deletion with chattr

The chattr (change attribute) command allows you to make a file immutable, meaning it cannot be modified, deleted, or renamed.

Follow these steps:

  1. Open a terminal
  2. Use the following command to make the file immutable:
    sudo chattr +i filename
  3. To verify the attribute, run:
    lsattr filename
  4. If you need to modify or delete the file later, remove the immutable attribute:
    sudo chattr -i filename

This method is useful for system configuration files or sensitive documents.

Method 2: Restrict Deletion Using File Permissions

Linux file permissions control who can read, write, and execute files. By modifying permissions, you can prevent deletion or renaming.

Your steps:

  • Remove write permissions for all users except the owner:
    chmod 444 filename

This makes the file read-only

  • To prevent others from modifying the file, you can set stricter permissions:
    chmod 400 filenam
  • If needed, restore permissions with:
    chmod 644 filename

This approach works well when multiple users share a system.

Method 3: Protect Files by Changing Ownership with chown

Changing the file owner to root or another privileged user can prevent unauthorized modifications.

Your next steps here:

  1. Change file ownership to root:
    sudo chown root:root filename
  2. Combine this with restricted permissions:
    sudo chmod 400 filename
  3. If you need to regain ownership, run:
    sudo chown yourusername:yourgroup filename

This method is ideal for securing configuration files and preventing accidental changes.

Conclusion

Locking a file in Linux is straightforward. You can make a file immutable with chattr, restrict write permissions with chmod, or change ownership with chown. Each method has its own use case, so choose the one that fits your needs best. By applying these techniques, you can safeguard important files from accidental deletion or modification.

Share

Was this article helpful to you?

VPS popular offers

-10%

CPU
CPU
10 Xeon Cores
RAM
RAM
64 GB
Space
Space
300 GB SSD
Bandwidth
Bandwidth
Unlimited
KVM-SSD 65536 Linux

134.99 /mo

/mo

Billed annually

-20.8%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
150 GB SSD
Bandwidth
Bandwidth
10 TB
wKVM-SSD 16384 Metered Windows

100 /mo

/mo

Billed annually

-8.1%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
200 GB HDD
Bandwidth
Bandwidth
Unlimited
wKVM-HDD 8192 Windows

31.25 /mo

/mo

Billed annually

-15.6%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
20 GB SSD
Bandwidth
Bandwidth
30 Mbps
DDoS Protected SSD-KVM 1024 Linux

38 /mo

/mo

Billed annually

-10%

CPU
CPU
10 Epyc Cores
RAM
RAM
64GB
Space
Space
400 GB NVMe
Bandwidth
Bandwidth
Unlimited
Keitaro KVM 65536
OS
CentOS
Software
Software
Keitaro

149.04 /mo

/mo

Billed annually

-10%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
150 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-KVM-SSD 16384 Linux

231 /mo

/mo

Billed annually

-20.5%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
150 GB SSD
Bandwidth
Bandwidth
10 TB
KVM-SSD 16384 Metered Linux

95 /mo

/mo

Billed annually

-10%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
40 GB HDD
Bandwidth
Bandwidth
Unlimited
KVM-HDD 1024 Linux

6.1 /mo

/mo

Billed annually

-10%

CPU
CPU
4 Epyc Cores
RAM
RAM
4 GB
Space
Space
50 GB NVMe
Bandwidth
Bandwidth
Unlimited
aiKVM-NVMe 4096 Linux

17.08 /mo

/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-KVM-SSD 8192 Linux

115.5 /mo

/mo

Billed annually

Other articles on this topic

CloudFlare for your website
Two-factor authentication
CloudFlare for your website
Two-factor authentication
cookie

Accept cookies & privacy policy?

We use cookies to ensure that we give you the best experience on our website. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on the HostZealot website.