File permission on Linux

Asked

Viewed 381 times

0

Hello, my question is not exactly about programming issues, but about Linux and its file access permissions.

Recently, I had to change the permissions of two system files into directories that normally offer read-only access (apparently) to your files. I used chmod 777 to be able to do the editing and now I would like to return to the default linux system files. However, I can’t find any place specifying it. Would anyone know that? Thank you.

  • Could you clarify your question? I think there’s a question in there, but I’m not getting it 100%.

2 answers

2


TL;DR

Log with the same user with whom you have done all the described actions.

Execute the command umask to find out your user’s default permissions. The command will return you a number on octal basis, type 0002 (ignore the first 0, because, like the 0x preceding every hexadecimal number, the first 0 is irrelevant).

If the files you modified nay are directories, then subtract the value obtained with the command umask of 666, and use the resulting value in a new command chmod to be executed on the files. Example:

$ umask
Resultado: "0002"
666 - 002 = 664

$ chmod 664 NOME_DO_ARQUIVO

But if the modified files are directories, replace the 666 in the above calculations by 777.

Explanation of what happened up there

Permissions on a Unix/Linux system are defined by the following sequence of characters:

_rwxrwxrwx 1 dono:grupo

The _ at first you can ignore your problem (it’s a special permissions flag, you can sort the file with special permissions of filing cabinet, directory or no special permission)

Next there are 3 sets of rwx (where r stands for reading permission, w written permission, and x implementing). The first set displays the permissions of the file owner (who created the file), the second set is the permissions of the group to which the file belongs (a group may contain more than one user)and the last set of permissions are permissions for all other users of the system.

dono:grupo represents which user and group the file belongs to.

Then a file with the following permissions _rw_rw_r_ can be Read and Written by the file owner and users belonging to the file group. Other users can only Read the file.

To change the permissions of a file you use the command chmod followed by 3 numbers representing, in order, permissions for Owner, Group, Other Users. Numbers can be:

  • 4 for Reading
  • 2 for Writing
  • 1 for execution
  • any sum of the above (e.g. 4 + 2 = 6, represents Read + Write permission)

That’s why your command chmod 777 released everything you wanted to do in the file, because 7 = 6 + 2 + 1, ie Read, Write and Run permission pro owner, group and other system users.

So now you already know how to set file or directory permission the way you want, but what about

and now would like to return to the default of the linux system files.

For this you can use the command umask. It returns what permissions nay should be set in all files and directories created by the current user on the system. For default users (not root), the default value is 0002 (ignore the first 0 which only indicates that the number is on octal basis), while for root users the default is 0022.

Then to set the permissions of a normal file, subtract the value returned by umask of 666 and use this new value in the command chmod.

And to set directory permissions, subtract the umask of 777.

References

https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions http://www.tutonics.com/2012/12/linux-file-permissions-chmod-umask.html

  • Thank you for the information.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.