User and user groups under Linux, file permissions: Chown and chmod

Source: Internet
Author: User
Tags chmod file permissions

As shown, create a new user Masterbai under root privileges, and add some information to the/etc/passwd file

In this file, the user information of the server, such as the red box user, is the user we created, and up to 2-25 of the users, belonging to the system users (in Linux, all services must be established in the case of users can be executed, so these users are called Linux system users).

What do you mean by that? (password encrypted, saved under Etc/shadow file, root permission can be viewed):

user account

user password

user ID

user group ID

username full

user home directory

shell used by user

masterbai

X

1002

1010

 

/home/masterbai

/bin/bash

Here we can see that when we create a new user, we automatically created a user group of the same name Masterbai,id 1010

If you use the-G parameter, specify the user group when creating the user, or use the-n parameter if you do not need to specify the user group at the time of creation. eg

We can use groups to view the user groups to which the current user belongs:

So in turn, I want to know all the users under a certain user group? In Linux, the user information is saved as a/etc/passwd file, and the user group information is saved in the/etc/group file, which lists all user groups, passwords, and GID (user group ID).

Through the relevant GID, in the user information file query: For example, here query gid=1002 all users:

At this point we know that gid=1002, named ' Baihuidong ' under the user group, has three users: Baihuidong,masterbai2,masterbai3

Now that a user group can have more than one user, can a user belong to more than one user group? Is possible:

Currently Masterbai this user, only one user group, gid=1003, name is called Masterbai, we create a test text in the master user directory

You can see that its user rights are Masterbai, and the user group permissions are Masterbai

OK, now let's add another user group to Masterbai, see what happens to all the permissions for this text, and switch to root permissions:

A new user group was added using GROUPADD-G 1009 BaiGroup1

Using Usermod-g Baigroup1,masterbai,baihuidong Masterbai to add three user groups to the ' Masterbai ' user under root privileges. :

Let's take a look at the text we just created with Masterbai permissions, and now what it looks like:

Wow, the user group becomes BaiGroup1, and what does this BaiGroup1 group have to do with Masterbai users? Yes, I guess you've seen it, gid!.

Although this user has more than one user group, the system still has a primary user group, which is the user group for the GID. For each user, there is only one GID, so the file is only relevant to the user's primary user group, and not to the other user groups.

Attention:

Usermod-g group_name user_name

Change a user's primary user group (change GID)

Usermod-g GROUP1,GROUP2,GROUP3 user_name

Add user groups to users, but the primary user group does not change. (The user group that the file belongs to also does not change)

OK, after the basic knowledge of some users, then introduce a more commonly used 2 command brothers: Chown and chmod, file permission control command. Believe it is not strange, these two commands, often used online patch script. Next talk about the Chown command: (chmod back)

These two commands, chown, are the ones that control who has permissions and chmod what permissions to control. Ha ha haha

Let's start with a brief introduction to the files in Linux:

D

Rwxr-xr-x

2

Masterbai

Baihuidong

4096

Jul

13

16:15

TestFolder

File identification

Permissions

Number of hard links to file

File owner

Owning user group

BYTE size

Month

Day

Time

Filename

In the file ID, D represents the directory,-representing the file (other types are uncommon and not known)

The role of the chown is to change the user and the owning user group of the directory or file

I now change the user Masterbai's main user group to Masterbai (GID)

Next we use the Chown command to change the TestFolder directory to user MasterBai2, and the user group to Baihuidong

OK, use Chown user_name:group_name file_dir to make the file's owner and user group change

Let's see if the properties of the test.txt inside the TestFolder are changed:

Wow, not change, in some cases we also let all directories and files under the directory and the user group to change, what to do:

OK, use Chown-r user_name:group_name file_dir to change the owner and user group of the file and all files and directories under the directory.

If I only want to change the user's owner, do not want to change the group, you can also use Chown user_name File_dir do not follow the ": Group_name" to achieve.

Well, Chown is the bird, haha, next will be chmod, talk about chmod before, first of all the file attributes before the big string of rwxr-xr-x is what it means:

It should be easy to see that these properties are made up of 9-bit letters, and Linux divides three of them into three groups:

This picture should be able to explain, OK, now start using the chmod command:

For example, the current Testa this directory permission is 775, I now want to change it to 755, how to operate it?

Switch to root permission execution: chmod 755 TestA

Use the Chmod-r 755 testa to change all the file directory permissions in the Testa directory to 755:

Next the user's Delete and change password:

To delete a user:

Userdel MasterBai4 Delete the user MasterBai4, there is no information in the/etc/passwd, but the user's directory file is still there.
RM-RF MasterBai4 Delete the directory where the user MasterBai4 is located

Change Password: Use the passwd command to change the password

User and user groups under Linux, file permissions: Chown and chmod

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.