preface

This article mainly describes in Linux, and file related permissions management, including how to view the permissions of files, and modify the file permissions of several common commands.

Check the permissions

The simplest command to check permissions is ls -l. Some systems also add an alias for LL.

Example:

#View permissions on all files in the current directory
ls -l
total 24
-rw-r--r--  1 yangan  staff   522 Jan 10 11:28 app.log
drwxr-xr-x  2 yangan  staff    64 Jan 10 11:52 test

#View specific file permissions
ls -l /etc/php.ini           
-r--r--r--  1 root  wheel  71890 Nov 30 20:36 /etc/php.ini
Copy the code

The output above can be broken down into six parts. The first part is -rw-r–r–, which represents the file type and its permissions (more on that later). The second part is the total number of file directories, 1 if files, 2 if empty folders (i.e. And..) ; The third part is the owning user, the fourth part is the owning user group, the fifth part is the size of the file/directory (in bytes), and the sixth part is the modification time of the file.

Permission to illustrate

As you can see above, each file/folder is preceded by ten characters, with the first digit indicating the type of file/folder, d for directory, L for linked file, – for normal file, p for pipe, etc.

The following nine characters are groups of three, representing the permissions of the file owner (user), the group to which the file owner belongs, and other users (other).

There are three permissions: Read, write, and execute.

Finally, each permission corresponds to a number, 4 for read, 2 for write, and 1 for execute. The combination has the following situations:

--- 000 0
--x 001 1
-w- 010 2
-wx 011 3
r-- 100 4
r-x 101 5
rw- 110 6
rwx 111 7
Copy the code

So, like the example above, cd-rw – r – r – 1 yangan staff 522 Jan 10. But the app log, its meaning is to have a app. The log file, it belongs to a user has read and write access to its, groups of users and other users only read permission, size is 522 bytes, The modification time is 11:28 on January 10th.

Permission to modify

Common commands :(better to remember commands from the full English name)

  • Chown: change owner Changes the owner
  • CHGRP: change group Changes the owning group
  • Chmod: change mode Changes the permission

The chmod command

The chmod command is used to change permissions. For example, to add an execution permission to app.log, you can perform two operations

#First, 744 corresponds to RWXR --r--
$ chmod 744 app.log

#The second,
#U is the user mentioned above, + indicates the add permission, and x indicates the execute permission
$ chmod u+x app.log 
Copy the code

Chown command

The chown command is used to modify users and user groups, but requires super administrator rights

#Change the user and user group of the app.log file to nobody
$ sudo chown nobody:nobody app.log

#Modify the user and user group of a directory
$ sudo chown -R nobody:nobody test/
Copy the code

The CHGRP command

The CHGRP command is used to modify user groups. The difference between chown and CHGRP is that it does not require super administrator permission. As long as the user is a member of the group, the user can change the group to which a file belongs

$ chgrp -v nobody app.log
chgrp: you are not a member of group nobody
Copy the code