File permission on Linux
Hello, my doubt 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 in 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 of the linux system files. In the meantime, I can't find nowhere specifying this. Would anyone know how to tell me that? Thanks.
2 answers
TL; DR
Log in with the same user with whom you did all the described actions.
Run the umask
Command to find out your user's default permissions. The command will return you an octal-based number, type 0002
(ignore the first 0
, because, like the 0x
that precedes every hexadecimal-based number, the first 0
is irrelevant).
If the files you modified are not 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 calculations above with 777
.
Explanation of what happened over there
Permissions on a Unix / Linux system are defined by the following character sequence:
_rwxrwxrwx 1 dono:grupo
The _
in the beginning you can ignore for your problem (is a special permissions flag, which can classify the file with special permissions of file, directory or no special permission )
Then there are 3 sets of rwx
(where r
represents read permission, w
write permission, and x
execution). The first set displays the permissions of the user who owns the file (who created the file), the second set is the permissions of the group to which the file belongs (a group can contain more than one user), and the last set of permissions are the 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 the 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, the permissions for owner, Group, Other Users. The numbers can be:
- 4 for reading
- 2 for writing
- 1 for execution
- any sum of the previous ones (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, that is, permission to read, write and execute pro owner, group and others system users.
So now you know how to set the permission of files or directories the way you want, but what about
And now I would like to return to the default linux system files.
For this you can use the command umask
. It returns which not permissions should be set on all files and directories created by the current user on the system. For default (non-root) users, 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
from 666
and use this new value in the chmod
command.
And to set directory permissions, subtract umask
from 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
Uses the command
ls -lah
It will show you the permissions of the files.
See this link: https://www.vivaolinux.com.br/dica/Entendendo-as-permissoes-de-arquivos-no-Linux
I believe the default is 644...