Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use chmod permission to modify commands in Linux

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Linux how to use chmod authority to modify commands, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Linux permission

To better understand how chmod commands work, we should carefully study the Linux file permissions model.

In Linux, we have three types of file permissions: read (r), write (w), and execute (x) permissions. These permissions determine which users can read, write, or execute files. You can assign these permissions using text or octal (numeric) notation, which we will discuss later in this tutorial.

From: https://www.linuxmi.com/linux-chmod.html

Files and directories can belong to the owner of a file (u), group (g), or other (o)

U-everyone's permissions

G-permissions for all groups

O-the authority of others

Use the ls-l command to view the detailed properties of all visible files in the current directory in a long format. The-l flag lists the permissions for the file. Permissions are divided into three groups: user (user) group (group) and others (others).

To better understand file permissions, we will list the contents of the directory as follows:

Linuxmi@linuxmi:~/www.linuxmi.com$ ls-l

Starting at the far left, the first character / symbol represents the file type. A hyphen (-) indicates that the file is a normal file. The symbol d indicates that it is a directory. The symbol l indicates that it is a symbolic link.

The remaining nine characters are divided into three triples, each with three symbols r (read), w (write) and x (execute). As mentioned earlier, the first part points to owner permissions, the second part indicates group permissions, and the last part specifies the permissions that other users have on the file or directory.

From the output, we can see that we have 4 files and 2 directories.

Let's look at the first file.

-rw-rw-r-- 1 linuxmi linuxmi 1087 October 3 20:23 linuxmi.com.cpp

"for the first file, the-rw-rw-r- permission indicates that the owner of the file has read and write permissions, the group also has read and write permissions, while other users have read permissions only."

Take a look at the permissions of the directory:

Drwxrwxr-x 3 linuxmi linuxmi 4096 October 12 13:31 Linux fans

We can see that the owners of directories and groups have all permissions (read, write, and execute), while other users only have read and execute permissions.

The triple hyphen-indicates that no permissions have been granted to the owner of the file, team or other user.

Use the chmod command to set file and directory permissions

After looking at the file permissions and how to view them, we no longer focus on how to modify them.

The chmod command in Linux is used to change file and directory permissions using text (symbol) or number (octal) notation. It uses the following syntax:

$chmod [option] schema file name

Only root users or ordinary users with sudo privileges can change file or directory permissions. You can pass multiple files or directories in the command syntax, separated by spaces.

Now let's delve into and look at different examples of the chmod command.

Example 1) assign permissions using numeric symbols

When setting permissions using numeric styles / symbols, use the following syntax:

$sudo chmod [option] numeric value file name

The value can be 3 or 4 digits. However, in most cases, three numbers are used. Read, write, and execute permissions take the following values:

Read permission = > 4

Write permission = > 2

Execute permission = > 1

The sum of permission values, that is, reads, writes, and execution in each of the three segments, accounts for the full permissions of a given file or directory.

What does this mean?

Assuming that the owner has read, write, and execute permissions, the group has read and execute permissions, while other users only have read permissions, they can be subdivided as follows:

U:rwx = 7

G:rx = 5

ORV r-= 4

Therefore, we get a value of 754 as the value of the file permissions for a given file.

To assign read, write, and execute permissions to the owner, and only read permissions to groups and other users, run the following command:

$chmod 744 www.linuxmi.com.txt

To assign all permissions to the owner of the file, read and execute permissions for the group, but no permissions at all for other users, execute:

$chmod 750 www.linuxmi.com.txt

To assign all permissions to the owner of the file, have read and write access to the group, and other users run the following command:

$chmod 755 linuxmi.txt

Example 2) Recursively assign directory permissions

When assigning permissions to a directory, use the-R flag to recursively assign permissions to its directories and subfolders. For example:

$chmod 755-R directory name

$chmod 755-R / home/linuxmi/linux

Example 3) specify permissions using text notation

Another way to assign permissions is to use text notation. In this method, the chmod command takes flags or symbols that represent the owner, group, other or all users in the syntax (uLog g and o).

This method is not as simple and straightforward as the previous method, and care should be taken to avoid assigning the wrong permissions.

This is what grammar looks like.

$chmod [options] [ugoa] [- + =] [rreco wjol x] file

Let's break it down.

The first set of parameters [ugoa] specifies the category of users whose permissions will be changed.

U: user

G: group

O: other

A: all (including all of the above)

If this collection is omitted, the default option is the an option.

The second set of options-operator option [- + =]-determines whether you want to add permissions or remove permissions from the user category. Option deletion

-: this flag removes file permissions from the specified user.

+: add / add permissions to the specified user.

=: assign different permissions to the specified user and delete the previous permissions for that user segment.

Example 4) assign read permissions to the file

$chmod o = r file name

The above command only assigns read permissions to the file for other users represented by the symbol "o" and removes permissions previously assigned to the 'others' section. Get a linuxmi.txt with the following permissions

Linuxmi@linuxmi:~/www.linuxmi.com$ ls-l linuxmi.txt-rwxrwxr-x 1 linuxmi linuxmi 1087 October 3 20:23 linuxmi.txt

To assign read permissions to the "others (o)" section, please only run

Password for linuxmi@linuxmi:~/www.linuxmi.com$ sudo chmod otakr linuxmi.txt [sudo] linuxmi: linuxmi@linuxmi:~/www.linuxmi.com$ ls-l linuxmi.txt-rwxrwxr-- 1 linuxmi linuxmi 1087 October 3 20:23 linuxmi.txt

From the output, we can see that the read permission has been assigned to the "others" segment, while the execution permission has been lost. This means that the = operator assigns new permissions while removing the previous permissions.

Example 5) assign execution permissions to file and group owners

Linuxmi@linuxmi:~/www.linuxmi.com$ sudo chmod ug+x linuxmi.txt linuxmi@linuxmi:~/www.linuxmi.com$ ls-l linuxmi.txt-rwxrwxr-- 1 linuxmi linuxmi 1087 October 3 20:23 linuxmi.txt

The above command adds execution permissions to the owner and group of the file. Using our file, it will become:

Example 6) assign different permissions to files, groups, and others

Sudo chmod uprirwx filename gendrwm obsolete

The above command assigns all permissions to the owner of the file, assigns read and write permissions to groups, and grants read permissions only to other users.

Note: if no permissions are specified after the = operator, all permissions in that user segment are deleted.

Example 7) remove all permissions from other users

$sudo chmod o = filename sudo chmod o = linuxmi.txt $sudo chmod o-rwx linuxmi.txt

The above command removes all permissions for the specified file from the other user section.

This command has the same effect as the following command:

$sudo chmod o-rwx filename linuxmi@linuxmi:~/www.linuxmi.com$ sudo chmod o = linuxmi.txt linuxmi@linuxmi:~/www.linuxmi.com$ ls-l linuxmi.txt-rwxrw---- 1 linuxmi linuxmi 1087 October 3 20:23 linuxmi.txt linuxmi@linuxmi:~/www.linuxmi.com$ sudo chmod o-rwx linuxmi.txt linuxmi@linuxmi:~/www.linuxmi.com$ ls-l linuxmi.txt-rwxrw---- 1 linuxmi linuxmi 1087 October 3 20:23 linuxmi.txt

Example 8) use template files to assign permissions

Another convenient way to assign file permissions is to use reference files. In this method, you use the-- reference= option to set the permissions of the file to be the same as those of another reference file. Use the following syntax

$sudo chmod-reference=ref_file filename

For example, to set the file permissions of linuxmi.txt to the same permissions as www.linuxmi.com.py, run the following command:

$sudo chmod-reference=www.linuxmi.com.py linuxmi.txt

The template file is www.linuxmi.com.py, and change linuxmi.txt to the same permissions as the template file.

Example 9) assign execution permissions only to directories

Suppose we have a directory that contains files and subdirectories, and we want to assign execution permissions only to the directory without touching the files. Therefore, this can be achieved by using the following chmod command

$chmod axix *

If you have noticed that we have used uppercase X as the execution permission, the above command will set the execution permission on all directories in the current working directory.

After reading the above, do you know how to use chmod permissions to modify commands in Linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report