How to modify file attributes and permissions in linux

王林
Release: 2019-12-06 10:04:34
Original
3855 people have browsed it

How to modify file attributes and permissions in linux

We know the importance of file permissions to the security of a system, and we also know the relevance of file permissions to users and groups. So how to modify the attributes and permissions of a file?

Here we introduce several commands commonly used for permissions of groups, owners, and various identities. As shown below:

chgrp : Change the group to which the file belongs;

chown : Change the file owner;

chmod: Change file permissions, SUID, SGID, SBIT and other characteristics.

1. Change the group chgrp

[root@www ~]# chgrp [-R] dirname/filename ...
Copy after login

options and parameters:

-R: Perform recursive and continuous changes, that is, All files and directories under the subdirectory are updated to represent this group. Often used to change all files in a directory.

Example:

[root@www ~]# chgrp users install.log 
[root@www ~]# ls -l 
-rw-r--r-- 1 root users 68495 Jun 25 08:53 install.log 
[root@www ~]# chgrp testing install.log 
chgrp: invalid group name `testing&#39; <== 发生错误信息息~找不到这个群组名~
Copy after login

Free video tutorial recommendation:linux video tutorial

2. Change the file owner chown

[root@www ~]# chown [-R] 账号名称 档案或目录 
[root@www ~]# chown [-R] 账号名称:组名 档案或目录
Copy after login

Options and parameters:

-R: Perform recursive and continuous changes, that is, all files in the subdirectory are also changed.

Example: Change the owner of install.log to the bin account:

[root@www ~]# chown bin install.log 
[root@www ~]# ls -l 
-rw-r--r-- 1 bin users 68495 Jun 25 08:53 install.log
Copy after login

Example: Change the owner and group of install.log back to root:

[root@www ~]# chown root:root install.log 
[root@www ~]# ls -l 
-rw-r--r-- 1 root root 68495 Jun 25 08:53 install.log
Copy after login

3. Change permissions chmod

There are two ways to set permissions. You can use numbers or symbols to change permissions.

3.1 Change file permissions by numeric type

There are nine basic permissions for Linux files, namely owner/group/others, each of which has its own read/ write/execute permission.

Example: The permission character of the file is -rwxrwxrwx. These nine permissions are in groups of three! Among them, we can use numbers to represent each permission. The score comparison table for each permission is as follows:

r:4; w:2; x:1

Each identity (owner/group/ The three permissions (r/w/x) scores of others need to be accumulated. For example, when the permission is: [-rwxrwx---] the score is:

owner = rwx = 4+2+1 = 7
group = rwx = 4+2+1 = 7
others= --- = 0+0+0 = 0
Copy after login

So we set the change of permissions At this time, the permission number of the file is 770! The syntax of the chmod command to change permissions is as follows:

[root@www ~]# chmod [-R] xyz 档案或目录
Copy after login

Options and parameters:

xyz: It is the numeric type permission attribute just mentioned, which is the addition of the rwx attribute value.

-R: Perform recursive and continuous changes, that is, all files in the subdirectory will be changed.

For example, if you want to set and enable all permissions of the .bashrc file, then issue:

[root@www ~]# ls -al .bashrc 
-rw-r--r-- 1 root root 395 Jul 4 11:45 .bashrc 
[root@www ~]# chmod 777 .bashrc 
[root@www ~]# ls -al .bashrc 
-rwxrwxrwx 1 root root 395 Jul 4 11:45 .bashrc
Copy after login

Then if you want to change the permissions to -rwxr-xr -- Woolen cloth? Then the permission score becomes [4 2 1][4 0 1][4 0 0]=754! So you need to issue:

[root@www ~]# chmod 754 filename
Copy after login

3.2 Symbol type to change file permissions

There is also a way to change permissions! From the previous introduction, we can find that there are basically nine permissions, namely (1) user (2) group (3) others! Then we can use u, g, o to represent the permissions of the three identities! In addition, a represents all, that is, all identities! Then the read and write permissions can be written as r, w, x! That is to say, you can use the following method to look at it:

How to modify file attributes and permissions in linux

Let’s experiment! If we want to set the permissions of a file to be "-rwxr-xr-x", it is basically:

o user (u):具有可读、可写、可执行的权限;
o group 与 others (g/o):具有可读不执行的权限。
Copy after login

So it is:

[root@www ~]# chmod u=rwx,go=rx .bashrc
Copy after login

Pay attention! That u=rwx,go=rx are connected together without any spaces in between!

[root@www ~]# ls -al .bashrc 
-rwxr-xr-x 1 root root 395 Jul 4 11:45 .bashrc
Copy after login

So what if it is a permission like "-rwxr-xr--"? You can use "chmod u=rwx,g=rx,o=r filename" to set. In addition, if we know the original file attributes, and I just want to increase the permissions that everyone can write to the .bashrc file, then I can use:

[root@www ~]# ls -al .bashrc 
-rwxr-xr-x 1 root root 395 Jul 4 11:45 .bashrc 
[root@www ~]# chmod a+w .bashrc 
[root@www ~]# ls -al .bashrc 
-rwxrwxrwx 1 root root 395 Jul 4 11:45 .bashrc
Copy after login

And if I want to remove the permissions Without changing other existing permissions? For example, if you want to remove the executable permissions of everyone, then:

[root@www ~]# chmod a-x .bashrc 
[root@www ~]# ls -al .bashrc 
-rw-rw-rw- 1 root root 395 Jul 4 11:45 .bashrc
Copy after login

Recommended related articles and tutorials: linux tutorial

The above is the detailed content of How to modify file attributes and permissions in linux. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!