error message when trying to remove file

Posted on

Question :

When I run the command

root@debian-hy:/var/www/sys# rm teste.txt
rm: cannot remove 'teste.txt': Operation not permitted

Strange because I can edit the file and save, but when I close the vi it generates a swap file, it follows the permissions of the files:

-rwxrwxrwx  1 root root        6 May 21 08:31 teste.tx~
-rwxrwxrwx  1 root root      12K May 21 08:31 teste_tx.swn
-rwxrwxrwx  1 root root      12K May 21 08:31 teste_tx.swo
-rwxrwxrwx  1 root root     4.0K May 21 08:26 teste_tx.swp
-rwxrwxrwx  1 root root       12 May 21 08:31 teste.txt
-rwxrwxrwx  1 root root        0 May 21 08:26 .teste.txt.swp
-rwxrwxrwx  1 root root        0 May 21 08:26 .teste.txt.swx


Answer :

I found documentation ref. the lsattr and chattr commands

The chattr command serves to increase security in the file or directory, the command it uses attributes and not permission such as chmod , these attributes are extensions that do not allow the file to be removed, changed or renamed depending on the parameters used, these attributes can be assigned to the owner of the file and even to root p>

When I ran the command:

root@debian-hy:/var/www/sys# lsattr -d teste.txt
-----a------------- teste.txt

This “a” indicates that the file can be incremented ie add more text at the end of the file, but can not be renamed or deleted.

Then I used the command:

root@debian-hy:/var/www/sys# chattr -a teste.txt
root@debian-hy:/var/www/sys# lsattr -a teste.txt
------------------- teste.txt

Note that I used the lsattr command to see the attributes again, and this time the file has no other attributes, so now it can be removed.




Run the operation with sudo: sudo rm teste.txt

If this does not work, perform the following operations:

sudo chown root teste.txt
sudo chmod 777 teste.txt
sudo rm teste.txt


Leave a Reply

Your email address will not be published. Required fields are marked *