zoukankan      html  css  js  c++  java
  • 【Linux命令】id,usermod用户管理命令(包括/etc/passwd、shadow、group、gshadow文件)

    一、id命令

    可以用来查看用户的UID、GID和附加组信息

    id会显示用户以及所属群组的实际与有效ID。若两个ID相同,则仅显示实际ID。若仅指定用户名称,则显示目前用户的ID。

    1.格式

    id [OPTION]... [USER]
    id [-gGnru][--help][--version][用户名称]

    2.参数:

      -a        忽略,为了与其他版本的兼容性
      -Z, --context  只打印当前用户的安全上下文

      -u, --user    只打印用户有效的ID
      -g, --group     只打印有效的组ID
      -G, --groups  打印所有的组【附加组】ID
      -n, --name     打印名称而不是数字。适用于 -ugG
      -r, --real      打印真实的ID替代有效的ID。 对-ugG 而言
      -z, --zero delimit entries with NUL characters, not whitespace;
    not permitted in default format
    --help display this help and exit
    --version output version information and exit

    4.使用

    [root@VM_0_10_centos shellScript]# id root
    uid=0(root) gid=0(root) groups=0(root)

    PS:能看到uid(用户ID)、gid(初始组ID), groups是用户所在组,这里既可以看到初始组,如果有附加组,则也能看到附加组

    5.案例

    [root@VM_0_10_centos shellScript]# id -nug root
    id: cannot print "only" of more than one choice
    [root@VM_0_10_centos shellScript]# id -ng root
    root
    [root@VM_0_10_centos shellScript]# id -nu root
    root
    [root@VM_0_10_centos shellScript]# id -n root
    id: cannot print only names or real IDs in default format
    [root@VM_0_10_centos shellScript]# id -r
    id: cannot print only names or real IDs in default format
    [root@VM_0_10_centos shellScript]# id -ru
    0
    [root@VM_0_10_centos shellScript]# id -ru root
    0
    [root@VM_0_10_centos shellScript]# id -rg root
    0
    [root@VM_0_10_centos shellScript]# id -rG root
    0
    [root@VM_0_10_centos shellScript]# id -ruG root
    id: cannot print "only" of more than one choice

    PS: -nr 不能和ugG连用,只能使用-nu或-nG、-ng。-r同理

    6.查看命令所在位置和类型

    [root@VM_0_10_centos shellScript]# whereis id
    id: /usr/bin/id /usr/share/man/man1/id.1.gz
    [root@VM_0_10_centos shellScript]# type id
    id is hashed (/usr/bin/id)

    二、usermod命令

      修改系统帐户文件来反映通过命令行指定的变化(修改用户账户的各项设置)

      /etc/passwd文件中的每个用户都有一个对应的记录行,记录着这个用户的基本属性。该文件对所有用户可读。

      /etc/shadow文件是passwd文件的一个影子,/etc/shadow文件中的记录行与/etc/passwd中的一一对应,它由pwconv命令根据/etc/passwd中的数据自动产生。但是/etc/shadow文件只有系统管理员才能够进行修改和查看。

    1.格式

    usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G <群组>][-l <帐号名称>][-s <shell>][-u <uid>][用户帐号]

    2.参数

    -a | --append      ##把用户追加到某些组中,仅与-G选项一起使用
    -c | --comment     ##修改/etc/passwd文件第五段comment
    -d | --home        ##修改用户的家目录通常和-m选项一起使用
    -e | --expiredate  ##指定用户帐号禁用的日期,格式YY-MM-DD
    -f | --inactive    ##用户密码过期多少天后采用就禁用该帐号,0表示密码已过期就禁用帐号,-1表示禁用此功能,默认值是-1
    -u | --uid         ##修改用户的uid,该uid必须唯一
    -g | --gid         ##修改用户的gid,改组一定存在
    -G | --groups      ##把用户追加到某些组中,仅与-a选项一起使用
    -l | --login       ##修改用户的登录名称
    -L | --lock        ##锁定用户的密码
    -m | --move-home   ##修改用户的家目录通常和-d选项一起使用
    -s | --shell       ##修改用户的shell-U | --unlock      ##解锁用户的密码

    3.案例

    1)新建test1账户,密码自己设置,添加用户组gtest1

    [root@VM_0_10_centos shellScript]# useradd test1
    [root@VM_0_10_centos shellScript]# echo "密码" | passwd --stdin test1
    [root@VM_0_10_centos shellScript]# groupadd gtest1

    2)将test1用户加入到用户组gtest1中(多个组之间用空格隔开)

    [root@VM_0_10_centos shellScript]# usermod -aG gtest1 test1
    [root@VM_0_10_centos shellScript]# id test1
    uid=1006(test1) gid=1008(test1) groups=1008(test1),1009(gtest1)

    3)修改用户的家目录为usertest

    [root@VM_0_10_centos shellScript]# usermod -md /home/usertest1 test1
    [root@VM_0_10_centos shellScript]# ll /home/
    drwx------ 2 test1    test1    4096 Oct 14 09:57 usertest1

    4)修改用户名称

    [root@VM_0_10_centos shellScript]# usermod -l usertest1 test1
    [root@VM_0_10_centos shellScript]# id usertest1
    uid=1006(usertest1) gid=1008(test1) groups=1008(test1),1009(gtest1)

    5)锁定usertest1的密码

    # 查看usertest1用户的密码
    # $p:匹配$最后一行  /$/p:匹配带$的所有行 [root@VM_0_10_centos shellScript]# sed
    -n '$p' /etc/shadow usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7::: [root@VM_0_10_centos shellScript]# usermod -L usertest1 [root@VM_0_10_centos shellScript]# sed -n '$p' /etc/shadow usertest1:!$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7::: # 查看用户密码锁定状态 [root@VM_0_10_centos shellScript]# passwd -S usertest1 usertest1 LK 2019-10-14 0 99999 7 -1 (Password locked.)

    或使用passwd锁定密码

    [root@rhel7 ~]# passwd -l usertest1    --- -l 锁定
    Locking password for user usertest1.
    passwd: Success
    [root@rhel7 ~]# passwd -S usertest1   --- 查看状态
    usertest1LK 2016-06-20 0 99999 7 -1 (Password locked.)
    [root@rhel7 ~]# passwd -u usertest1   --- 解锁
    Unlocking password for user usertest1.
    passwd: Success
    [root@rhel7 ~]# passwd -S usertest1
    usertest1 PS 2016-06-20 0 99999 7 -1 (Password set, SHA512 crypt.)

    6)解锁usertest1的密码

    [root@VM_0_10_centos usertest1]# usermod -U usertest1
    [root@VM_0_10_centos usertest1]# sed -n '$p' /etc/shadow
    usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7:::

    7)修改用户的shell

    # $!d:最后一行不删除($表示最后一行)  d: 删除pattern中的所有行,并读入下一新行到pattern中
    [root@VM_0_10_centos usertest1]# sed -n '$p' /etc/passwd
    usertest1:x:1006:1008::/home/usertest1:/bin/bash
    [root@VM_0_10_centos usertest1]# sed '$!d' /etc/passwd
    usertest1:x:1006:1008::/home/usertest1:/bin/bash
    [root@VM_0_10_centos usertest1]# usermod -s /bin/sh usertest1
    [root@VM_0_10_centos usertest1]# sed '$!d' /etc/passwd
    usertest1:x:1006:1008::/home/usertest1:/bin/sh
    或
    手动编辑 vi /etc/passwd 找到usertest1编辑保存即可

    8)修改用户的UID、GID

    [root@VM_0_10_centos usertest1]# usermod -u 222 usertest1
    [root@VM_0_10_centos usertest1]# usermod -g 1115 test1

    9)指定账户过期日期

    [root@VM_0_10_centos usertest1]# sed -n '$p' /etc/shadow  
    usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7::: [root@VM_0_10_centos usertest1]# usermod -e 2020-10-15 usertest1 [root@VM_0_10_centos usertest1]# sed -n '$p' /etc/shadow usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7::18550:

    10)指定用户帐号密码过期多少天后,禁用该帐号

    [root@VM_0_10_centos usertest1]# usermod -f 0 usertest1
    [root@VM_0_10_centos usertest1]# sed -n '$p' /etc/shadow
    usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7:0:18550:

    PS:  usermod不允许你改变正在线上的使用者帐号名称。当usermod用来改变userID,必须确认这名user没在电脑上执行任何程序

    三、用户文件详解

    1./etc/passwd、/etc/shadow

    1)/etc/passwd中一行记录对应着一个用户,每行记录又被冒号(:)分隔为7个字段

    其格式和具体含义如下: 

    用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell

    字段解释:

      用户名(login_name):代表用户账号的字符串。通常长度不超过8个字符,并且由大小写字母和/或数字组成。登录名中不能有冒号(:),因为冒号在这里是分隔符。为了兼容起见,登录名中最好不要包含点字符(.),并且不使用连字符(-)和加号(+)打头。
      口令(passwd):存放着加密后的用户口令字。虽然这个字段存放的只是用户口令的加密串,不是明文,但是由于/etc/passwd文件对所有用户都可读,所以这仍是一个安全隐患。因此,现在许多Linux系统(如SVR4)都使用了shadow技术,把真正的加密后的用户口令字存放到/etc/shadow文件中,而在/etc/passwd文件的口令字段中只存放一个特殊的字符,例如“x”或者“*”。
      用户标识号(UID):是一个整数,系统内部用它来标识用户。一般情况下它与用户名是一一对应的。如果几个用户名对应的用户标识号是一样的,系统内部将把它们视为同一个用户,但是它们可以有不同的口令、不同的主目录以及不同的登录Shell等。取值范围是0-65535。0是超级用户root的标识号,1-99由系统保留,作为管理账号,普通用户的标识号从100开始。在Linux系统中,这个界限是500。
      组标识号(GID):用户所属的用户组。它对应着/etc/group文件中的一条记录。
      注释性描述(users):字段记录着用户的一些个人情况,例如用户的真实姓名、电话、地址等,这个字段并没有什么实际的用途。在不同的Linux系统中,这个字段的格式并没有统一。在许多Linux系统中,这个字段存放的是一段任意的注释性描述文字,用做finger命令的输出。
      主目录(home_directory):用户的起始工作目录(家目录),它是用户在登录到系统之后所处的目录。在大多数系统中,各用户的主目录都被组织在同一个特定的目录下,而用户主目录的名称就是该用户的登录名。各用户对自己的主目录有读、写、执行(搜索)权限,其他用户对此目录的访问权限则根据具体情况设置。
      登录Shell(Shell):用户登录后,要启动一个进程,负责将用户的操作传给内核,这个进程是用户登录到系统后运行的命令解释器或某个特定的程序,即Shell。Shell是用户与Linux系统之间的接口。Linux的Shell有许多种,每种都有不同的特点。常用的有sh(BourneShell),csh(CShell),ksh(KornShell),tcsh(TENEX/TOPS-20typeCShell),bash(BourneAgainShell)等。系统管理员可以根据系统情况和用户习惯为用户指定某个Shell。如果不指定Shell,那么系统使用sh为默认的登录Shell,即这个字段的值为/bin/sh。

    2)/etc/shadow文件格式与/etc/passwd文件格式类似,同样由若干个字段组成,字段之间用“:”隔开。

    字段主要含义为:

    登录名:加密口令:最后一次修改时间:最小时间间隔:最大时间间隔:警告时间:不活动时间:失效时间:标志

    字段解释:

      登录名:是与/etc/passwd文件中的登录名相一致的用户账号
      口令:存放的是加密后的用户口令:

        如果为空,则对应用户没有口令,登录时不需要口令;
        星号代表帐号被锁定;
        双叹号表示这个密码已经过期了;
        $6$开头的,表明是用SHA-512加密;
        $1$表明是用MD5加密;
        $2$ 是用Blowfish加密;
        $5$ 是用 SHA-256加密;
      最后一次修改时间:表示的是从某个时刻起,到用户最后一次修改口令时的天数。时间起点对不同的系统可能不一样。例如在SCOLinux中,这个时间起点是1970年1月1日

      最小时间间隔:指的是两次修改口令之间所需的最小天数。
      最大时间间隔:指的是口令保持有效的最大天数。
      警告时间:表示的是从系统开始警告用户到用户密码正式失效之间的天数。
      不活动时间:表示的是用户没有登录活动但账号仍能保持有效的最大天数。
      失效时间:给出的是一个绝对的天数,如果使用了这个字段,那么就给出相应账号的生存期。期满后,该账号就不再是一个合法的账号,也就不能再用来登录了。

    2./etc/group、/etc/gshadow文件

    1)/etc/group

      包括用户组(Group):用户组口令:GID及该用户组所包含的用户(User):每个用户组一条记录

    格式如下:

    group_name:passwd:GID:user_list

    2)/etc/gshadow

      /gshadow和/etc/group是互补的两个文件;对于大型服务器,针对很多用户和组,定制一些关系结构比较复杂的权限模型,设置用户组密码是极有必的。比如我们不想让一些非用户组成员永久拥有用户组的权限和特性,这时我们可以通过密码验证的方式来让某些用户临时拥有一些用户组特性,这时就要用到用户组密码。

    格式如下,每个用户组独占一行;

    groupname:password:admin,admin,…:member,member,…

    字段解释:

      第一字段:用户组
      第二字段:用户组密码,这个段可以是空的或!,如果是空的或有!,表示没有密码;
      第三字段:用户组管理者,这个字段也可为空,如果有多个用户组管理者,用,号分割;
      第四字段:组成员,如果有多个成员,用,号分割;

     

  • 相关阅读:
    Js事件触发列表与解说(转)
    HTTP 头部解释,HTTP 头部详细分析,最全HTTP头部信息
    【转】php 数组 编码转换
    Cookie 的规范介绍
    PHP底层的运行机制与原理
    PHP为什么会被认为是草根语言?
    asp.net中读取带有加号(+)的Cookie,会自动把加号替换为空格
    git克隆某个分支到本地指定的目录中
    yq(json,yaml)格式转换工具安装和使用
    k8s中configmap的作用及使用方式
  • 原文地址:https://www.cnblogs.com/HeiDi-BoKe/p/11671367.html
Copyright © 2011-2022 走看看