File Permissions Overview
The permissions of files or directories in linux are closely related to users and user groups.
Each file or directory in linux has a set of 9 basic permission bits. Every three characters is divided into a group. They are the owner bit (3 characters) and the user group permission bit (3 characters) , other user permission bits (3 characters). For example, rwxr-xr-x, it is these 9 permissions (more permission bits will be mentioned later) in linux to control the permissions of the file owner, user group and other users.
- r (read) read permission, corresponding to the number 4.
- w (write) writable permission, corresponding to the number 2.
- x (execute) execute permission, corresponding to the number 1.
- - (without any permissions), corresponding to the number 0.
Example:
Simulation environment:
groupadd inhome # 一个用户组
useradd IanA -g inhome # 属主
useradd IanB -g inhome # 同一个用户组的其它用户
useradd Lisa # 其它用户组的用户
- 1
- 2
- 3
- 4
The members of a user group inhome are IanA, IanB. Other users group member Lisa. superuser root.
[root@ianLinux ~]# mkdir /data/test/IanA -p
[root@ianLinux ~]# cd /data/test/IanA
[root@ianLinux IanA]# vi test.sh
echo "study Linux."
~
~
...
[root@ianLinux IanA]# ls -l test.sh
-rw-r--r-- 1 IanA inhome 20 9月 8 10:05 test.sh
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
rw-r–r– permission:
IanA user (rw-) readable, writable but not executable:
IanB user (r–) can read, write, and execute:
Lisa user (r–) can read, write, and execute:
rwxrw-r – under permissions:
First modify the permissions:
IanA user (rwx) readable and writable executable:
[IanA@ianLinux IanA]$ whoami
IanA
[IanA@ianLinux IanA]$ echo "##" >>test.sh
[IanA@ianLinux IanA]$ cat test.sh
echo "study Linux."
##
##
[IanA@ianLinux IanA]$ ./test.sh
study Linux.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
IanB user (rw-) readable, writable and non-executable:
[IanB@ianLinux IanA]$ whoami
IanB
[IanB@ianLinux IanA]$ cat test.sh
echo "study Linux."
[IanB@ianLinux IanA]$ echo "##" >>test.sh
[IanB@ianLinux IanA]$ cat test.sh
echo "study Linux."
##
[IanB@ianLinux IanA]$ ./test.sh
-bash: ./test.sh: 权限不够
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Lisa user (r–) can read, write, and execute:
[Lisa@ianLinux IanA]$ whoami
Lisa
[Lisa@ianLinux IanA]$ cat test.sh
echo "study Linux."
##
[Lisa@ianLinux IanA]$ echo 111 >>test.sh
-bash: test.sh: 权限不够
[Lisa@ianLinux IanA]$ ./test.sh
-bash: ./test.sh: 权限不够
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-wx-wx-x permissions:
First modify the permissions:
[root@ianLinux IanA]# chmod 331 test.sh
[root@ianLinux IanA]# ls -l test.sh
--wx-wx--x 1 IanA inhome 23 9月 8 11:02 test.sh
- 1
- 2
- 3
IanA user (-wx) is not readable and writable executable:
It cannot be executed without read permission.
Because it is not readable, when vi edits, you can't see the content, but you can still write, but this writing is not normal. :
Unreadable, unwritable, executable by Lisa user (-x):
Mandatory also cannot write, no write permission.
Deletion of files:
We found that although the owner of test.sh is IanA, test.sh cannot be deleted under the IanA user.
We have discussed this issue before.
File deletion principle:
When we delete a file, we do not clear the inode node and block data block of the file. Just delete the name of the file in the block data block of the parent directory of the file, so that the file name disappears and cannot point to the inode node of the file.
The owner of the parent directory is root. So IanA cannot be deleted.
Modify it with superuser.
It can be deleted now.
Summarized test conclusions:
Description of read, write, and execute permissions for linux ordinary files:
- Readable r: Indicates permission to read\read the contents of the file
- Writable w: Indicates the authority to add and modify the content of the file
①If there is no r, then vi cannot be edited, and forced editing will overwrite the data, and echo can be appended.
② The permission to delete files (modify file names, etc.) is controlled by the permissions of the parent directory, and has nothing to do with the permissions of the file itself) - Executable x: indicates the permission to execute the file
①The file itself must be able to execute ②Ordinary
users also need to have the permission of r to be able to
③root can execute
executable files under win32: *.exe, *.bat, *.com
- Executable files under linux: *.sh, *.py, *perl, etc.
Reprinted to https://blog.csdn.net/codetz/article/details/52464275