4

There are some important files that I might delete by accident (if I'm not careful), and I need to ensure that these files cannot be deleted or moved. Is it possible to set file permissions so that I cannot delete or move certain specific files? Also, is it possible to prevent files from being moved or deleted by non-root users?

0

4 Answers 4

7

Also: sudo chattr +i <file> is a good trick. It makes the file immutable.

4
  • 3
    Note that this only works on Ext2/3/4 file systems. (source)
    – gertvdijk
    Dec 6, 2012 at 16:33
  • 3
    Also note even root won't be able to delete the file before changing back to -i. This is not very good while administering or doing some maintenance on the disk if you try to move whole directories as root and you have a lot of +i files in them but this is a strong security against accidental deletes! :)
    – laurent
    Dec 6, 2012 at 16:37
  • You both have right, but the user asked for a solution to prevent accidentally file deletion. ;) I suppose that he uses a default install (ext3/ext4).
    – Frantique
    Dec 6, 2012 at 16:39
  • @Frantique - absolutely, it will work well! (I gave +1). I only made the note as the OP doens't look very proficient with permissions :) and this will work too well as he may be unable to delete the file later and won't know why if he never seen this!
    – laurent
    Dec 6, 2012 at 16:45
6

You only need to do this in your home directory as the system files in /etc, /var or /usr are already set the way you want by default:

You can remove the write permission of the file for everybody (owner user, group and others). This way the file will be readable by you (or the owner of the file) and won't be writable (or deleted/moved) (obs: and also not editable)

Via command line:

chmod -w filename

for directories (recursive to all files inside): chmod -R -w diretory

Obs: you need to use sudo to apply these commands only if you are not the owner of the file.

Via graphic interface in Nautilus:

Right-click on the file and go to permissions to adjust.

You can see a more detailed explanation about permissions in Ubuntu Help

2
  • 1
    Would it be possible to prevent a file from being moved or deleted, while still allowing its content to be modified? Jan 15, 2013 at 19:02
  • I don't think so but maybe someone knows a way to do that.
    – laurent
    Jan 15, 2013 at 21:20
1

chmod 000 <file> (as root or sudo) would prevent any modification to <file> from a non-root user.

4
  • Is it possible to do this using a context menu option for the file (instead of using the command line?) Dec 6, 2012 at 15:33
  • Does this apply to folders as well as files, or is it necessary to use a different command for folders? Dec 6, 2012 at 15:34
  • 1
    1. Hmm yes from nautilus, right click on the file and set perms :) 2. "A Linux system, just like UNIX, makes no difference between a file and a directory, since a directory is just a file containing names of other files." Dec 6, 2012 at 15:40
  • 3
    using chmod 000 will prevent any non-root user to do anything to the file, even reading. Not sure this is what you want.
    – laurent
    Dec 6, 2012 at 15:52
1

Change read-write permission of your own files in your /home folder to read only for the owner, group and others:

  1. Right click on the file and select Properties. For multiple files in the same folder, select files by Ctrl+Click.
  2. Go to Permissions Tab
  3. Change permission for Owner to read only.
  4. Change permission for Group to read only.
  5. Change permission for Others to read only.
1
  • 3
    This will work but there is no need to change the owner to root to do what the OP needs as this question applies only to his home directory as the system files in /etc, /var/or /usr are already set the way he wants by default. Also giving read to other users will make the file readable to all users. tbh this is a kind of killing the use of the permission system: the file will be accessible by all or only by root (no more user permission). Anyways a lot of security files like in .ssh directory or gnome configuration directories will have problems with the owner being root and not the user.
    – laurent
    Dec 6, 2012 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .