Linux user concept and its authority management

 What is a user? What is a group? Why is there a user concept for our system? We also said that modern operating systems are multi-tasking and multi-user systems called Multi-task, Multi-Users. So before the appearance of PC compatible machines, many used hosts. At that time, many terminals can be used to link to this host. As a result, many people use the same computer together. Create files on the same system and manage file usage files. But we should know that resources are limited. If I find that the disk space is not enough, I secretly use other people's space. So in order to avoid this situation. We must have a mechanism to protect everyone's files from damage. So we need to let everyone who uses a computer. Its rights and interests will not be harmed. We must establish a mechanism for resource isolation. In order to achieve isolation, we need to identify each user, so each user is a user.

 Our group here is a user group. What does a user group refer to? To put it bluntly, a user group is a container for users, which can merge multiple users into a logical concept. Form a logical component, a single logical component. What is the use of this logical component? For example, when we want to authorize multiple users to access a certain file in the future, it is troublesome if we implement it based on the user level, because there can only be one input for a file. I now want to let multiple users have the same permissions on the same file. Note that not all users are just a certain part of users, so we use a container to classify multiple users and let a file have a group. So we authorize the permissions of the group on the file, thereby adding several users to this group, so that multiple users have the same access permissions to this file. This management mechanism makes it easier for us to authorize and revoke permissions or revoke permissions. This is the meaning of the existence of user groups. So we say that it is a user container to facilitate the implementation of permissions. In fact, in some systems, groups are also called roles. Although the two are not completely equivalent, sometimes groups can be called roles. It can realize that a user can be added to a group later.

 User management commands:
useradd command: create a user or update the default new user information

    useradd  [选项] 登录名

    我们创建完docker以后后面的字符会自动有的,ID号1004,组ID1005.我们创建一个用户时,如果我们没有给他指定组的时候,会自动创建用户同名的组作为用户的私有组。   私有组一般跟用户名同名一般只包含一个用户。

    但是我们说过用户是有很多属性的,家目录、UID、GID等等,我们都可以自行指定。

    -u, --uid UID: 指定UID;
    -g, --gid GROUP: 指定基本组ID,此组得事先存在,否则创可能出现错误。
    -G,--groups GROUP1[,GROUP2,...[,GROUPN]]]:指明用户所属的附加组,多个
        组之间用逗号分隔。
    -c,--comment COMMENT:指明注释信息。
    -d, --home-dir HOME_DIR:创建以指定的路径为用户的家目录:而他的创建方式
        是通过复制/etc/skel此目录并重命名实现的。指定的家目录路径如果事先存在
        则不会为用户复制环境配置文件。

        因此如果说指明的目标目录存在话表示,你所指明的路径事先已经存在了,那这样会报错的,报错但是并不会阻止你。命令照样能执行成功,但用户的文件就没了。

        每一个用户的家目录的文件下都有隐藏文件,这些文件从哪里来的呢?这里要注意为了要实现用户环境初始化,我们系统一般的这个家目录的创建不是直接创建一个空目录,而是复制一个目录并更名为用户所指明的家目录名。或者创建空目录并复制其他文件来实现的。

    -s, --shell SHELL:
        指定用户的默认shell,可用的所有shell列表存储在/etc/shells文件中

        指定用户的默认shell,这个shell不会保存在/etc/passwd文件中,到底有
        哪些shell可以用我们不是可以随意指定的。我们这里面所建议使用的shell
        列表为/etc/shells文件中。

        通过cat /etc/shells,看到的这些shell我们称为安全shell,所以我们期望
        用户能登录时我们应该在此处我们要指定shell时要指定列表中的shell。

    -r, --system:创建系统用户

    注意:创建用户时的诸多默认设定配置文件为/etc/login.defs

    useradd -D: 显示创建用户的默认配置
    useradd -D: 选项:修改默认选项的值

        修改的结果保存于/etc/default/useradd文件中

usermod命令:修改用户属性

    usermod [options] LOGIN

        -u, --uid UID:修改用户的ID为此处指定的新UID。
        -g, --gid GROUP:修改用户所属的基本组。
        -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]:修改用户所属的附加组
            原来的附加组会被覆盖。
        -a, --append:与-G一同使用,用于为用户追加新的附加组
        -c, --comment COMMENT:修改注释信息
        -d, --home HOME_DIR:修改用户的家目录:用户原有的文件不会被转移至新
                            位置.如果想转移一同使用-m选项将用户的主目录移动到新位置它只有和-d一起使用才有效

            但是这个命令会一样有问题,如果这个用户本来就有家目录,而在家目录中创建了很多文件了怎么办?你这样一修改原来的文件怎么样了呢?就会没了。所以为了避免出现这种问题-d要跟另外一个选项使用-m

        -m, --move-home:只能与-d选项一同使用,用户将原来的家目录移动为新的家
            目录;

        -l,--login NEW_LOGIN:修改用户名
        -s,--shell SHELL:修改用户默认shell    

        -L,--lock:锁定用户密码,即禁止用户登录了。即在用户原来的密码字符串之          前添加了“!”
        -U,--unlock:解锁用户密码

    我们将来用的比较多的是usermod在修改用户时,将用户添加到新附加组当中去。这个用的比较多,需要和-a一起使用。

userdel command: delete user

 userdel [options] LOGIN

    -r:删除用户时一并删除其家目录        

    权限管理命令:

chmod命令:改变文件权限  

    chmod [OPTION]... MODE[,MODE]... FILE... 
    chmod [OPTION]... OCTAL-MODE FILE...
    chmod [OPTION]... --reference=RFILE FILE...

    三类用户:
        u:属主
        g:属组
        o:其他
        a:所有

    (1)chmod [OPTION]... MODE[,MODE]... FILE... 

        MODE表示法:
            赋权表示法:直接操作一类用户的所有权限位
                u=
                g=
                o=
                a=

            授权表示法:直接操作一类用户的一个权限位r,w,x
                u+,u-
                g+,g-
                o+,o-
                a+,a-   

    (2)chmod [OPTION]... OCTAL-MODE FILE... 八进制赋权           
            chmod 660 文件名   

    (3)chmod [OPTION]... --reference=RFILE FILE... 引用形修改

        chmod --reference=/var/log/messages fstab
        指名一个参考文件来修改文件权限

    选项:
        -R, --recursive:递归修改。通常在授权表示法用的比较多,赋权和八进制一
                        般不用。    修改目录和目录下的文件一起递归修改   

    注意:用户仅能修改属主为自己的哪些文件的权限                  

Subordination management commands: chown to modify the owner, chgrp to modify the group

    chown命令:
        chown [OPTION]... [OWNER][:[GROUP]] FILE...
        chown [OPTION]... --reference=RFILE FILE...

        选项:
            -R:递归修改。我们修改一个目录及内部所有的文件属主为同一个用户还是
                很常见的。   

    chgrp命令:

        chgrp [OPTION]... GROUP FILE...
        chgrp [OPTION]... --reference=RFILE FILE...

    注意:仅管理员可修改文件的属主和属组

Guess you like

Origin blog.51cto.com/14743944/2540315