用户与组管理

用户与组管理

一.用户和组的概念

1.1 用户的概念

1.用户确认登录的身份(账户,密码),区分不同人的身份,权限分配,不同人的身份有不同的权限

2.用户存在的目的是为了资源的分配。

3.认证:确认身份(账号,密码)登录

授权:用户权限不一样

审计:用户登录做的事情,什么时候登录的给记录下来

二.用户分类,组的分类

2.1 用户分类
  • 用户分为管理员和普通用户

  • 用户类别用户ID
    管理员0
    普通用户1-65535
  • 普通用户分为系统用户和登录用户

  • 用户类别用ID
    系统用户1-999(为守护类进程获取系统资源而完成权限指派的用户)
    登录用户1000-65535(为了完成交互式登录使用的用户)
2.2 Linux通过安全上下文的概念完成用户权限的指派。
-rw- --- --- . 1 root root 1092 Mar  1 17:08 anaconda-ks.cfg   //以tom用户为例,tom是不是anaconda-ks.cfg这个文件第一个root的拥有者如果是就能用rw-的权限,看tom用户在第二个root的组里面,在的话能使用 --- 的权限,不在这个组里面就定为其他用户,看 --- 的权限
  • 先判断用户是否是某一文件的属主
  • 再判断用户是否属于某个组
  • 最后定其为其他用户
2.3 Linux用户组类别
  • 私有组 —默认组,创建用户时,如果没有为其指定所属的组,
    系统会自动为其创建一个与用户名相同的组
  • 基本组 —用户的默认组
  • 附加组 —默认组(除了私有组和基本组)以外的组
2.4 配置文件说明
  • [root@SYL2 ~]# head -3 passwd 
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    
    /etc/passwd
    root :x :0 :0 :root :/root :/bin/bash
    1. root 用户名 2. :x 密码占位符 3. :0 uid(用户的id) 4. :0 gid(组的id) 5. :root用户说明 6. :/root 用户的家目录 7. :/bin/bash 用户登录shell
    [root@SYL2 ~]# cat /etc/shells  //用户能使用的shells
    /bin/sh
    /bin/bash
    /usr/bin/sh
    /usr/bin/bash
    
  • [root@SYL2 ~]# cat /etc/group
    root:x:0:
    bin:x:1:
    daemon:x:2:
    
    etc/group
    tom :x :1000 :
    1. tom组名 2. :x 密码占位符 3. :1000 gid 4.  : 以当前组为附加组的用户(分隔符为逗号)
    
  • [root@SYL2 ~]# cat /etc/shadow
    root:$6$LGNqZ7R0nFlaSc.z$miwRGvpzAfo2d07e3WqTJaejB.gU6286YyLATI8TWJg0J8yWFDJoIwV5poGZQ6UEwKHLo1BEHyS1wrUtmnaUs0::0:99999:7:::
    bin:*:18397:0:99999:7:::
    
    /etc/shadow
    tom :!! :19080 :0 :99999 :7 : : :
    1. tom 用户名 2. :!! 加密后的密码 3. :19080 最近更改密码的日期(从1970.1.1开始,0表示下次登录必须改密码 空表示密码年龄功能被禁用) 4. :0 密码的最小使用期限 5. :99999 密码的最大使用期限 6. :7提醒用户修改密码的时间间隔  7. :空格 密码禁用期 8. : 帐号的过期日期  9. : 保留字段
    

三.用户管理

3.1 用户创建 — useradd user,用户 add,创建
  • [root@SYL2 ~]# useradd tom
    [root@SYL2 ~]# id tom
    uid=1000(tom) gid=1000(tom) groups=1000(tom)
    [root@SYL2 ~]# useradd zhangs
    [root@SYL2 ~]# useradd lisi
    [root@SYL2 ~]# id zhangs
    uid=1001(zhangs) gid=1001(zhangs) groups=1001(zhangs)
    [root@SYL2 ~]# id lisi
    uid=1002(lisi) gid=1002(lisi) groups=1002(lisi)
    [root@SYL2 ~]# 
    
  • 指定用户UID,后下次创建会自动按照最大创建,并且别的用户没有用 /-u 创建用户的基本组,用户必须存在

    • [root@SYL2 ~]# useradd -u 1500 addy //指定用户UID
      [root@SYL2 ~]# id addy
      uid=1500(addy) gid=1500(addy) groups=1500(addy)
      [root@SYL2 ~]# useradd timi
      [root@SYL2 ~]# id timi
      uid=1501(timi) gid=1501(timi) groups=1501(timi)
      [root@SYL2 ~]# 
      
  • 指定用户GID,用户必须存在(基本组) /-g

    • [root@SYL2 ~]# useradd -g 1000 tiantian //指定用户的基本组
      [root@SYL2 ~]# id tiantian
      uid=1502(tiantian) gid=1000(tom) groups=1000(tom)
      
      
  • 指定用户的附加组 /-G

  • [root@SYL2 ~]# id tiantian
    uid=1502(tiantian) gid=1000(tom) groups=1000(tom)
    [root@SYL2 ~]# useradd -g 1000 -G 1002 wangzhe  //
    [root@SYL2 ~]# id wangzhe
    uid=1503(wangzhe)  gid=1000(tom) //基本组         groups=1000(tom),1002(lisi)  //附加组
    [root@SYL2 ~]# useradd -g 1000 -G 1001 tiantian
    useradd: user 'tiantian' already exists
    [root@SYL2 ~]# id tiantian
    uid=1506(tiantian) gid=1000(tom) groups=1000(tom),1001(zhangs)
    [root@SYL2 ~]# 
    
  • 注释消息 /-c

  • [root@SYL2 ~]# useradd -c "boss" qiqi //注释消息
    [root@SYL2 ~]# tail /etc/passwd
    sssd:x:996:993:User for sssd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    tom:x:1000:1000::/home/tom:/bin/bash
    zhangs:x:1001:1001::/home/zhangs:/bin/bash
    lisi:x:1002:1002::/home/lisi:/bin/bash
    addy:x:1500:1500::/home/addy:/bin/bash
    timi:x:1501:1501::/home/timi:/bin/bash
    tiantian:x:1502:1000::/home/tiantian:/bin/bash
    wangzhe:x:1503:1000::/home/wangzhe:/bin/bash
    qiqi:x:1504:1504:boss:/home/qiqi:/bin/bash
    [root@SYL2 ~]# 
    
    
  • 指定用户的家目录。此目录必须不能事先存在 /-d

  • [root@SYL2 ~]# ls /home/  //没有指定创建的用户默认的位置
    addy  lisi  qiqi  tiantian  timi  tom  wangzhe  zhangs
    [root@SYL2 ~]# ls /opt/
    [root@SYL2 ~]# useradd -d /opt/yinghuo yinghuo  /指定创建用户在opt下,事先不能存在
    [root@SYL2 ~]# ll /opt/
    total 0
    drwx------. 2 yinghuo yinghuo 62 Mar 29 16:53 yinghuo
    [root@SYL2 ~]# 
    
    
  • 创建系统用户 /-r 指定系统用户 /-M 创建用户时不给其创建家目录 /-s 指定不能登录的shell,/sbin/nologin

  • [root@SYL2 ~]# useradd -r -M -s /sbin/nologin yinggou //-r
    [root@SYL2 ~]# id yinggou
    uid=995(yinggou) gid=992(yinggou) groups=992(yinggou)
    [root@SYL2 ~]# tail /etc/passwd
    tom:x:1000:1000::/home/tom:/bin/bash
    zhangs:x:1001:1001::/home/zhangs:/bin/bash
    lisi:x:1002:1002::/home/lisi:/bin/bash
    addy:x:1500:1500::/home/addy:/bin/bash
    timi:x:1501:1501::/home/timi:/bin/bash
    tiantian:x:1502:1000::/home/tiantian:/bin/bash
    wangzhe:x:1503:1000::/home/wangzhe:/bin/bash
    qiqi:x:1504:1504:boss:/home/qiqi:/bin/bash
    yinghuo:x:1505:1505::/opt/yinghuo:/bin/bash
    yinggou:x:995:992::/home/yinggou:/sbin/nologin
    [root@SYL2 ~]# 
    
3.2 查看用户信息 — id
  • [root@SYL2 ~]# id zhangs
    uid=1001(zhangs) gid=1001(zhangs) groups=1001(zhangs)
    [root@SYL2 ~]# id lisi
    uid=1002(lisi) gid=1002(lisi) groups=1002(lisi)
    
  • [root@SYL2 ~]# id -u lisi //查看UID
    1002
    [root@SYL2 ~]# id -g lisi //查看GID
    1002
    [root@SYL2 ~]# id -G lisi //查看Groups
    1002
    
3.3 修改修改用户属性 — usermod
  • 改用户的UID /-u

  • [root@SYL2 ~]# usermod -u 1503 tom
    [root@SYL2 ~]# id tom
    uid=1503(tom) gid=1000(tom) groups=1000(tom)
    [root@SYL2 ~]# usermod -u 1504 tom
    [root@SYL2 ~]# id tom
    uid=1504(tom) gid=1000(tom) groups=1000(tom)
    [root@SYL2 ~]# ll /home/
    total 0
    drwx------. 2 addy   addy   62 Mar 29 16:28 addy
    drwx------. 2 lisi   lisi   62 Mar 29 16:20 lisi
    drwx------. 2 tom      1504 62 Mar 29 16:50 qiqi
    drwx------. 2 timi   timi   62 Mar 29 16:28 timi
    drwx------. 2 tom    tom    62 Mar 29 16:19 tom
    drwx------. 2   1503 tom    62 Mar 29 16:42 wangzhe
    drwx------. 2 zhangs zhangs 62 Mar 29 16:20 zhangs
    [root@SYL2 ~]# 
    
    
  • 改用户的gid /-g

  • [root@SYL2 ~]# usermod -g 1001 tom
    [root@SYL2 ~]# id tom
    uid=1504(tom) gid=1001(zhangs) groups=1001(zhangs)
    [root@SYL2 ~]# grep zhangs /etc/group
    zhangs:x:1001:tiantian
    [root@SYL2 ~]# id tiantian
    uid=1506(tiantian) gid=1000(tom) groups=1000(tom),1001(zhangs)
    
  • 会覆盖此前的附加组 /-G 加一个/-a

  • [root@SYL2 ~]# usermod -G lisi tom  //会覆盖此前的附加组
    [root@SYL2 ~]# id tom
    uid=1504(tom) gid=1001(zhangs) groups=1001(zhangs),1002(lisi)
    [root@SYL2 ~]# usermod -G addy tom
    [root@SYL2 ~]# id tom
    uid=1504(tom) gid=1001(zhangs) groups=1001(zhangs),1500(addy)
    [root@SYL2 ~]# usermod -a -G lisi tom //加个/-a
    [root@SYL2 ~]# id tom
    uid=1504(tom) gid=1001(zhangs) groups=1001(zhangs),1002(lisi),1500(addy)
    
  • 移动用户到别的目录 /-md

  • [root@SYL2 ~]# usermod -md /opt/addy addy //m移动 d目录
    [root@SYL2 ~]# ls /opt/
    addy  yinghuo
    
  • 指明用户帐号过期日期 /-e

  • [root@SYL2 ~]# usermod -e '2022-3-31' zhangs
    [root@SYL2 ~]# grep zhangs /etc/shadow
    zhangs:!!:19080:0:99999:7::19082: //从1970.1.1到2022.3.31
    [root@SYL2 ~]# 
    
    
  • 锁定账户 /-L lock 解锁账户 /-U unlock

  • 修改用户的默认shell

  • [root@SYL2 ~]# usermod -s /sbin/nologin timi
    [root@SYL2 ~]# grep timi /etc/passwd
    timi:x:1501:1501::/home/timi:/sbin/nologin
    [root@SYL2 ~]# 
    
  • 锁定账号 /-L 解锁账号 /-U

  • [root@SYL2 ~]# echo 'run123' | passwd --stdin buliangren  //设密码
    Changing password for user buliangren.
    passwd: all authentication tokens updated successfully.
    [root@SYL2 ~]# tail /etc/shadow
    tom:$6$AWT07jr1LJ4NICve$b9mFSqRNEfsMQttsmF0weIqitnSygAAWhCWB1/JPOcoad3JvrFPlv9IyngBH6oOsn4khPA.8Ph60ZH7RhHhne/:19081:0:99999:7:::
    timi:$6$Ol6bKJygBe/rSQAm$m.TyIj2FZT21oNwvlmS/xIxVKuoC6fkF8wAYdahBCBdW54AaZCuldyanQ.yZtqOWeXutpmMZb7UhWWyw9s/5y1:19081:0:99999:7:::
    buliangren:$6$Ksm6sr6QFB1s.19J$zOuvZWyEg9QiPi/yVcc4RdPNW3XnVkbPLg29Y9kEnzwtMUU0LFLJ8KZOhniT2eKEEKQGmNZne9CtL92.Gm7tX.:19081:0:99999:7:::
    [root@SYL2 ~]# usermod -L buliangren  //锁定账号
    [root@SYL2 ~]# tail /etc/shadow
    buliangren:!$6$Ksm6sr6QFB1s.19J$zOuvZWyEg9QiPi/yVcc4RdPNW3XnVkbPLg29Y9kEnzwtMUU0LFLJ8KZOhniT2eKEEKQGmNZne9CtL92.Gm7tX.:19081:0:99999:7:::
    [root@SYL2 ~]# usermod -U buliangren   //解锁账号
    [root@SYL2 ~]# tail /etc/shadow
    buliangren:$6$Ksm6sr6QFB1s.19J$zOuvZWyEg9QiPi/yVcc4RdPNW3XnVkbPLg29Y9kEnzwtMUU0LFLJ8KZOhniT2eKEEKQGmNZne9CtL92.Gm7tX.:19081:0:99999:7:::
    
    
3.4切换用户 su
  • 非登录式切换,即不会读取目标用户的配置文件

  • [root@SYL2 ~]# su lisi
    [lisi@SYL2 root]$ ls
    ls: cannot open directory '.': Permission denied
    [lisi@SYL2 root]$ exit
    
  • 登录式切换,即会读取目标用户的配置文件。完全切换

  • [root@SYL2 ~]# su - lisi
    Last login: Tue Mar 29 17:50:09 CST 2022 on pts/1
    [lisi@SYL2 ~]$ ls
    [lisi@SYL2 ~]$ pwd
    /home/lisi
    [lisi@SYL2 ~]$
    
    
  • 不指定用户时默认切换至root用户

  • [lisi@SYL2 ~]$ su -
    Password: 
    Last login: Tue Mar 29 13:56:44 CST 2022 from 192.168.232.1 on pts/2
    Last failed login: Tue Mar 29 17:52:38 CST 2022 on pts/1
    There was 1 failed login attempt since the last successful login.
    [root@SYL2 ~]# exit
    logout
    [lisi@SYL2 ~]$ exit
    logout
    [root@SYL2 ~]# 
    
    
  • root su至其他用户不需要密码,非root用户su至其他用户时需要输入目标用户的密码

  • 切换身份执行命令,命令执行结束后又回到原来的身份

  • [root@SYL2 ~]# ls /home/lisi
    [root@SYL2 ~]# su - lisi -c 'touch 999'
    [root@SYL2 ~]# ls /home/lisi
    999
    [root@SYL2 ~]#
    
3.5 bash的配置文件
  • 配置文件类型功能
    profile类为交互式登录的shell提供配置,用来设定环境变量、运行命令或脚本
    bashrc类为非交互式登录的shell提供配置,用来设定本地变量、定义命令别名
    • [root@SYL2 ~]# echo "alias cdnet='cd /etc/sysconfig/network-scripts/'" >> /etc/bashrc
      [root@SYL2 ~]# alias
      alias cdnet='cd /etc/sysconfig/network-scripts/'
      alias cp='cp -i'
      alias egrep='egrep --color=auto'
      
      
  • 文件类型文件路径
    全局配置/etc/profile
    /etc/profile.d/*.sh
    /etc/bashrc
    个人配置~/.bash_profile
    ~/.bashrc
    • [root@SYL2 ~]# ls -a
      .   .bash_history  .bash_profile  .config  .tcshrc  anaconda-ks.cfg
      ..  .bash_logout   .bashrc        .cshrc   1.1.txt  passwd    //在家目录设置只影响当前用户
      [root@SYL2 ~]# ls /etc/profile //在etc下影响所有用户
      /etc/profile
      [root@SYL2 ~]# ls /etc/bashrc 
      /etc/bashrc
      [root@SYL2 ~]# 
      
3.6 用户的删除 userdel
  • [root@SYL2 ~]# userdel wangzhe
    [root@SYL2 ~]# id wangzhe
    id: 'wangzhe': no such user
    [root@SYL2 ~]# ls /home/
    addy  lisi  qiqi  tiantian  timi  tom  wangzhe  zhangs
    [root@SYL2 ~]# 
    
    
  • 删除用户的同时删除其家目录 /-r

  • [root@SYL2 ~]# userdel -r tiantian 
    [root@SYL2 ~]# id tiantian
    id: 'tiantian': no such user
    [root@SYL2 ~]# ls /home/
    addy  lisi  qiqi  timi  tom  wangzhe  zhangs
    [root@SYL2 ~]# 
    
    

四.密码管理

4.1 密码管理命令 — passwd
  • 普通用户设置密码设置不了

  • [root@SYL2 ~]# su - lisi
    Last login: Tue Mar 29 18:15:28 CST 2022 on pts/1
    [lisi@SYL2 network-scripts]$ cd
    [lisi@SYL2 ~]$ passwd
    Changing password for user lisi.
    Current password: 
    passwd: Authentication token manipulation error
    [lisi@SYL2 ~]$ 
    
  • 在管理员设置密码

  • [root@SYL2 ~]# passwd lisi 
    Changing password for user lisi.
    New password:      //run123456
    BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
    Retype new password: 
    passwd: all authentication tokens updated successfully.
    [root@SYL2 ~]# 
    
    
  • 给用户设置密码

  • [lisi@SYL2 ~]$ passwd
    Changing password for user lisi.
    Current password:  //run123456
    New password:  //RUNtime123456!
    Retype new password: //RUNtime123456!
    passwd: all authentication tokens updated successfully.
    [lisi@SYL2 ~]$ 
    
    
  • 管理员设置密码 /–stdin 从标准输入获取用户密码

  • [root@SYL2 ~]# echo 'run123456' | passwd --stdin lisi
    Changing password for user lisi.
    passwd: all authentication tokens updated successfully.
    [root@SYL2 ~]#
    
4.2 密码生成 — openssl
  • openssl dgst -md5 /etc/fstab
    
  • openssl passwd -1 -salt string //string一般为8位
    
  • openssl rand -base64 NUM  //num表示随机数的长度
    
[root@SYL2 ~]# openssl dgst -md5 /etc/fstab 
MD5(/etc/fstab)= 47ed283ec26061970736bd3532043e97  //提取特征码

[root@SYL2 ~]# openssl passwd -1 -salt string
Password:     //888
$1$string$Jw0XKC37KZTtSGfF6DA9H1  //生成密码

[root@SYL2 ~]# openssl rand -base64 10
GxFMJnYSWYfxAg==
[root@SYL2 ~]# openssl rand -base64 22
NJ/mVPvz7e1TcREhV26jViHATFyJWg== //生成随机数
[root@SYL2 ~]# 

4.2.1 tr -dc
[root@SYL2 ~]# tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 |xargs  //A-Za-z0-9_ A-Z大写 a-z小写 0-9 数字 _下划线
swwIH1oI7N53oX_DZZA3p4FjeES5k4
[root@SYL2 ~]# tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 |xargs
NrmRdVI9gUmgNOrG5fdbFkbpm10CcN
[root@SYL2 ~]# tr -dc Az0-9_ < /dev/urandom | head -c 6 |xargs  // /dev/urandom 随机设备
76876_
[root@SYL2 ~]# tr -dc A0-9_ < /dev/urandom | head -c 6 |xargs  /-c字符  xargs放在一起
87A23_
[root@SYL2 ~]# tr -dc 0-9_ < /dev/urandom | head -c 6 |xargs
_766__
[root@SYL2 ~]# tr -dc a-z_ < /dev/urandom | head -c 6 |xargs
utysxy
[root@SYL2 ~]# 
五.组的管理
5.1 组的创建 — groupadd
  • 指定GID /-g 添加一个系统组 /-r

  • [root@SYL2 ~]# groupadd -g 2000 luoluo  //指定GID
    [root@SYL2 ~]# grep luoluo /etc/group
    luoluo:x:2000:
    [root@SYL2 ~]# groupadd -r jingjing //添加一个系统组
    [root@SYL2 ~]# grep jingjing /etc/group
    jingjing:x:991:
    [root@SYL2 ~]# 
    
5.2 组的删除 — groupdel
[root@SYL2 ~]# groupdel jingjing  //删除组
[root@SYL2 ~]# grep jingjing /etc/group
[root@SYL2 ~]# 

-dc a-z_ < /dev/urandom | head -c 6 |xargs
utysxy
[root@SYL2 ~]#


#### 五.组的管理

##### 5.1 组的创建 --- groupadd

- 指定GID /-g    添加一个系统组 /-r

- ```
  [root@SYL2 ~]# groupadd -g 2000 luoluo  //指定GID
  [root@SYL2 ~]# grep luoluo /etc/group
  luoluo:x:2000:
  [root@SYL2 ~]# groupadd -r jingjing //添加一个系统组
  [root@SYL2 ~]# grep jingjing /etc/group
  jingjing:x:991:
  [root@SYL2 ~]# 
5.2 组的删除 — groupdel
[root@SYL2 ~]# groupdel jingjing  //删除组
[root@SYL2 ~]# grep jingjing /etc/group
[root@SYL2 ~]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值