Detailed analysis of the chmod command to modify file permissions under Linux

Detailed analysis of the chmod command to modify file permissions under Linux

Use the Linux chmod command to control who can access, read, write, or run the target file .

In Linux, who can do what with a file or directory is controlled through a set of permissions.

Permissions control the operations (read, write, or execute) that can be performed on a file or directory.

We can use the -l (long format) option to ls to list the file permissions for files and directories.

ls -l 

In each line, the first character identifies the type of entry listed. If it is a dash ( - ), it is a file. If it is the letter d , it is a directory.

The next nine characters represent three sets of permission settings.

The first three characters show the permissions of the user who owns the file ( User Rights ).

The middle three characters show the permissions of the user group membership ( group permissions ).

The last three characters show permissions for anyone outside the first two categories ( other permissions ).

There are three characters in each set of permissions. The character is an indicator of whether the permission is present or not. They are either a dash ( - ) or a letter. If the character is a dash, it means that permission is not granted. If the character is an r , w , or x , the permission is granted.

The letters represent:

r : Read permission. You can open the file and view its contents.

w : write permission. This file can be edited, modified and deleted.

x : Execute permission. If the file is a script or program, it can be run (executed).

For example:

--- means no permissions are granted at all.

rwx means full permissions are granted. Read, write, and execute indicators are present.

Syntax for setting permissions

To set permissions with chmod , we need to tell it:

who : For whom we are setting permissions.

what : What changes are we making? Are we adding or removing permissions?

which: Which permissions did we set?

We use indices to represent these values ​​and form short "permission statements" such as u+x , where "u" stands for "current user" (who), "+" stands for addition ( what ), and "x" stands for execute permission (which).

The "who" values ​​we can use are:

u : user, indicating the owner of the file.

g : Group, indicating the members of the group to which the file belongs.

o : Others, meaning people who are not managed by u and g permissions.

a : All, meaning all of the above.

The values ​​we can use for " what " include:

: Minus sign. Remove permissions.

+ : Plus sign. Grant permissions. The permission is added to the existing permissions. If you want to have this permission and only set this permission, use the = option as described below.

= : equal sign. Set permissions and remove others.

The "which" values ​​we can use are:

r : Read permission.

w : write permission.

x : Execute permission.

Examples:

We can add execute permissions for everyone with the following command:

chmod a+x new_script.sh 

We want user dave to have read and write permissions, while group and other users have only read permissions. We can use the following command:

chmod u=rw,og=r new_file.txt 

Using the "=" operator means we clear all existing permissions and then set the specified permissions.

This is the end of this article about the detailed analysis of the chmod command to modify file permissions under Linux. For more relevant Linux chmod 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:
  • Modify file permissions (ownership) under Linux
  • Linux common commands chmod to modify file permissions 777 and 754
  • Linux server programming utime() function to modify file access time
  • How to modify the user and group of a file in Linux

<<:  Example code for implementing beautiful clock animation effects with CSS

>>:  Detailed explanation of html printing related operations and implementation

Recommend

Detailed explanation of the use of stat function and stat command in Linux

stat function and stat command Explanation of [in...

Common HTML tag writing errors

We better start paying attention, because HTML Po...

HTML Tutorial: Collection of commonly used HTML tags (5)

Related articles: Beginners learn some HTML tags ...

Detailed explanation of Mysql transaction processing

1. MySQL transaction concept MySQL transactions a...

React implements a highly adaptive virtual list

Table of contents Before transformation: After tr...

How to use Linux whatis command

01. Command Overview The whatis command searches ...

Docker Swarm from deployment to basic operations

About Docker Swarm Docker Swarm consists of two p...

Example code for implementing div concave corner style with css

In normal development, we usually use convex roun...

Solve the problem of using less in Vue

1. Install less dependency: npm install less less...

15 JavaScript functions worth collecting

Table of contents 1. Reverse the numbers 2. Get t...

What to do if the container started by docker run hangs and loses data

Scenario Description In a certain system, the fun...

Use docker to build kong cluster operation

It is very simple to build a kong cluster under t...

How to convert extra text into ellipsis in HTML

If you want to display extra text as ellipsis in ...

Several skills you must know when making web pages

1. z-index is invalid in IE6. In CSS, the z-index...