User management of linux basic knowledge

Linux user and user group management


1. Basic user overview

1. What is a user?

1.1 用户指的是能够正常登录Linux或Windows系统(可以理解为你租了房子,能够正常入驻)
1.2 Linux与Windows系统的用户有什么区别? 本质都是登陆系统,只不过Linux支持多个用户同时登陆。
1.3 难道Windows就不算多用户操作系统吗? 其实不是,在Windows系统中可以创建多个用户,但不允许同一时刻多个用户登陆系统,但Linux系统则允许同一时刻多个用户同时登陆,登陆后相互之间操作并不影响

2. What is the use of users under Linux, or why do we create users?

2.1 系统上的每一个进程(运行的程序),都需要一个特定的用户运行
2.2 通常在公司是使用普通用户管理服务器,因为root权限过大,容易造成故障。

3. View the existing users in the system

3.1 [root@jiangshen ~]# id    #查看当前所登陆的用户信息
3.2 [root@jiangshen ~]# id jiangshen #查看其它用户的信息

4. Where do our users exist?

Linux系统会将用户的信息存放在/etc/passwd,记录了用户的信息,但没有密码信息,密码被存放在/etc/shadow中。也就是说这两个文件非常的重要,不要轻易删除与修改。

/etc/passwd:用户的信息库
    name:password:UID:GID:GECOS:directory:shell 
        name:用户名
        password:可以是加密的密码,也可以是站位符x;
        UID:
        GIP:用户所属的主组的ID号
        GECOS:注释信息
        directory:用户的家目录
        shell:用户的默认shell,登录时默认shell程序

/etc/shadow:用户密码
    nginx:!!:18590::::::
    用户名:加密的密码:最近一次修改密码的时间:最短使用期限:最长使用期限:警告期段:禁用期:过期期限:保留字段

5. Finally, we need to understand a convention of the system to users?

User ID The agreed meaning in the system
0 Super administrator, the highest authority, has a strong destructive power
1~200 System user, used to run the process that comes with the system, created by default
201~999 System users, used to run programs installed by users, so such users do not need to log in
1000+ Ordinary users, users of the system that can log in normally, have relatively small permissions, and can perform limited tasks
注意:1、在CentOS7系统之前,UID1-499用于系统登录,而UID500+则用于普通用户
     2、管理员UID为0,普通用户UID为1-65535,用户组也是

2. User related commands

1. Use the useradd command to add users

 选项
 -u 指定要创建用户的UID,不允许冲突
 -g 指定要创建用户默认组
 -G 指定要创建用户附加组,逗号隔开可添加多个附加组
 -d 指定要创建用户家目录
 -s 指定要创建用户的bash shell
 -c 指定要创建用户注释信息
 -M 给创建的用户不创建家目录
 -r 创建系统账户,默认无家目录

1.创建bgx用户,UID5001,基本组students,附加组sa 注释信息:2019 new student,登陆shell:/bin/bash
[root@jiangshen ~]# groupadd sa
[root@jiangshen ~]# groupadd students
[root@jiangshen ~]# useradd -u 5001 -g students -G sa -c "2019 new student" -s /bin/bash bg

2.创建mysql系统用户,-M不建立用户家目录 -s指定nologin使其用户无法登陆系统
[root@jiangshen ~]# useradd mysql -M -s /sbin/nologin
[root@jiangshen ~]# useradd -r dba -s /sbin/nologin

2. How to use usermod command to modify user information

 选项
 -u 指定要修改用户的UID
 -g 指定要修改用户基本组
 -G 指定要修改用户附加组,使用逗号隔开多个附加组, 覆盖原有的附加组
 -d 指定要修改用户家目录
 -s 指定要修改用户的bash shell
 -c 指定要修改用户注释信息
 -l 指定要修改用户的登陆名
 -L 指定要锁定的用户
 -U 指定要解锁的用户

1.修改bgx用户uid、gid,附加组
[root@jiangshen ~]# groupadd -g 5008 network_sa
[root@jiangshen ~]# groupadd -g 5009 devops
[root@jiangshen ~]# usermod -u 6001 -g5008 -a -G 5009 bgx

2.修改bgx用户的注释信息, 用户家目录, 登录shell, 登录名
[root@jiangshen ~]# usermod -c "2019 new student" -md /bgx -s /bin/sh -l change_bgx bgx

3. Use the userdel command to delete the account

#1.删除user1用户,但不删除用户家目录和 mail spool
[root@jiangshen ~]# userdel user1

#2.-r参数可以连同用户家目录一起删除(慎用)
[root@jiangshen ~]# userdel -r root

3. Management of user groups

1. What is a user group?

其实就是一种逻辑层面的定义,逻辑上将多个用户归纳至一个组,当我们对组操作,其实就相当于对组中的所有用户操作

2. For users, how many types of groups are there?

2.1:基本组,用户只能有一个基本组,创建时可通过-g指定,如未指定则创建一个默认的组(与用户同名)
2.2:附加组,基本组不能满足授权要求,创建附加组,将用户加入该组,用户可以属于多个附加组

3. Where is the information of that group stored?

组账户信息保存在/etc/group和/etc/gshadow两个文件中

/etc/group:组的信息库
    group_name:password:GID:user_list
    group_name: 组名
    password:       组密码
    GID:            组ID
    user_list:      显示附加组不显示基本组成员

/etc/gshadow:组密码的信息库
    group_name:password:groupmanage:group_user
    group_name: 组名
    password:       组的密码
    groupmanage:    组管理员
    group_user: 显示附加组成员不显示基本组成员

2. User group related commands

1. Use the groupadd command to add a group, groupadd [-g GID] groupname

#创建基本组, 不指定gid
[root@jiangshen ~]# groupadd no_gid

#创建基本组, 指定gid为5555
[root@jiangshen ~]# groupadd -g 5555 yes_gid

#创建系统组,gid从201-999
[root@jiangshen ~]# groupadd -r sys_group

2. Use the groupmod command to add a new group

#-g 修改组gid
[root@jiangshen ~]# groupmod -g 1111 no_gid

#-n 修改组名称
[root@jiangshen ~]# groupmod -n active_group yes_gid

3. groupdel deletes a group. You need to pay attention when deleting. If the user has a basic group, the group cannot be deleted directly. If the user is deleted, the default private group will be removed, but the basic group will not be removed.

#删除组
[root@jiangshen ~]# groupdel active_group

#删除用户附加组
[root@jiangshen~]# id jiangshen
uid=1069(jiangshen) gid=5005(jiangshen) groups=5005(jiangshen),5004(devops)
[root@jiangshen ~]# groupdel devops
[root@jiangshen ~]# id jiangshen
uid=1069(jiangshen) gid=5005(jiangshen) groups=5005(jiangshen)

#无法删除用户基本组
[root@jiangshen ~]# groupdel network_sa
groupdel: cannot remove the primary group of user 'bgx_jiangshen'
#只有删除用户或者用户变更基本后,方可删除该组

Guess you like

Origin blog.51cto.com/15048360/2561959