Detailed explanation of permission management commands in Linux (chmod/chown/chgrp/unmask)

Detailed explanation of permission management commands in Linux (chmod/chown/chgrp/unmask)

The management of multiple users in the Linux operating system is very cumbersome, so it becomes simple to manage users using the concept of groups. Each user can be in an independent group, and each group can have zero or multiple users. This article introduces the detailed explanation of the permission management commands in Linux (chmod/chown/chgrp/unmask). The specific contents are as follows:

chmod

explain

Command name: chmod Command English meaning: change the permissions mode of a file Command path: /bin/chmod Execution permission: all users Function description: change file or directory permissions

grammar

chmod [{ugoa}{+-=}{rwx}] [file or directory] 
chmod [mode=421] [file or directory]
 -R recursive modification # The first modification method chmod [{ugoa}{+-=}{rwx}] [file or directory]
ugoa:
 u: Owner g: Group o: Others a: Everyone +-=:
 +: Add a permission to a file or directory -: Reduce a permission to a file or directory =: Give a file or directory new permissions, based on the current permissions # The second modification method chmod [mode=421] [file or directory]
rwx:
 r:4
 w:2
 x:1
rwxrw-r--
 Permission: 764 (4+2+1=7/4+2=6/4)

Example

# The first way to increase permissions chmod g+x test.txt
 
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rw-r--r-- 1 root root 11 Nov 28 15:39 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod g+x test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt

# The second way to increase permissions: chmod 777 test.txt

[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rwxrwxrwx 1 root root 11 Nov 28 15:39 test.txt

Special attention to permissions

# Create a new folder test under /tmp
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# mkdir test

# Create a new test.txt in the /tmp/test folder
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# touch test/test.txt

# View the files under the test file [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test
total 0
-rw-r--r-- 1 root root 0 Nov 28 17:54 test.txt

# Check the permissions of the /tmp/test folder [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld test
drwxr-xr-x 2 root root 4096 Nov 28 17:54 test

# Grant full permissions to the /tmp/test folder [root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld test
drwxrwxrwx 2 root root 4096 Nov 28 17:54 test

[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test/test.txt
-rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt

# Add a new ordinary user and change the password [root@izm5e2q95pbpe1hh0kkwoiz tmp]# useradd eternity
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# passwd eternity

# Use the eternity account and password 123456 to log in to the server # View the current directory [eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ pwd
/home/eternity

# Enter the /tmp directory [eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ cd /tmp

# Check the permissions of the /tmp/test directory, which has all permissions [eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -ld test
drwxrwxrwx 2 root root 4096 Nov 28 17:54 test

# test.txt exists in the /tmp/test directory and has read permission [eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txt
-rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt

# Delete the test.txt file under /tmp/test [eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ rm test/test.txt
rm: remove write-protected regular empty file 'test/test.txt'? y

# Deletion is successful. At this time, test.txt no longer exists in the /tmp/test directory [eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txt
ls: cannot access test/test.txt: No such file or directory

Only the administrator has rw read and write permissions, and the group and others have only read permissions. However, at this time, ordinary users deleted the file with only r read permissions. Why???? Summary of file directory permissions

Representative characters Permissions Implications for the file Implications for Directories
r Read permission You can view the file contents Can list the contents of a directory
w Write permissions Can modify file contents Can create and delete files in the directory
x Execute permissions Executable file Can enter the directory

analyze

Having write permission for a file only means that the content of the file can be modified, but not the permission to delete the file.

Have write permission for the directory and can create and delete files in the directory

Because the permissions of the /tmp/test directory above are 777, ordinary users also have the permissions to create and delete files in the /tmp/test directory. Therefore, ordinary users can also delete the /tmp/test/test.txt file. However, ordinary users cannot edit the /tmp/test/test.txt file. When using vim to edit the file, it will prompt Waring: Changing a readonly file

chown

explain

Command name: chown Command English meaning: change file ownership Command path: /bin/chown Execution permission: all users Function description: change the owner of a file or directory

grammar

chown [user] [file or directory]

In Linux, only root can change the file owner, not even the creator.

Example

# Change the file owner (change the owner of test.txt from eternity to root)
chown root /tmp/test/test.txt

[root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd
/root
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 eternity eternity 7 Nov 28 18:15 /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# chown root /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txt

chgrp

explain

Command name: chgrp
The original meaning of the command in English: change file group ownership
Command path:/bin/chgrp
Execution permission: All users Function description: Change the group to which a file or directory belongs

grammar

chgrp [user group] [file or directory]

Example

# Change the group to which the file belongs (change the group to which test.txt belongs from eternity to eternityz)
chgrp eternityz /tmp/test/test.txt

# Current directory [root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd
/root
# View detailed information [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txt
# Add eternityz group [root@izm5e2q95pbpe1hh0kkwoiz ~]# groupadd eternityz
# Change the group [root@izm5e2q95pbpe1hh0kkwoiz ~]# chgrp eternityz /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 root eternityz 7 Nov 28 18:15 /tmp/test/test.txt

umask

explain

Command name: umask Command English original meaning the user file-creation mask Command path: shell built-in command Execution permission: all users Function description: display/set the default permissions of the file

grammar

umask [-S] -S Displays the default permissions for newly created files in the form of rwx (uppercase S)

Example

# View the default permissions of the file umask -S

# Check umask
umask

[root@izm5e2q95pbpe1hh0kkwoiz ~]# umask
0022

0022 in 0 Special permissions 022 ----w--w-

# Perform an XOR operation on all permissions 777 and 022 to get the default permission 777 rwx rwx rwx
022 --- -w- -w-
================
Directory rwx rx rx
File rwx r-- r--


# Change the umask value to change the default permission umask 077

# After changing the umask value, the default permissions become 777 rwx rwx rwx
077 --- rwx rwx
================
Directory rwx --- ---
File rw- --- ---

# The following experiment complies with the setting of changing the default permissions [root@izm5e2q95pbpe1hh0kkwoiz ~]# umask 077
[root@izm5e2q95pbpe1hh0kkwoiz ~]# mkdir /tmp/lyf
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -ld /tmp/lyf
drwx------ 2 root root 4096 Nov 29 10:55 /tmp/lyf
[root@izm5e2q95pbpe1hh0kkwoiz ~]# touch /tmp/lyf/lyf
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/lyf/lyf
-rw------ 1 root root 0 Nov 29 10:56 /tmp/lyf/lyf

In Linux, only root can change the file owner. Even the creator cannot set the file creator as the default owner. In this case, the default group is also the file creator. The default permissions for folders in Linux are rwxr-xr-x, and the default permissions for files are rw-r--r--. New files do not have executable permissions.

This is the end of this article about the detailed explanation of permission management commands in Linux (chmod/chown/chgrp/unmask). For more relevant Linux permission management command content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Linux common commands chmod to modify file permissions 777 and 754
  • Detailed Analysis of the chmod Command to Change File Permissions in Linux
  • Modify Linux file permissions command: chmod command detailed explanation
  • Detailed analysis of the chmod command to modify file permissions under Linux

<<:  Vue Router loads different components according to background data

>>:  Solution to MySQL server login error ERROR 1820 (HY000)

Recommend

Complete steps of centos cloning linux virtual machine sharing

Preface When a Linux is fully set up, you can use...

About the problems of congruence and inequality, equality and inequality in JS

Table of contents Congruent and Incongruent congr...

MySQL 8.0.20 installation tutorial and detailed tutorial on installation issues

Original address: https://blog.csdn.net/m0_465798...

Install JDK1.8 in Linux environment

Table of contents 1. Installation Environment 2. ...

How to import Tomcat source code into idea

Table of contents 1. Download the tomcat code 2. ...

Summary of MySQL log related knowledge

Table of contents SQL execution order bin log Wha...

Mysql join query syntax and examples

Connection query: It is the result of connecting ...

MySQL users and permissions and examples of how to crack the root password

MySQL Users and Privileges In MySQL, there is a d...

Summary of commonly used performance test scripts for VPS servers

Here is a common one-click performance test scrip...

How to run tomcat source code in maven mode

Preface Recently, I was analyzing the startup pro...

Summary of commonly used CSS encapsulation methods

1. pc-reset PC style initialization /* normalize....

Detailed explanation of how to configure openGauss database in docker

For Windows User Using openGauss in Docker Pull t...

The implementation of Youda's new petite-vue

Table of contents Preface Introduction Live Easy ...