Linux command learning series 10 - the use of permissions related chown, chmod, chgrp commands

Review the content of the previous section:

1. Enter the directory: cd command

2. Create a directory: mkdir command

3. Delete a directory: rmdir command

4. File and directory copy: cp

Homework: Create directories test1, test2, create a test1.txt file in test1, and then copy the test1.txt file to the test2 folder

mkdir test1

mkdir test2

cd test1

touch test1.txt

cp test1.txt /test2


Contents of this section:


1. File/ directory details


When you use the ll command to view file/directory details, you can see information similar to the following

image

I used the red box to divide this information into 7 columns, which are explained below

image

First column: file type and permissions

image

There are a total of 10 horizontal lines, the first one represents the file type, commonly used file types:

d: folder

-: Normal file

l: link (windows-like shortcut)

b: block device files (such as hard disks, optical drives, etc.)

p: pipe file

c: character device file (such as serial port devices such as cats)

s: socket file/data interface file (for example, a mysql.sock file will be generated when a MySql server is started)

The last 9 are in groups of 3 to represent the owner of the file, all groups of the file, the permissions of other users, and the possible values ​​of permissions:

r=read attribute //value=4
w=write attribute //value=2
x=execute attribute //value=1

When a position is empty, - is displayed, indicating that this permission is not available.

Second column: the number of connections

All files except directory files have a link count of 1

The number of connections of directory files is the total number of other directories in this directory + 2. For example, if directory a contains directories b and c, the number of links in directory a is 4.

Third column: file owner

Fourth column: file all groups

Fifth column: file size

Column 6: Last modification time

Seventh column: file name


2. Change file permissions: chmod command


Use u, g, o in the permissions to represent the file owner, owning group, and others, respectively. The permissions are represented by rwx, the + sign for adding permissions, the - sign for deleting permissions, and the = sign for specifying permissions.

Chmod u+w filename #indicates adding write permission to the file owner

Chmod uw filename # means to delete the write permission of the file owner

Chmod u=rwx filename # means to set the permissions of the file owner to readable, writable, executable

Chmod u+w,g+w #filename #Indicates that the file owner adds write permission, and all groups add write permission

To set permissions for everyone at the same time, it can be represented by numbers, rwx is represented by 7, r- is represented by 4, -w- is represented by 2, --x is represented by 1.

Chmod 741 filename #Indicates that the owner has read, write and execute permissions, all groups have read permissions, and others have execute permissions.

Taking test.txt as an example, the default permissions are as follows:

image

Now change the permissions, add execute permissions to the owner, add write permissions to all groups and others, execute

chmod u+x,g+w,o+w test.txt

image

Similarly, we can also use numbers to modify permissions. For example, to change the permissions of test.txt to u, g, and o have rwx permissions, you can use chmod 777 test.txt

image

Finally, change the test.txt permission to the original state, chmod u=rw,g=r,o=r test.txt

If you want to change the permissions of the directory and its subfiles, you need to use the -R parameter

Take the test folder as an example, you can see the default permissions:

image

Modify the permissions of the directory to g with w permissions

Chmod –R g+w test

image

image

Other operations are the same as the file, and finally change the permission of test to the default, execute chmod –R gw test

image

image


3. Change the file owner: chown command


Basic usage chown owner: own group filename, or chown -R owner: own group directory name

For example, to modify the owner of the file test.txt to user1, use: chown user1 test.txt

To modify the owning group of the file test.txt to user1, use: chown :user1 test.txt

To modify the owner and the owning group at the same time, use: chown user1:user1 test.txt

To modify the ownership of a directory and its sub-items, you need to use the -R parameter. For example, to modify the owner of the directory test and sub-files to be user1, use: chown –R user1 test


4. Change the file owning group: chgrp command


The basic user is similar to chown, except that this command only modifies all groups of the user. The basic usage is chgrp group name file name, or chgrp -R group name directory


Homework: Create a file test.txt, modify the file permissions to the owner with read, write and execute permissions, all groups have read and write permissions, others have read permissions, change the owner of this file to user1


Free video tutorials follow the WeChat public account "Kicked Xueba" to get

qrcode_for_gh_be6c076f1d26_258

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324721559&siteId=291194637