zoukankan      html  css  js  c++  java
  • Linux_用户和组管理

    何为用户何为组?

    用户分类

    Linux用户分为管理员和普通用户两种:

    用户类别 用户ID
    管理员 0
    普通用户 1-65535

    其中普通用户又分为系统用户和登录用户两种:

    用户类别 用户ID
    系统用户 1-999(为守护类进程获取系统资源而完成权限指派的用户)
    登录用户 1000-60000(为了完成交互式登录使用的用户)

    Linux安全上下文

    运行中的程序:进程(process)

    • 以进程发起者的身份运行
      • root:cat
      • tom:cat
    • 进程所能够访问的所有资源的权限取决于进程的发起者的身份

    Linux通过安全上下文的概念完成用户权限的指派。

    • 先判断用户是否是某文件的属主
    • 再判断用户是否属于某个组
    • 最后定其为其他用户

    Linux用户组类别

    用户组类别 特性
    私有组 创建用户时,如果没有为其指定所属的组, 系统会自动为其创建一个与用户名相同的组
    基本组 用户的默认组
    附加组(额外组) 默认组以外的其它组

    Linux用户和组相关的配置文件

    各配置文件说明

    配置文件 作用
    /etc/passwd 用户及其属性信息(名称、uid、基本组id等等)
    /etc/group 组及其属性信息
    /etc/shadow 用户密码及其相关属性
    /etc/gshadow 组密码及其相关属性。在用户执行基本组切换时使用
    配置文件 /etc/passwd /etc/group
    第一字段 用户名 组名
    第二字段 密码占位符 组密码
    第三字段 UID GID
    第四字段 GID 以当前组为附加组的用户列表(分隔符为逗号)
    第五字段 用户的描述信息
    第六字段 用户家目录
    第七字段 用户的登录shell
    配置文件 /etc/shadow
    第一字段 登录名
    第二字段 加密后的密码
    第三字段 最近一次更改密码的日期
    第四字段 密码的最小使用期限
    第五字段 密码的最大使用期限
    第六字段 密码警告时间段
    第七字段 密码禁用期
    第八字段 帐号的过期日期
    第九字段 保留字段

    密码复杂性

    密码复杂性策略:

    • 使用数字、大写字母、小写字母及特殊字符中至少3种
    • 足够长
    • 使用随机密码,不要使用有意义的单词或数字
    • 定期更换,不要使用最近曾经使用过的密码

    用户和组相关的管理命令

    用户管理

    用户创建命令useradd

    //语法:useradd [option] USERNAME
        -u UID                     //[UID_MIN,UID_MAX]定义在/etc/login.defs文件中
        -g GID                     //指定用户所属基本组,可为组名或GID
        -G groupname,...           //附加组,可以有多个,用逗号隔开。组groupname必须事先存在
        -c "COMMENT"               //注释信息
        -d /path/to/directory      //指定用户的家目录。此目录必须不能事先存在, 
                                   //否则将不会从/etc/skel中复制环境设置文件
        -s shell                   //这里的shell最好使用/etc/shells里面有的shell, 
                                   // /etc/shells指定了当前系统可用的安全shell
        -M                         //创建用户时不给其创建家目录
        -r                         //添加一个系统用户
        -D                         //直接打印/etc/default/useradd文件的内容或配合其它选项 
                                   //(例如-s SHELL)直接修改/etc/default/useradd文件中的默认值
    

    用户删除命令userdel

    //语法:userdel [option] USERNAME
        -r      //删除用户的同时删除其家目录(userdel默认不会删除其家目录)
    

    查看用户帐号的信息命令id

    id          //查看用户的帐号属性信息
        -u      //查看UID
        -g      //查看GID
        -G      //查看Groups
    

    修改用户帐号属性的命令

    //usermod   修改用户属性
    //语法:usermod [options] username
        -u UID
        -g GID
        -a -G groupname    //不使用-a选项,会覆盖此前的附加组
        -d -m              //改变用户家目录的同时把原来家目录的文件移动到新的家目录中
        -e YYYY-MM-DD      //指明用户帐号过期日期
        -f INACTIVE        //设定非活动期限
        -L      //锁定帐号。
                //被锁定的帐号在/etc/shadow文件中密码前面会有一个!感叹号
        -U      //解锁帐号
      
    //chsh  修改用户的默认shell
    //语法:chsh [options] [username]
        -s SHELL
    

    切换用户命令su

    切换用户的方式 特点
    su USERNAME 非登录式切换,即不会读取目标用户的配置文件
    su - USERNAME 登录式切换,即会读取目标用户的配置文件。完全切换
    su - 不指定用户时默认切换至root用户

    注意:root su至其他用户不需要密码,非root用户su至其他用户时需要输入目标用户的密码

    //语法:su [options] [-] [USER [arg]...]
        -c 'COMMAND'    //切换身份执行命令,命令执行结束后又回到原来的身份
    

    bash的配置文件:

    配置文件类型 配置文件路径
    全局配置 /etc/profile /etc/profile.d/*.sh /etc/bashrc
    个人配置 ~/.bash_profile ~/.bashrc
    配置文件类型 功能
    profile类 为交互式登录的shell提供配置,用来设定环境变量、运行命令或脚本
    bashrc类 为非交互式登录的shell提供配置,用来设定本地变量、定义命令别名

    登录式shell如何读取配置文件?
    /etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc

    非登录式shell如何读取配置文件?
    ~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh

    密码管理

    密码管理命令passwd

    语法:passwd [options] [USERNAME]
        --stdin     //从标准输入获取用户密码, 
                    //例:echo "redhat"|passwd --stdin user1
        -l          //锁定用户
        -u          //解锁用户
        -d          //删除用户密码
        -n mindays      //指定最短使用期限
        -x maxdays      //指定最长使用期限
        -w warndays     //提前多少天开始警告
        -i inactivedays     //非活动期限,密码过期后到禁用前的这段时间
    

    密码生成工具openssl

    //语法:openssl command [ command_opts ] [ command_args ]
        command         //包含标准命令、消息摘要命令、加密命令
            version     //查看程序版本号
            dgst        //提取特征码
            passwd      //生成密码
            rand        //生成伪随机数
    
    //提取特征码
    [root@localhost ~]# openssl dgst -md5 /etc/fstab
    MD5(/etc/fstab)= a977cc5e638d779ed880f3fee6d7067e
    
    //生成密码 openssl passwd -1 -salt string        //string一般为8位
    [root@localhost ~]# openssl passwd -1 -salt hellotom
    Password:
    $1$hellotom$HpEDmPGqWwhP/eHg0kJHA0
    
    //生成随机数 openssl rand -base64 NUM
    //NUM表示随机数的长度
    [root@localhost ~]# openssl rand -base64 20
    S/q8tjUSBuUNHb+0cgDX66dbvTQ=
    

    组管理

    创建组命令groupadd

    //语法:groupadd [options] GROUP
        -g GID      //指定GID
        -r          //添加一个系统组
    

    删除组命令groupdel

    //语法:groupdel [options] GROUP
    //删除组时只需要指定组名即可
    

    演示如下:

    用户管理

    1. useradd //用户创建命令;创建一个叫 linux001 的用户(不能用纯数字创建)
    [root@lc ~]# useradd linux001
    [root@lc ~]# id linux001
    uid=1000(linux001) gid=1000(linux001) groups=1000(linux001)
    [root@lc ~]# 
    
    1. ll //列出当前文件或目录的详细信息,含有时间、读写权限、大小、时间等信息;判断用户是否是某个文件的属主
    • r //表示读取权限
    • w //表示写入权限
    • x //表示执行权限
    • ‘-’ //表示无此权限
    [root@lc ~]# ll
    total 12
    -rw-r--r--. 1 root root 12 Nov  8 18:15 abc
    -rw-r--r--. 1 root root  5 Nov  8 18:51 123
    -rw-r--r--. 1 root root  5 Nov  8 18:16 def
    
    1. 进程所能够访问的所有资源的权限取决于进程的发起者的身份 //在root 和 linux001 里编辑abc文件,查看进程
    [root@lc ~]# ps -ef|grep vi
    root         966       1  0 12:00 ?        00:00:00 /usr/bin/VGAuthService -s
    root       15011   14889  0 19:30 pts/0    00:00:00 vi abc
    root       15013   14936  0 19:30 pts/1    00:00:00 grep --color=auto vi
    [root@lc ~]# ps -ef|grep vi
    root         966       1  0 12:00 ?        00:00:00 /usr/bin/VGAuthService -s
    root       15011   14889  0 19:30 pts/0    00:00:00 vi abc
    linux001   15015   14985  0 19:31 pts/2    00:00:00 vim abc
    root       15017   14936  0 19:31 pts/1    00:00:00 grep --color=auto vi
    [root@lc ~]# 
    
    1. -u UID //[UID_MIN,UID_MAX]定义在/etc/login.defs文件中
    [root@lc ~]# useradd -u 2000 linux002
    [root@lc ~]# id linux002
    uid=2000(linux002) gid=2000(linux002) groups=2000(linux002)
    [root@lc ~]# 
    
    1. -g GID //指定用户所属基本组,可为组名或GID
    [root@lc ~]# useradd -g linux001 linux003
    [root@lc ~]# id linux003
    id=2001(linux003) gid=1000(linux001) groups=1000(linux001)
    [root@lc ~]# 
    
    1. -G groupname,... //附加组,可以有多个,用逗号隔开。组groupname必须事先存在
    [root@lc ~]# useradd -g linux001 -G linux002 linux004
    [root@lc ~]# id linux004
    uid=2002(linux004) gid=1000(linux001) groups=1000(linux001),2000(linux002)
    [root@lc ~]# 
    
    1. -c //“COMMENT”,注释信息
    [root@lc ~]# useradd -c "我是中国人,何必学外文,什么ABC,都是狗屁文" linux006
    [root@lc ~]# id linux006
    uid=2003(linux006) gid=2003(linux006) groups=2003(linux006)
    [root@lc ~]# grep linux006 /etc/passwd
    linux006:x:2003:2003:我是中国人,何必学外文,什么ABC,都是狗屁文:/home/linux006:/bin/bash
    [root@lc ~]# 
    
    1. -d //指定用户的家目录,此目录必须不能事先存在,否则将不会从etc/skel中复制环境文件
    [root@lc ~]# ls /opt/
    anaconda-ks.cfg
    [root@lc ~]# useradd -d /opt/007 linux007
    [root@lc ~]# ls /opt/
    007  anaconda-ks.cfg
    
    1. -s //指定的shell登录(组合使用)
    • M //创建用户时不给其创建家目录
    • -r //添加一个系统用户
    [root@lc ~]# useradd -r -M -s /sbin/nologin linux008
    [root@lc ~]# id linux008
    uid=994(linux008) gid=991(linux008) groups=991(linux008)
    

    查看用户账号的信息命令 //id

    1. id //当前登录账户的信息
    [root@lc ~]# id linux008
    uid=994(linux008) gid=991(linux008) groups=991(linux008)
    [root@lc ~]# 
    
    1. id -u //查看UID
    [root@lc ~]# id -u linux008
    994
    [root@lc ~]#
    
    1. id -g //查看GID
    [root@lc ~]# id -g linux008
    991
    [root@lc ~]#
    
    1. id -G //查看Groups
    [root@lc ~]# id -G linux008
    991
    [root@lc ~]#   
    

    删除用户;-r 删除用户的同时删除其家目录(userdel默认不会删除其家目录) //userdel

    [root@lc ~]# ll /home/
    total 0
    drwx------. 2 linux001 linux001 99 Nov  9 20:22 linux001
    drwx------. 2 linux002 linux002 62 Nov  9 19:42 linux002
    drwx------. 2 linux003 linux001 62 Nov  9 19:44 linux003
    drwx------. 2 linux004 linux001 62 Nov  9 20:19 linux004
    [root@lc ~]# userdel -r linux004
    [root@lc ~]# ll /home/
    total 0
    drwx------. 2 linux001 linux001 99 Nov  9 20:22 linux001
    drwx------. 2 linux002 linux002 62 Nov  9 19:42 linux002
    drwx------. 2 linux003 linux001 62 Nov  9 19:44 linux003
    [root@lc ~]# 
    

    修改用户属性 //usermod

    1. -u //修改UID
    [root@lc ~]# id linux001
    uid=1000(linux001) gid=1000(linux001) groups=1000(linux001)
    [root@lc ~]# usermod -u 2000 linux001
    usermod: UID '2000' already exists
    [root@lc ~]# usermod -u 3000 linux001
    [root@lc ~]# id linux001
    uid=3000(linux001) gid=1000(linux001) groups=1000(linux001)
    
    1. -g //修改GID;组必须要存在;下面是把linux001的GID变成了linux002的GID(组名就是GID)
    [root@lc ~]# id linux002
    uid=2000(linux002) gid=2000(linux002) groups=2000(linux002)
    [root@lc ~]# usermod -g linux002 linux001
    [root@lc ~]# id linux001
    uid=3000(linux001) gid=2000(linux002) groups=2000(linux002)
    [root@lc ~]# 
    
    1. -a -G groupname //不使用-a选项,会覆盖此前的附加组
    [root@lc ~]# usermod -a -G lc linux004
    [root@lc ~]# id linux004
    uid=3001(linux004) gid=3001(linux004) groups=3001(linux004),3002(lc)
    [root@lc ~]# 
    
    1. -d -m //-d后面必须加家目录;改变用户家目录的同时把原来家目录的文件移动到新的家目录;下面是把linux004 搬到opt下面,名叫004
    [root@lc ~]# usermod -m -d /opt/004 linux004
    [root@lc ~]# ll /home/
    total 0
    drwx------. 2 lc       lc       62 Nov 10 01:12 lc
    drwx------. 2 linux001 linux002 99 Nov  9 20:22 linux001
    drwx------. 2 linux002 linux002 62 Nov  9 19:42 linux002
    drwx------. 2 linux003 linux001 62 Nov  9 19:44 linux003
    [root@lc ~]# ls /opt/
    004  007  anaconda-ks.cfg
    [root@lc ~]# 
    
    • -L //锁定账户
    • -U //解锁账户
    [root@lc ~]# usermod -L lc
    [root@lc ~]# usermod -U lc
    
    1. -s shell //修改用户的默认shell
    [root@lc ~]# usermod -s /bin/sh linux008
    [root@lc ~]# grep linux008 /etc/passwd
    linux008:x:994:991::/home/linux008:/bin/sh
    [root@lc ~]# 
    

    切换用户命令 //su

    1. su username //非登录切换,既不会读取目标用户的配置文件
    [root@lc ~]# su lc
    [lc@lc root]$ 
    
    1. su - username //登录切换,读取目标用户配置文件
    [lc@lc root]$ su - root
    Password: 
    Last login: Tue Nov 10 00:06:06 CST 2020 from 192.168.91.1 on pts/2
    [root@lc ~]# 
    
    1. su - //不指定用户切换root
    [root@lc ~]# su lc
    [lc@lc root]$ su -
    Password: 
    Last login: Tue Nov 10 01:33:47 CST 2020 on pts/2
    [root@lc ~]# 
    

    4 -c command //切换身份执行命令结束后回到原来身份

    [lc@lc root]$ su -root -c "ip a"
    su: invalid option -- 'r'
    Try 'su --help' for more information.
    [lc@lc root]$ su - root -c "ip a"
    Password: 
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:0c:29:40:37:5a brd ff:ff:ff:ff:ff:ff
        inet 192.168.91.131/24 brd 192.168.91.255 scope global dynamic noprefixroute ens160
           valid_lft 1385sec preferred_lft 1385sec
        inet6 fe80::3c7:210b:e904:d05b/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever
    [lc@lc root]$ 
    

    密码管理命令 //passwd

    1. passwd //给当前用户设置密码;当前是管理员账户所以可以直接管理密码
    [root@runtime ~]# passwd
    Changing password for user root.
    New password: 
    BAD PASSWORD: The password is a palindrome
    Retype new password: 
    passwd: all authentication tokens updated successfully.
    [root@runtime ~]# 
    
    1. 在管理员账户下给别的用户设置密码
    • passwd tom //给tom设置密码
    [root@runtime ~]# passwd tom
    Changing password for user tom.
    New password: 
    BAD PASSWORD: The password is a palindrome
    Retype new password: 
    Sorry, passwords do not match.
    New password: 
    BAD PASSWORD: The password is a palindrome
    Retype new password: 
    passwd: all authentication tokens updated successfully.
    [root@runtime ~]#
    
    • echo '123456'|passwd --stdin tom //把密码给passwd命令给标准输入的tom用户(把密码重定向给tom;标准输入的重定向),这种方式会留下history,history -d 删除
    [root@runtime ~]# echo 'redhat'|passwd --stdin tom
    Changing password for user tom.
    passwd: all authentication tokens updated successfully.
    [root@runtime ~]# 
    

    密码生成工具 //openssl

    • command//包含标准命令、消息摘要命令、加密命令
    • version //查看程序版本号
    • dgst //提取特征码
    • passwd //生成密码
    • rand//生成伪随机数
    1. openssl degt -md5 //生成一个随机密码用md5加密算法
    [tom@runtime ~]$ touch abc
    [tom@runtime ~]$ openssl dgst -md5 abc
    MD5(abc)= d41d8cd98f00b204e9800998ecf8427e
    
    1. openssl rand -base64 20 //生成一个20位的随机密码;
    [root@runtime ~]# openssl rand -base64 20
    wZei4FsrJlink9xah6xWX5bPGF4=
    [root@runtime ~]# openssl rand -base64 30
    lA1M1Jri3c6Skr56aGA8t84eNjZ7ghZU6ScNhmv9
    

    创建组命令 //groupadd

    • -g GID //指定GID
    • -r //添加一个系统组
    1. -r //创建一个组
    [root@runtime ~]# groupadd -r mysys
    [root@runtime ~]# grep mysys /etc/group
    mysys:x:990:
    [root@runtime ~]# 
    
    1. -g//指定一个GID
    [root@runtime ~]# groupadd -g 6000 mysys1
    [root@runtime ~]# grep mysys1 /etc/group
    mysys1:x:6000:
    [root@runtime ~]# 
    

    删除组命令 //groupdel

    • groupdel //删除组
    [root@runtime ~]# groupdel mysys
    [root@runtime ~]# groupdel mysys1
    [root@runtime ~]# grep mysys1 /etc/group
    [root@runtime ~]# grep mysys /etc/group
    [root@runtime ~]# 
    
  • 相关阅读:
    孤荷凌寒自学python第六十五天学习mongoDB的基本操作并进行简单封装4
    孤荷凌寒自学python第六十四天学习mongoDB的基本操作并进行简单封装3
    孤荷凌寒自学python第六十三天学习mongoDB的基本操作并进行简单封装2
    孤荷凌寒自学python第六十二天学习mongoDB的基本操作并进行简单封装1
    孤荷凌寒自学python第六十一天在Fedora28版的linux系统上找搭建本地Mongodb数据服务
    孤荷凌寒自学python第六十天在windows10上搭建本地Mongodb数据服务
    孤荷凌寒自学python第五十九天尝试使用python来读访问远端MongoDb数据服务
    C++ 虚函数表与内存模型
    最长公共子序列
    最长公共子字符串
  • 原文地址:https://www.cnblogs.com/leixixi/p/14062021.html
Copyright © 2011-2022 走看看