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

sale

-20%

CPU
CPU
2 Xeon Cores
RAM
RAM
512 MB
Space
Space
10 GB SSD
Bandwidth
Bandwidth
300 GB
KVM-SSD 512 HK Linux

€ 7 /mo

€
/mo

Billed quarterly

sale

-20%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
400 GB HDD
Bandwidth
Bandwidth
300 Gb
wKVM-HDD HK 16384 Windows

€ 48.32 /mo

€
/mo

Billed semiannually

sale

-20%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
50 GB SSD
Bandwidth
Bandwidth
1 TB
wKVM-SSD 1024 Metered Windows

€ 15.67 /mo

€
/mo

Billed quarterly

sale

-20%

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

€ 237 /mo

€
/mo

Billed monthly

sale

-20%

CPU
CPU
10 Xeon Cores
RAM
RAM
64 GB
Space
Space
300 GB SSD
Bandwidth
Bandwidth
Unlimited
wKVM-SSD 65536 Windows

€ 138.99 /mo

€
/mo

Billed monthly

sale

-20%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
60 GB HDD
Bandwidth
Bandwidth
Unlimited
wKVM-HDD 2048 Windows

€ 13.7 /mo

€
/mo

Billed monthly

sale

-20%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
200 GB HDD
Bandwidth
Bandwidth
300 Gb
wKVM-HDD HK 8192 Windows

€ 27.47 /mo

€
/mo

Billed semiannually

sale

-20%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB HDD
Bandwidth
Bandwidth
Unlimited
KVM-HDD 4096 Linux

€ 15 /mo

€
/mo

Billed monthly

sale

-20%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB HDD
Bandwidth
Bandwidth
300 Gb
wKVM-HDD HK 4096 Windows

€ 18 /mo

€
/mo

Billed monthly

-10%

CPU
CPU
4 Epyc Cores
RAM
RAM
4 GB
Space
Space
50 GB NVMe
Bandwidth
Bandwidth
Unlimited
Keitaro KVM 4096
OS
CentOS
Software
Software
Keitaro

€ 18.1 /mo

€
/mo

Billed annually

Other articles on this topic

Setting up a reverse proxy
Setting up a reverse proxy
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.