Linux system is a typical multi-user system, where different users are in different positions and have different permissions. To protect system security, The Linux system has different permissions for different users to access the same file (including directory files).

In Linux, we usually use the following two commands to change the owner and permission of a file or directory:

  • chown(change ownerp) : changes the owning user and group.
  • chmod(change mode) : changes the user permission.
1 wangwu_01 commonUser 0 7月 12 21:50 index.js drwxr-xr-x. 2 wangwu_01 drwxr-xr-x CommonUser 6 July 12 21:50 WebCopy the code

The file type

  • As for thedIs a directory
  • As for the-Are files;
  • iflIs a link file.
  • ifbRepresents the interface device (random access device) that can be stored in the device file;
  • ifcRepresents a serial port device in a device file, such as a keyboard or mouse (a one-time reader device).

permissions

  • The first, fourth, and seventh characters indicate the read permission. If r characters are used, the read permission is available. If – characters are used, the read permission is unavailable

  • The second, fifth, and eighth characters indicate the write permission. A w character indicates that the write permission exists. A – character indicates that the write permission does not exist.

  • The third, sixth, and ninth digits indicate the execution permission. If x is used, the execution permission is granted. If – is used, the execution permission is not granted.

Owner and owner group

For a file, it has a specific owner, that is, the user who has ownership of the file.

[root@www /]# ls -l
drwxr-xr-x 3 mysql mysql 4096 Apr 21  2014 mysql
Copy the code

The mysql file is a directory file. The owner and owner group are both mysql. The owner has read, write, and execute permissions. Other users in the same group as the owner have readable and executable permissions. Other users also have readable and executable permissions.

Changing file properties

1: chgrp (change group)

CHGRP allows ordinary users to change the group to which a file belongs, as long as the user is a member of that group. Otherwise, the permission is root.

Format: CHGRP [-r] Indicates the name of the genus group

  • -R: Changes the file ownership group recursively. When changing the ownership group of a file in a directory, if -r is added, the ownership group of all files in the directory will be changed.
# change the group of index.js file to baidu CHGRP baidu index.jsCopy the code

2: chown

Chown: Changes the file owner or the file owner group.

Chown [-r] Indicates the name of the owner name

Chown [-r] Owner name: indicates the name of the owner group

chown baidu index.js
Copy the code

3: chmod

Linux file attributes can be set in two ways: numeric and symbolic.

Linux files have nine basic permissions: Owner, group, others(owner, group, and others) each has its own read, write, and execute permissions.

Use numbers to represent permissions:
  • r4:
  • w: 2
  • x: 1.
  • -: 0

The three permissions (r/w/x) for each identity are added, for example, -rwx RWX — the score is:

owner = rwx = 4+2+1 = 7

group = rwx = 4+2+1 = 7

others= --- = 0+0+0 = 0

Syntax: chmod [-r] xyz file or directory

chmod 777 .bashrc
Copy the code
Symbol types change file permissions
  • u : user: the user
  • g: groupGroup:
  • o: others: other

chmod u=rwx,g=rx,o=r  test1    // Modify test1 permissions

chomd +x index.js

chmod -r log.txt

chmod -R +wrx web
Copy the code