Linux protect file from remove

How to Protect Files and Directories from Deleting in Linux

Are you working as a Linux Admin? Do you generally defend script documents or any important files? You might be aware that, Chattr (change Attribute) is a command line Linux utility that is used to set/unset exact attributes to a file in Linux process to comfortable unintended deletion or change of major files and folders, although you’re logged in as a root user.

This article helps you to look after your records or folders that may support in disallowing an unauthorized user to delete your touchy contents.

Protecting File

In the below example abc.txt is the file name

Protecting a file

To protect file, use the following command –

Now try to remove file, using rm command as shown below –

The sample output should be like this –

Removing the protection to file

To remove the protection to file, use the following command –

To verify the above command, use the following command as shown below-

The above command removes the file without giving information.

Protecting Directories

In the below example abc is a directory name

Protecting a directory

To protect directory, use the following command –

To verify the above command, use the following command as shown below –

The sample output should be like this –

Removing protection to the directory

To remove protection to the directory, use the following command-

To verify the above command, use the following command as shown below –

The above command remove the file without giving information.

In the above article, we have learnt about – Learn how to protect files & directories from delete. In our next articles, we will come up with more Linux based tricks and tips. Keep reading!

Источник

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.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

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.

Читайте также:  Mdnsresponder mac os что это такое

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to delete a write protected file

Reader Ashish asks “How to delete write protected file under Linux…”

Under Linux (and other Unixsh oses) following criteria should matched before deleting a file:

Criteria # 1

To delete or remove a file you need to be owner of a file. Use ls -l command to verify your ownership:
$ ls -l filename

If you have sufficient rights, you can remove a file. Otherwise, become a superuser to remove a file.

Criteria # 2

In addition, you must have a write access to on a directory that allows you to remove or add new files. Again us ls -l or ls -ld command to get write ownership of directory.
$ ls -ld dirname

If you have sufficient rights, you can remove a file. Otherwise, become a superuser to remove a file. Use chown / chmod commands to setup permission OR run rm command as root user:
# su —
# rm -f file.txt

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Criteria # 3

Make sure file is not protected using extended file system ‘ 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 root can add or remove ‘ i ‘ attribute. Use lsatter command to verify that file is not write protected using extended ‘ i ‘ attribute:
$ lsattr file.txt

Above output indicates that file.txt has i attribute set on it. Become a superuser and follow the instruction mentioned here to remove a write protected file:
# chattr -i file.txt
# rm -f file.txt

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Your commands are slightly incorrect. Change chattar to chattr and lsattar to lsattr.

They stand for “CHange ATTRibute” and “LiSt ATTRibute”

Apologies for the typo.

sir, my sd card neither copy files from another source not deleting file from it. Kindly suggest a method to solve my problems.

can u use that
in deepfreeze
without knowing the password

can u delete deepfreeze just like that
it is write protected
and then theres password when you need to disable

is there any other method deleting write protected file

On the Mini SD adapter, there is a lock tap you push down usually when you put your mini SD card in to it, then into the USB adapter. To turn off the write protection on your mini SD, simply do not lock your mini SD in its adapter.

ok i had the same problem i downloaded a patch from a peer 2 peer site and it was’nt wot i wanted
and tried to delete it but it said it was write portected so i just tried to just stuff it up as much as i could just playin round with its properties and here is wot i came up with GO INTO PROPERTIES OF THE FILE THEN CLICK ADVANCED MAKE SURE THE BOXES THAT SAY…… FILE IS READY FOR ARCIVING,……FOR FAST SEARCHING ALOW INDEXING SERVICE TO INDEX THIS FILE AND…. COMPRES FILES TO SAVE DISC SPACE .. ARE ALL UNCHECKED THEN CLICK OK THEN GO TO THE PART THAT SAYS ….READ ONLY AND TICK IT THEN PRESS OK IT SHOULD EXIT IF ANY ERRORS COME UP THEN JUST CLICK OK THEN TRY DELETEING IT IT WORKED 4 MEHOPE IT DOES 4 ALL U PPL 2 NOTE:I DONE THIS WITH A FILE NOT A FOLDER SO DONT EXPECT IT TO WORK ON FOLDRES I DONT EVEN KNOW IF IT WORKS ON ALL FILES IT JUST WORKED 4 ME AT THE TIME AND WHEN I SEARCHED HOW TO FIX IT IT DIDENT REALY GIVE ME A CLEAR ANSWER ANYWAY E MAIL ME AND TELL ME HOW U WENT at dime_megga@yaoo.com.au 1 more note remember to close all programmes that even could b useing it even shut down your torrent if u have to before u try to get rid of it ok c ya sorry bout the typos

Thanks! Becoming su and using the command
rm -f filename worked. I am using an mp3
player as a card reader to transfer files between
computers (one Red Hat 7.2 and the other FC3),
and I have never had this problem (not being
able to delete a file just by clicking and
choosing “move to trash”) before. It is very odd.
I have not changed permissions on any of the
devices. It is not the card reader (i.e. mp3
player) because I had the same problem with 2
different ones. Do old SD cards often do this?
Mine isn’t even that old and does not get heavy
use. Thanks again for all the useful advice.

@Jashawn Ram:
If your system has automounted the SD card, you may find that everything from its mount point down is owned by root. if you’re trying to remove or write files to the SD you’ll either have to:
1) Do it as root.
2) As root, change the ownership recursively on the SD to your personal account.
EX. chown -R matt:mattgrp /the/path/to/your/SDcard

hel me when i install any thing in memory card its become the disk is write protected remove te write protection or use another disk

how do i gain access to a file that is share protected on my other user account i forgot the password to it and i really need to get my pics from that account and i tried accessing them from my other account but it says i don’t have permission to access

After installing f-secure antivirus, i am not able to see any programs in remove programs also i am not able to move mr either delete anything.what should i do.

i use ipod.in that ipod ihave write-protectional error while format the drive

plz give me a solution

how to solve write protected peoblem

sir, i hav a sandisk sd micro sd card (1 gb), but when i connected it in to my laptop(lg-ls50), it shows- the card is write protected.for that reason i could not format or transfar or delect any files,plz tell me, what to do? to solve this type of problem.

If you check carefully the SD card adapter (If you are using a micro SD) or the Sd card itself, in the side there is a little switch (colored white or black) that enable or disable the “write-protection”, look for the word “Lock” folowed by a little arrow, move the switch and that’s it.

I tried everything but still can’t fix it

Dear Pro,
My problem is same and i have not got any solution if I, will send u
Anand

HOW I REMOVE WRITE PROTECTED

HOW I REMOVE WRITE PROTECTED

Gracias por la información
en realidad pude resolver mi problema

i want remove write protection from USB pen drive, pls sujjest me about write protection
I have so many time try for removing the write protection but no result from site

sir, i’m using a ”king stone” pendrive and a ”card reader” with ”memory” card. from last 3 days both have been showing write protection disc. i can copy from any pen drive or memori card but couldn’t send any item to any pendrive or memorycard. when i’m going to send any contents it shows a write protection disc… sir, i’ll be highly thankful if you kindly reply my coments as quickly as possible….
thanking you……..

Hey Friend,
I have the same problem of write protection when ever I try to add file or remove files the system is showing that it is a “Read Only File System” Please help me with this I tried with chmod and chown but both of them are not worling .

Источник

Читайте также:  Не удалось загрузить windows поскольку ядро отсутствует или повреждено 0xc0000221
Оцените статью