Permissions for common files in Linux

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.

write picture description here

  • 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

write picture description here

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 98 10:05 test.sh   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

write picture description here

rw-r–r– permission:

IanA user (rw-) readable, writable but not executable:

write picture description here

write picture description here

IanB user (r–) can read, write, and execute: 
write picture description here

Lisa user (r–) can read, write, and execute: 
write picture description here

rwxrw-r – under permissions:

First modify the permissions: 
write picture description here

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 98 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.
write picture description here

Unreadable, unwritable, executable by Lisa user (-x):

write picture description here 
Mandatory also cannot write, no write permission. 
write picture description here

Deletion of files:

We found that although the owner of test.sh is IanA, test.sh cannot be deleted under the IanA user. 
write picture description here

We have discussed this issue before. 
File deletion principle:

http://blog.csdn.net/codetz/article/details/52415928

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.

write picture description here 
The owner of the parent directory is root. So IanA cannot be deleted. 
Modify it with superuser. 
write picture description here

It can be deleted now. 
write picture description here

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325806076&siteId=291194637