Linux centos user group permissions basic operation

Original  https://www.cnblogs.com/dubhlinn/p/11055010.html

 

# Zuo Xudong Example Create a user and specify the home directory and group. If the group exists, use big G?

useradd -g tomcat8 -d /home/tomcat8 -m tomcat8

# java environment variable configuration file is in

cat /etc/profile

# tomcat8 User default home directory file

[root@HLWYXPT tomcato8]# ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2  .mozilla

# Created .profile

PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:$HOME/bin:/usr/bin/X11:/sbin:/usr/java8_64/jre/bin

export PATH

if [ -s "$MAIL" ]           # This is at Shell startup.  In normal
then echo "$MAILMSG"        # operation, the Shell checks
fi                          # periodically.

alias ll='ls -la'
alias tailf='tail -f'
alias tailf2='tail -200f'

Users and groups

1. The concept of users, groups, and home directories

The Linux system supports multiple users. In addition to the administrator, other users should generally not use root, but should apply for an account from the administrator. Groups are similar to roles, and the system can uniformly manage users with similarities through groups. Each user should belong to at least one group and cannot be separated from the group; and when creating a user, the system will automatically generate a directory under the /home directory as the user’s "home directory", and the user will enter the home first after logging in table of Contents. The home directory can be specified when adding a user, if not specified, it will be named after the user name by default. For example, the home directory of user bob is /home/bob by default. After logging in to the linux system, he will first enter /home/bob.

2. Add users

useradd username [-d homePath] [-g groupName]

Among them, username is the user name, homePath is the home directory (optional, if not specified, it defaults to the same user name in the /home directory), groupName is the group name (optional, if not specified, the default is the same user name). For example, to create a new user named zhangsan, the home directory is /home/zhang3, belonging to the sales group, the command is: useradd zhangsan -d /home/zhang3 -g sales.

3. Set or modify user password

passwd username

Among them, username is the user name whose password is to be set or modified. After pressing Enter, the new password will be required to be entered twice.

4. Delete users

userdel [-r] username

Among them, username is the name of the user to be deleted, and -r is optional, which means that the user's home directory will be deleted at the same time. Generally, you do not need to delete his home directory when deleting a user.

5. View user information

id username

Among them, username is the user name to be viewed. After pressing Enter, information such as user id and group id will be displayed.

6. Switch users

su - newUsername

Among them, newUsername is the user name to be switched. If it is switched from a high-privileged user to a low-privileged user, such as switching from root to an ordinary user, no password is required; otherwise, a password is required. After testing, when switching to the user itself, no password is required from root to root, and a password is required from ordinary user to himself.

7. Add group

groupadd groupName

Among them, groupName is the name of the group to be added. Note that you cannot add groups with the same name repeatedly. If the groupName already exists, an error message will be returned.

8. Delete the group

groupdel groupName

Among them, groupName is the name of the group to be added. Note that if this group is the primary group of one or more users, it cannot be deleted.

9. Modify the group to which the user belongs

usermod -g groupname userName

Among them, groupname is the new group name, and userName is the user name to be modified.

10. Modify the user's home directory

usermod -d homepath username

Where homepath is the new home directory, and username is the user name to be modified.

11. Files related to users and groups

/etc/passwd: user configuration file, storing user information; from left to right: user name, password (not shown in plain text), user id, group id, home directory, login shell.

/etc/shadow: Password configuration file, used to store encrypted passwords, modification events, expiration time and other information.

/etc/group: Group configuration file, used to store group name and group id.

12. The relationship between files and users and groups

When a user creates a file, the user automatically becomes the "owner" of the file, and the group the user is in becomes the "group" of the file. In addition to the owner and users in the group, other users in the system are The files are all users in the "other group". You can use the ls -l command to view the owner and group of the file. However, the owner and group of the file are not fixed and can be modified.

13. Modify the owner of a file or directory

chown [-R] username source

Among them, username is the modified user name, source is the path name of the file or directory, and -R represents the owner of the subdirectories and files under the cascading modification directory.

14. Modify the group of a file or directory

chgrp [-R] groupname source

Among them, groupname is the modified group name, source is the path name of the file or directory, and -R means the group of subdirectories and files under the cascading modification directory. Note that the owner and group of the modified file are independent events and do not affect each other.

15. Modify the owner and group of a file or directory at the same time

chown [-R] username:groupname source

Among them, username is the modified user name, groupname is the modified group name, source is the path name of the file or directory, and -R means the group of subdirectories and files under the cascading modification directory. The user and group here are not necessarily related, that is, username does not necessarily belong to the group of groupname.

 

Authority

1. Introduction to file and directory permissions

In the directory and file management section, we learned that the ls -l command can display the subdirectories and files in the current directory in the form of a list. But what information is shown in this list is not detailed in the previous section. Here is an example to illustrate:

There is an animal/ directory under the home directory, and a description.txt file. The list fields are divided by different colors below.

drwxr-xr-x. 3 dubhlinn engineer 47 6月 19 22:26 animal

-rw-r--r--. 1 dubhlinn police 52 6月 19 22:25 description.txt

(1) The first group has only 1 character, indicating the type of entry:

-: ordinary files

d: Directory

l: soft link

c: Character device, such as keyboard and mouse

b: block file, such as hard disk;

(2) The second group has 9 characters, each of which expresses a meaning:

The first 1-3 represent the permissions of the file owner,

Numbers 4-6 represent the permissions of users in the group where the file is located,

Numbers 7-9 represent the permissions of other groups of the file.

So what do these characters mean? In general, r means readable, w means writable, and x means executable. However, for files and directories, the meanings are different, as described below.

  file table of Contents
- No permission No permission
r You can use cat, more, less and other commands to view the contents of the file You can use the ls command to view the contents of the directory
w You can use vi, vim editor to modify the file, but it does not mean that it can be deleted Can add, delete, rename files or subdirectories in the directory
x Executable You can use the cd command to enter the directory

(3) The third group is a number, which is the number of hard links for files, usually 1, and for directories, the number of subdirectories (including hidden subdirectories).

(4) The fourth group represents the owner of the file.

(5) The fifth group represents the group of the file.

(6) The sixth group is a number indicating its size (KB).

(7) The seventh group is the date and time, indicating the last modification time of the file or directory.

(8) The eighth group is the name of the file or directory.

Based on these theoretical knowledge, let’s look at the above example again

drwxr-xr-x. 3 dubhlinn engineer 47 6月 19 22:26 animal

This is a directory named animal; its owner has read, write, and execute permissions, users in its group have read and execute permissions, and other groups have read and execute permissions; there are 3 subdirectories under it; its The owner is dubhlinn, the group is engineer; its size is 47KB, and the last editing time is 22:26 on June 19th.

2. Modify the permissions of a file or directory

(1) Use permission code assignment

chmod role1=auth source

Among them, role is the role code. One or more of them can be set. Multiple roles are separated by commas (,) without spaces. The value range is as follows:

u- the owner of the file or directory;

g-The user of the group where the file or directory is located;

o- other users;

a- All users, including u, g, o, can only be used alone.

auth is a combination of permission codes. For example, the read and write permission is rwx, the read and execute permission is rx, and the write permission is w. Choose according to your needs.

source is the path name of the file or directory.

Here are a few examples:

chmod u=rwx,g=rx,o=rx /home/dubhlinn

chmod o = r /home/dubhlinn/description.txt

chmod a=rwx /home/public

(2) Use plus and minus signs to add or reduce permissions

chmod role+auth source

chmod role-auth source

The meanings of role, auth, and source are the same as above. One or more roles can also be set. Here are a few examples:

chmod u-x,g+rw /home/dubhlinn/description.txt

chmod a+w /home/public

chmod u+rwx,g-x,o-x /home/dubhlinn

(3) Use number assignment

chmod xyz source

Among them, x, y, and z are three numbers, which respectively represent the "authority value" of the owner of the file or directory, the user in the group, and the user in other groups.

The permission value is calculated like this:

r=4

w=2

x=1

Adding the numbers corresponding to this user's authorization code is the "authorization value", so its value range may be 0, 1, 2, 3, 4, 5, 6, 7.

For example, chmod 754 /home/void is equivalent to chmod u=rwx,g=rx,o=r /home/void.

Guess you like

Origin blog.csdn.net/yuezhilangniao/article/details/112895004