Users, groups, and permissions

A title

Users, groups, and permissions

Subheadings

1.Linux security model

Resource allocation:

Authentication:认证,验证用户身份
Authorization:授权,不同的用户设置不同权限
Accouting|Audition:审计
当用户登录成功时,系统会自动分配令牌token,包括:用户标识和组成员等信息

用户

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


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

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

Subheadings

2. The user and group related files

/etc/passwd:用户及其属性信息(名称、UID、主组ID等)
/etc/shadow:用户密码及其相关属性
/etc/group:组及其属性信息
/etc/gshadow:组密码及其相关属性

passwd文件格式
  
  
  login name:登录用名(wang)
  passwd:密码 (x)
  UID:用户身份编号 (1000)
  GID:登录默认所在组编号 (1000)
  GECOS:用户全名或注释
  home directory:用户主目录 (/home/wang)
  shell:用户默认使用shell (/bin/bash)
  
生成随机密码https://suijimimashengcheng.51240.com/

Subheadings

3. User and Group Management Command

用户管理命令
 
 useradd
  
  
  常见选项
   -u UID
   -o 配合-u 选项,不检查UID的唯一性
   -g GID 指明用户所属基本组,可为组名,也可以GID
   -c "COMMENT“ 用户的注释信息
   -d HOME_DIR 以指定的路径(不存在)为家目录
   -s SHELL 指明用户的默认shell程序,可用列表在/etc/shells文件中
   -G GROUP1[,GROUP2,...] 为用户指明附加组,组须事先存在
   -N 不创建私用组做主组,使用users组做主组
   -r 创建系统用户 CentOS 6之前: ID<500,CentOS 7以后: ID<1000
   -m 创建家目录,用于系统用户
   -M 不创建家目录,用于非系统用户
    
    范例:
     useradd -r -u 48 -g apache -s /sbin/nologin -d /var/www -c "Apache" apache
    

    useradd 命令默认值设定由/etc/default/useradd定义
    
     [root@centos8 ~]#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
    

    新建用户的相关文件
     
     /etc/default/useradd
     /etc/skel/*
     /etc/login.defs



 usermod
  
  
  常见选项:
   -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: 设定非活动期限,即宽限期
 
 
 
 userdel
  
  
  常见选项:
   -f, --force  强制
   -r, --remove 删除用户家目录和邮箱
 
 
 查看用户相关的ID信息
  
  常见选项:
   -u: 显示UID
   -g: 显示GID
   -G: 显示用户所属的组的ID
   -n: 显示名称,需配合ugG使用
 切换用户
  su
   常见选项:
      -l --login  su -l UserName  相当于 su - UserName
      -c, --command <command>     pass a single command to the shell with -c  
     切换用户的方式:
     
     su UserName:非登录式切换,即不会读取目标用户的配置文件,不改变当前工作目录,即不完
     全切换
     
     su - UserName:登录式切换,会读取目标用户的配置文件,切换至自已的家目录,即完全切换
     
     说明:root su至其他用户无须密码;非root用户切换时需要密码
     
     注意:su 切换新用户后,使用 exit 退回至旧的用户,而不要再用 su 切换至旧用户,否则会生成很多的
     bash子进程,环境可能会混乱。

 设置密码

  passwd 可以修改用户密码
   常见选项:
    -d:删除指定用户密码
    -l:锁定指定用户
    -u:解锁指定用户
    -e:强制用户下次登录修改密码
    -f:强制操作
    -n mindays:指定最短使用期限
    -x maxdays:最大使用期限
    -w warndays:提前多少天开始警告
    -i inactivedays:非活动期限
    --stdin:从标准输入接收用户密码,Ubuntu无此选项

 修改用户密码策略
  
  chage 可以修改用户密码策略
   常见选项:
    -d LAST_DAY        #更改密码的时间
    -m --mindays MIN_DAYS
    -M --maxdays MAX_DAYS
    -W --warndays WARN_DAYS
    -I --inactive INACTIVE #密码过期后的宽限期
    -E --expiredate EXPIRE_DATE #用户的有效期
    -l 显示密码策略
 
 
 用户相关其他命令
  chfn 指定个人信息
  chsh 指定shell,相当于usermod -s
  finger 可看用户个人信息


组帐号维护命令
  
  groupadd
   常见选项:
    -g GID 指明GID号;[GID_MIN, GID_MAX]
    -r 创建系统组,CentOS 6之前: ID<500,CentOS 7以后: ID<1000
  
  groupmod
   常见选项:
    -n group_name: 新名字
    -g GID: 新的GID
 
  groupdel
   常见选项:
    -f, --force 强制删除,即使是用户的主组也强制删除组

 更改组密码
  gpasswd命令
   常见选项:
    -a user 将user添加至指定组中
    -d user 从指定附加组中移除用户user
    -A user1,user2,... 设置有管理权限的用户列表
 
 临时切换主组
  newgrp 命令
 
 更改和查看组成员
  groupmems 可以管理附加组的成员关系
   常见选项:
    -g, --group groupname  #更改为指定组 (只有root)
    -a, --add username   #指定用户加入组
    -d, --delete username #从组中删除用户
    -p, --purge        #从组中清除所有成员
    -l,  --list         #显示组成员列表
   groups 可查看用户组关系

Subheadings

4. Understand and set file permissions

文件所有者chown
 用法说明:
  OWNER  #只修改所有者
  OWNER:GROUP #同时修改所有者和属组
  :GROUP  #只修改属组,冒号也可用 . 替换
  -reference=RFILE  #参考指定的的属性,来修改 
  -R #递归,此选项慎用,非常危险!


文件的属组信息chgrp

 文件权限
  owner 属主, u
  group 属组, g
  other 其他, o
  
  注意:用户的最终权限,是从左向右进行顺序匹配,即,所有者,所属组,其他人,一旦匹配权限立即
  生效,不再向右查看其权限
  
  每个文件针对每类访问者都定义了三种常用权限
   r Readable
   w Writable
   x eXcutable
  
  
  对文件的权限:
     r 可使用文件查看类工具,比如:cat,可以获取其内容
     w 可修改其内容
     x 可以把此文件提请内核启动为一个进程,即可以执行(运行)此文件(此文件的内容必须是可执行)

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

     数学法的权限:

       八进制数
       --- 000 0
       --x 001 1
       -w- 010 2
       -wx 011 3
       r-- 100 4
       r-x 101 5
       rw- 110 6
       rwx 111 7
 
 修改文件权限chmod 
  说明:
        MODE:who opt permission
        who:u,g,o,a
        opt:+,-,=
        permission:r,w,x
        修改指定一类用户的所有权限
        u= g= o= ug= a= u=,g=
        修改指定一类用户某个或某个权限
        u+ u- g+ g- o+ o- a+ a- + -
        -R: 递归修改权限

    执行 cp /etc/issue /data/dir 所需要的最小权限?
     /bin/cp 需要x权限
     /etc/ 需要x权限
     /etc/issue 需要r 权限
     /data 需要x权限
     /data/dir 需要w,x 权限   

Subheadings

The default permissions for
new files and directories default permissions
umask
default permissions for the new file: 666-umask, if the results obtained by the presence of execution (odd) authority, it is permission +1, even-
number change
the default permissions for the new directory : 777-umask

Subheadings

6. Special permissions
Special permissions on linux file system:
the SUID
4suid: acting on the binary executable program, when the user performs this program, this program will temporarily inherit the permissions of the owner
SGID
2sgid: 1) acting on the binary executable program when the user executes this program, this program will temporarily inherit the permissions of the owning group
acting on the directory (folder), when a user creates a new file in this directory, the file belongs to the group will automatically inherit the parent directory belongs to the group of
Sticky
1stikcy: the role of the directory, users can delete their own files

 设置文件特殊属性:
  设置文件的特殊属性,可以访问 root 用户误操作删除或修改文件
    chattr +i
     不能删除,改名,更改
     chattr +a
      只能追加内容,不能删除,改名
      lsattr
       显示特定属性

Subheadings

7. File Access Control Lists
acl permissions feature
ACL: Access Control List, flexible rights management
in addition to the owner, owning group, and others documents, more users can set permissions
CentOS7 created by default, xfs and ext4 file system with ACL features
before CentOS7 version, the default ext4 file system without ACL created manually, you need to manually increase

        ACL生效顺序:
       所有者,自定义用户,所属组|自定义组,其他人
       
       setfacl 可以设置ACL权限
       getfacl 可查看设置的ACL权限 


mask permissions
mask affects only the largest human rights group in addition to the owner and the other of the
mask after the need for a logical AND operation with the user's permission to become limited permissions (Effective Permission)
to set user or group must be present in within the range set permissions mask to take effect

Guess you like

Origin www.cnblogs.com/zhaihongyu/p/12591413.html