• 5.用户和权限管理


    1.系统中用户

    1.1 用户

    Linux中每个用户是通过User Id (UID)来唯一标识的

    • 管理员root:0
    • 系统用户:1-499 (CentOS 6以前), 1-999 (CentOS 7以后) 对守护进程获取资源进行权限分配
    • 登录用户:500+ (CentOS6以前), 1000+(CentOS7以后) 给用户进行交互式登录使用

    1.2用户组

    用户组是通过Group ID(GID) 来唯一标识的。

    • 管理员root:0
    • 系统组:1-499(CentOS 6以前), 1-999(CentOS7以后), 对守护进程获取资源进行权限分 配
    • 普通组:500+(CentOS 6以前), 1000+(CentOS7以后), 给用户使用

    1.3 用户和组的关系

    用户的主要组(primary group):用户必须属于一个且只有一个主组,默认创建用户时会自动创建 和用户名同名的组,做为用户的主要组,由于此组中只有一个用户,又称为私有组

    用户的附加组(supplementary group): 一个用户可以属于零个或多个辅助组,附属组

    2.用户和组的配置文件

    2.1 用户和组的主要配置文件

    • /etc/passwd
    • /etc/shadow
    • /etc/group
    • /etc/gshadow

    2.2 passwd文件格式

    [root@Centos7 data]# cat /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    

    从左至右依次表示用户名、密码、UID、GID、用户全名或信息、用户主目录、用户默认使用shell

    2.3 shadow文件格式

    [root@Centos7 data]# cat /etc/shadow
    user:$6$h1C.cePewZU.s9pw$Nxq5eAgSWeAJ5POU55DBI.awbioC4I12Y1zj9IJpZ2EY4SttRQakRxgN.GUz8wH6BYZbtc9AOT98WtOqW1Z43/::0:99999:7:::
    

    从左至右依次表示

    用户名

    用户密码:一般用sha512加密

    从1970年1月1日起到密码最近一次被更改的时间

    密码再过几天可以被变更(0表示随时可被变更)

    密码再过几天必须被变更(99999表示永不过期)

    密码过期前几天系统提醒用户(默认为一周)

    密码过期几天后帐号会被锁定

    从1970年1月1日算起,多少天后帐号失效

    范例

    #使用tr命令配合/dev/urandom生成随机密码
    [root@Centos7 ~]# tr -dc [:alnum:] < /dev/urandom | head -c 9
    HSSb6TIwv
    [root@Centos7 ~]# openssl rand -base64 9
    VrRy07H0KQK4
    

    2.4 group文件格式

    [root@Centos7 ~]# cat /etc/group
    root:x:0:
    bin:x:1:
    

    群组名称:就是群组名称

    群组密码:通常不需要设定,密码是被记录在 /etc/gshadow

    GID:就是群组的 ID

    以当前组为附加组的用户列表(分隔符为逗号)

    2.5 gshdow文件格式

    [root@Centos7 ~]# cat /etc/gshadow
    root:::
    

    群组名称:就是群的名称

    群组密码:

    组管理员列表:组管理员的列表,更改组密码和成员

    以当前组为附加组的用户列表:多个用户间用逗号分隔

    3 用户和组管理命令

    3.1 useradd

    useradd:创建或更改用户信息

    useradd [options] LOGIN
    

    常用选项

    • -r 创建系统用户 CentOS 6之前: ID<500,CentOS 7以后: ID<1000
    • -s SHELL 指明用户的默认shell程序,可用列表在/etc/shells文件中
    • -c "COMMENT“ 用户的注释信息
    • -u 指定用户的UID
    • -o 配合-u 选项,不检查UID的唯一性
    • -d HOME_DIR 以指定的路径(不存在)为家目录
    • -D display the current default values(显示useradd默认值)

    范例

    #创建apache系统用户
    [root@Centos7 ~]# useradd -r -s /sbin/nologin -d /var/www -c 'apache' apache
    

    useradd 命令默认值设定由/etc/default/useradd定义

    [root@Centos7 ~]# cat /etc/default/useradd 
    # useradd defaults file
    GROUP=100
    HOME=/home
    INACTIVE=-1					#对应/etc/shadow文件第7列,即用户密码过期的宽限期
    EXPIRE=						 #对应/etc/shadow文件第8列,即用户帐号的有效期
    SHELL=/bin/bash
    SKEL=/etc/skel				#用户家目录的模板目录
    CREATE_MAIL_SPOOL=yes
    
    #修改默认值
    [root@centos8 ~]# useradd -D -s /sbin/nologin
    [root@centos8 ~]# cat /etc/default/useradd
    # useradd defaults file
    GROUP=100
    HOME=/home
    INACTIVE=-1
    EXPIRE=
    SHELL=/sbin/nologin
    SKEL=/etc/skel
    CREATE_MAIL_SPOOL=yes
    #更多配置可以在帮助中Changing the default values小节查看
    

    用户的配置相关文件

    /etc/default/useradd

    /etc/skel

    /etc/login.defs

    批量创建用户修改口令

    #批量创建用户
    newusers passwd 格式文件  
    #批量修改密码
    echo username:passwd | chpasswd 
    

    3.2 usermod

    usermod:修改用户账户属性

    usermod [options] LOGIN
    

    常用选项

    • -u UID: 新UID

    • -g GID: 新主组

    • -G GROUP1[,GROUP2,...[,GROUPN]]]:新附加组,原来的附加组将会被覆盖;若保留原有,则要同时使 用-a 选项

    • -s SHELL:新的默认SHELL

    • -c 'COMMENT':新的注释信息

    • -d HOME: 新家目录不会自动创建;若要创建新 家目录并移动原家数据,同时使用-m选项

    • -l login_name: 新的名字

    • -L: lock指定用户,在/etc/shadow 密码栏的增加 !

    • -U: unlock指定用户,将 /etc/shadow 密码栏的 ! 拿掉

    • -e YYYY-MM-DD: 指明用户账号过期日期

    • -f INACTIVE: 设定非活动期限,即宽限期

    3.3 userdel

    userdel:删除用户

    userdel [options] LOGIN
    

    常用选项

    • -f, --force 强制
    • -r, --remove 删除用户家目录和邮箱

    3.4 id

    id:查看用户相关的id信息

    id [OPTION]... [USER]
    

    常用选项

    • -u: 显示UID
    • -g: 显示GID
    • -G: 显示用户所有所属的组的ID
    • -n: 显示名称而不是数字,需配合ugG使用

    3.5 su、getent

    getent:getent - get entries from Name Service Switch libraries

    getent [option]... database key...
    

    su:命令可以切换用户身份,并且以指定用户的身份执行命令

    su [options...] [-] [user [args...]]
    

    常用选项

    • -l --login su -l UserName 相当于 su - UserName
    • -c, --command pass a single command to the shell with -c
    • -s 使用指定的shell类型进行切换

    加-和不加-的区别

    ​ su UserName:非登录式切换,即不会读取目标用户的配置文件,不改变当前工作目录,即不完 全切换

    ​ su - UserName:登录式切换,会读取目标用户的配置文件,切换至自已的家目录,即完全切换

    ​ 注意:su 切换新用户后,使用 exit 退回至旧的用户,而不要再用 su 切换至旧用户,否则会生成很多的 bash子进程,环境可能会混乱。

    范例

    #/sbin/nologin和/bin/false这两种shell类型都不能登陆
    [root@Centos7 ~]# su user
    [user@Centos7 root]$ pwd
    /root
    [user@Centos7 root]$ exit
    exit
    
    [root@Centos7 ~]# su - user
    Last login: Mon Dec 21 02:49:38 CST 2020 on pts/0
    [user@Centos7 ~]$ pwd
    /home/user
    
    [user@Centos7 root]$ getent passwd user
    user:x:1000:1000:user:/home/user:/bin/bash
    

    3.6 passwd

    passwd:设置修改用户密码

    passwd  [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warn‐
           days] [-i inactivedays] [-S] [--stdin] [username]
    

    常用选项

    • --stdin:从标准输入接收用户密码,Ubuntu无此选项
    • -e:强制用户下次登录修改密码
    • -d:删除指定用户密码
    • -l:锁定指定用户
    • -u:解锁指定用户

    范例

    #使用标准输出修改密码
    [root@Centos7 ~]# echo 'qwe123' | passwd --stdin user
    
    #限定用户下次登陆必须改密码
    [root@Centos7 ~]# passwd -e user
    Expiring password for user user.
    passwd: Success
    

    3.7 chage

    chage:修改密码策略

    chage [options] LOGIN
    

    常见选项

    • -d LAST_DAY #更改密码的时间(可以强制用户下次登陆必须改密码)
    • -I --inactive INACTIVE #密码过期后的宽限期
    • -E --expiredate EXPIRE_DATE #用户的有效期

    3.8 用户相关的其它命令

    chfn :指定个人信息

    chfn  [-f  full-name]  [-o office] ,RB [ -p office-phone] [-h home-phone]
           -u] [-v] [username]
           
    [root@Centos7 ~]# chfn user
    Changing finger information for user.
    Name [user]: user
    Office []: it
    Office Phone []: 10000
    Home Phone []: 111111
    [root@Centos7 ~]# cat /etc/passwd
    user:x:1000:1000:user,it,10000,111111:/home/user:/bin/bash
    

    chsh:指定shell,相当于usermod -s

    chsh [-s shell] [-l] [-u] [-v] [username]
    [root@centos7 ~]#chsh -s /bin/csh user
    [root@Centos7 ~]# chsh -s /bin/sh user
    Changing shell for user.
    Shell changed.
    [root@Centos7 ~]# getent passwd user
    user:x:1000:1000:user,it,10000,111111:/home/user:/bin/sh
    

    finger:可看用户个人信息

    chsh [username]
    

    3.9 groupadd

    groupadd:创建组

    groupadd [options] group
    

    常见选项

    • -g 指定GID
    • -r 创建系统组

    范例

    [root@Centos7 ~]# groupadd -r mysql
    [root@Centos7 ~]# getent group mysql
    mysql:x:995:
    

    3.10 groupmod

    groupmod:修改组属性

    groupmod [OPTION]... group
    

    常用选项

    • -n group_name: 新名字
    • -g GID: 新的GID

    3.11 groupdel

    groupdel:删除组

    groupdel [options] GROUP
    

    常见选项

    • -f, --force 强制删除,即使是用户的主组也强制删除组

    3.12 gpasswd

    gpasswd:可以更改组密码,也可以修改附加组的成员关系

    gpasswd [OPTION] GROUP
    

    常见选项

    • -a user 将user添加至指定组中
    • -d user 从指定附加组中移除用户user
    • -A user1,user2,... 设置有管理权限的用户列表

    范例

    #添加组成员
    [root@Centos7 ~]# gpasswd -a user mysql
    Adding user user to group mysql
    [root@Centos7 ~]# groups user
    user : user user1 mysql
    #删除组成员
    [root@Centos7 ~]# gpasswd -d user mysql
    Removing user user from group mysql
    

    3.13 groupmems

    groupmems:管理附加组的成员关系

    groupmems [options] [action]
    

    常见选项

    • -g, --group groupname #指定组 (只有root)
    • -a, --add username #指定用户加入组
    • -d, --delete username #从组中删除用户
    • -p, --purge #从组中清除所有成员
    • -l, --list #显示附加组成员列表

    4.文件权限管理

    4.1 chown

    chown:修改文件的属主,也可以修改文件属组

    chown [OPTION]... [OWNER][:[GROUP]] FILE...
    chown [OPTION]... --reference=RFILE FILE...
    #--reference=RFILE  参考指定的的属性,来修改  
    #-R 递归,此选项不建议使用,非常危险!
    

    范例

    [root@Centos7 tmp]# ll
    total 0
    -rw-rw-r--. 1 user user 0 Dec 21 07:44 user.tx
    [root@Centos7 tmp]# chown user2:user2 user.txt 
    [root@Centos7 tmp]# ll
    total 0
    -rw-rw-r--. 1 user2 user2 0 Dec 21 07:44 user.tx
    

    4.2 chgrp

    chgrp:修改文件属组

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

    范例

    [root@centos8 data]# ll
    total 0
    -rw-rw-r-- 1 user user 0 Dec 21 11:43 user.txt
    [root@centos8 data]# chgrp user1 user.txt 
    [root@centos8 data]# ll
    total 0
    -rw-rw-r-- 1 user user1 0 Dec 21 11:43 user.txt
    

    4.3 文件权限

    对文件的权限

    r 可使用文件查看类工具,比如:cat,可以获取其内容
    w 可修改其内容
    x 可以把此文件提请内核启动为一个进程,即可以执行(运行)此文件(此文件的内容必须是可执行)
    

    对目录的权限

    r 可以使用ls查看此目录中文件列表
    
    w 可在此目录中创建文件,也可删除此目录中的文件,而和此被删除的文件的权限无关
    
    x 可以cd进入此目录,可以使用ls -l查看此目录中文件元数据(须配合r权限),属于目录的可访问的最 小权限
    
    X 只给目录x权限,不给无执行权限的文件x权限
    

    4.4 chmod

    chmod:更改文件权限

    chmod [OPTION]... MODE[,MODE]... FILE...
    chmod [OPTION]... OCTAL-MODE FILE...
    chmod [OPTION]... --reference=RFILE FILE...
    #MODE:who opt permission
    #who:u,g,o,a
    #opt:+,-,=
    #permission:r,w,x
    

    范例

    #两种方式修改权限
    [root@centos8 data]# chmod o+w user.txt 
    [root@centos8 data]# ll
    total 0
    -rw-rw-rw- 1 user user1 0 Dec 21 11:43 user.txt
    
    [root@centos8 data]# chmod 755 user.txt 
    [root@centos8 data]# ll
    total 0
    -rwxr-xr-x 1 user user1 0 Dec 21 11:43 user.txt
    

    4.5 umask

    umask:决定新建文件和文件夹的默认权限

    实现方式

    • 新建文件的默认权限: 666-umask,如果所得结果某位存在执行(奇数)权限,则将其权限+1,偶 数不变
    • 新建目录的默认权限: 777-umask

    非特权用户umask默认是 002

    root的umask 默认是 022

    范例

    #查看umask
    [root@centos8 data]# umask
    0022
    #修改umask
    [root@centos8 data]#umask 002
    
    #持久保存umask
    全局设置: /etc/bashrc
    用户设置:~/.bashrc
    

    4.6 SUID、SGID、Sticky

    4.6.1 SUID

    前提:进程有属主和属组;文件有属主和属组

    1.执行者对该程序拥有执行权限

    • SUID只对二进制可执行程序有效,设置在目录上无意义

    • 启动进程后,其进程属主为源程序文件属主

    • 权限在执行过程中有效

    范例

    #两种修改方式
    chmod u+s FILE...
    chmod 6xxx FILE
    
    [root@centos8 test]# chmod 6640 root.txt 
    [root@centos8 test]# ll
    -rwSr-S--- 1 root root 0 Dec 21 12:26 root.txt
    

    4.6.2 SGID

    二进制文件上的SGID权限功能:

    • 启动为进程之后,其进程的属组为原程序文件的属组
    • 任何一个可执行程序文件能不能启动为进程:取决发起者对程序文件是否拥有执行权限

    目录上的SGID权限功能:

    默认情况下,用户创建文件时,其属组为此用户所属的主组,一旦某目录被设定了SGID,则对此目录有 写权限的用户在此目录中创建的文件所属的组为此目录的属组,通常用于创建一个协作目录

    SGID权限设定

    #二进制文件设定权限
    chmod g+s FILE...
    chmod 2xxx FILE
    [root@centos8 test]# chmod g+s root.txt 
    -rw-r-S--- 1 root root 0 Dec 21 12:26 root.txt
    [root@centos8 test]# chmod g-s root.txt 
    
    #目录设定权限
    chmod g+s DIR...
    chmod 2xxx DIR
    chmod g-s DIR...
    

    4.6.3 Sticky

    具有写权限的目录通常用户可以删除该目录中的任何文件,无论该文件的权限或拥有权 在目录设置Sticky 位,只有文件的所有者或root可以删除该文件

    sticky 设置在文件上无意义

    范例

    #权限设定
    chmod o+t DIR...
    chmod 1xxx DIR
    chmod o-t DIR...
    [root@centos8 ~]#ll -d /tmp
    drwxrwxrwt. 15 root root 4096 Dec 12 20:16 /tmp
    

    4.7 chattr

    chattr:设置文件的特殊属性,可以访问 root 用户误操作删除或修改文件

    不能删除,改名,更改

    chattr +i 
    

    只能追加内容,不能删除,改名

    chattr +a
    

    显示特定属性

    lsattr 
    

    4.8 ACL

    ACL:Access Control List,实现灵活的权限管理

    setfacl 可以设置ACL权限
    getfacl 可查看设置的ACL权限
    

    范例

    [root@centos8 test]# setfacl -m u:user:rw root.txt 
    [root@centos8 test]# getfacl root.txt 
    # file: root.txt
    # owner: root
    # group: root
    user::rw-
    user:user:rw-
    group::r--
    mask::rw-
    other::---
    #清楚所有ACL权限
    [root@centos8 test]# setfacl -b root.txt
    #复制file1的acl权限给file2
    getfacl file1 | setfacl --set-file=-   file2  
    

    5 练习

    5.1 用户组练习

    #创建组distro,其GID为2019
    [root@centos8 test]# groupadd -g 2019 distro
    [root@centos8 test]# getent group distro
    distro:x:2019:
    
    #创建用户mandriva,其ID号为1005,基本组为distro
    [root@centos8 test]# useradd -u 1005 -g distro mandriva
    [root@centos8 test]# getent passwd mandriva 
    mandriva:x:1005:2019::/home/mandriva:/sbin/nologin
    
    #创建用户mageia,其ID号为1100,家目录为/home/linux
    [root@centos8 test]# useradd -u 1100 -d /home/linux mageia 
    [root@centos8 test]# ls /home/
    git  linux  mandriva  user  user1
    
    #给用户mageia添加密码,密码为mageedu,并设置密码7天后过期
    [root@centos8 test]# echo mageedu | passwd --stdin mageia
    [root@centos8 test]# chage -M 7 mageia
    
    #删除mandriva,但保留其家目录
    [root@centos8 test]# userdel mandriva
    
    #创建用户slackware,其ID号为2002,其基本组为distro,附加组peguin
    [root@centos8 test]# groupadd peguin
    [root@centos8 test]# useradd -u 2002 -g distro -G peguin slackware 
    [root@centos8 test]# getent passwd slackware 
    slackware:x:2002:2019::/home/slackware:/sbin/nologin
    
    #修改slackware默认shell为/bin/sh
    [root@centos8 ~]# chsh -s /bin/sh slackware 
    Changing shell for slackware.
    Shell changed.
    [root@centos8 ~]# getent passwd slackware 
    slackware:x:2002:2019::/home/slackware:/bin/sh
    
    #为用户slackware新增附加组admins,并设置不可登陆
    [root@centos8 ~]# groupmems -a slackware -g admins 
    [root@centos8 ~]# groups slackware 
    slackware : distro peguin admins
    

    5.2 权限练习

    #创建用户user1、user2、user3,在/data下创建目录test
    [root@centos8 ~]# cat ls.txt 
    user2:x:::::/bin/bash
    user3:x:::::/bin/bash
    [root@centos8 ~]# newusers ls.txt
    [root@centos8 ~]# cat /etc/passwd
    user1:x:1002:1002::/home/user1:/sbin/nologin
    user2:x:2003:2003:::/bin/bash
    user3:x:2004:2004:::/bin/bash
    
    #目录/data/test属主、属组为user1
    [root@centos8 data]# chown user1:user1 /data/test/
    
    #在目录属主、属组不变的情况下,user2对文件user.txt有读写权限
    [root@centos8 data]# setfacl -m u:user2:rw user.txt 
    [root@centos8 data]# getfacl user.txt 
    # file: user.txt
    # owner: user
    # group: user1
    user::rw-
    user:user2:rw-
    group::---
    mask::rw-
    other::---
    
    #user1在/data/test目录下创建文件a1.sh,a2.sh,a3.sh,设置所有用户都不可删除1.sh,2.sh文件,除了user1及root以外,所有用户都不可删除a3.sh
    [root@centos8 test]# chattr +i a1.sh a2.sh
    [root@centos8 test]# rm -rf a1.sh 
    rm: cannot remove 'a1.sh': Operation not permitted
    [root@centos8 test]# chmod 600 a3.sh 
    
    #user3增加附加组user1,同时要求user1不能访问/data/test
    [user1@centos8 test]$ groupmems -a user3 -g user1
    [user1@centos8 data]$ chmod 700 test
    
    #清理/data/test目录下所有文件的acl权限
    [user1@centos8 data]$ setfacl -Rb test/
    
  • 相关阅读:
    国外保健品品牌介绍
    海淘第一单
    表查询语句与方法
    表与表关系
    表完整性约束
    表字段数据类型
    存储引擎
    数据库之MySQL基本操作
    MAC重置MySQL root 密码
    进程池、线程池、协程
  • 原文地址:https://www.cnblogs.com/bestvae/p/14170847.html
Copyright © 2020-2023  润新知