修改用户属性信息 usermod

一、命令详解


1.命令说明

usermod命令用于修改用户的基本信息。如果用户的UID、用户名或用户的主目录发生更改,则必须确保在执行此命令时,命名用户未执行任何进程。

usermod命令不允许更改正在线上的使用者帐号名称。当usermod命令用来改变user id,必须确认这名user没有执行任何程序。

usermod命令的操作修改的是/etc/passwd和/etc/shadow这两个文件中的内容。


2.语法格式

usermod       [option]      [user]
usermod       [选项]         [用户]

3.选项描述

  -c, --comment COMMENT         new value of the GECOS field
                                #修改用户帐号的备注文字
  -d, --home HOME_DIR           new home directory for the user account
                                #修改用户登入时的目录
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
                                #修改帐号的有效期限
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
                                #修改在密码过期后多少天即关闭该帐号
  -g, --gid GROUP               force use GROUP as new primary group
                                #修改用户所属的主组
  -G, --groups GROUPS           new list of supplementary GROUPS
                                #修改用户所属的附加组
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                the user from other groups
                                #将用户附加到-G选项提到的补充组中,而不将其从其他组中移除
  -h, --help                    display this help message and exit 帮助
  -l, --login NEW_LOGIN         new value of the login name
                                #修改用户帐号名称
  -L, --lock                    lock the user account
                                #锁定用户密码,密码无效
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
                                #将主目录的内容移动到新位置(仅与-d一起使用)
  -o, --non-unique              allow using duplicate (non-unique) UID
                                #允许使用重复的(非唯一的)UID
  -p, --password PASSWORD       use encrypted password for the new password
  			 		            #修改用户密码
  -R, --root CHROOT_DIR         directory to chroot into
                                #修改导入目录
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
                                #/etc/*文件所在的前缀目录
  -s, --shell SHELL             new login shell for the user account
                                #修改用户登入后所使用的shell
  -u, --uid UID                 new UID for the user account
                                #修改UID
  -U, --unlock                  unlock the user account 
                                #解除密码锁定
  -v, --add-subuids FIRST-LAST  add range of subordinate uids
                                #添加UID范围
  -V, --del-subuids FIRST-LAST  remove range of subordinate uids
                                #移除UID范围
  -w, --add-subgids FIRST-LAST  add range of subordinate gids
                                #添加GID范围
  -W, --del-subgids FIRST-LAST  remove range of subordinate gids
                                #移除GID范围
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account
                                #用户帐户设置新SELinux用户映射                      

二、命令示例


案例1:修改用户注释说明 usermod -c

使用格式: usermod -c 新注释 用户名

[root@centos7 home]#cat /etc/passwd | grep zhangsansan
zhangsansan:x:3013:1006::/home/zhangsansan:/bin/bash
原来是空的
[root@centos7 home]#usermod -c "this is test for zhangsansan" zhangsansan
[root@centos7 home]#cat /etc/passwd | grep zhangsansan
zhangsansan:x:3013:1006:this is test for zhangsansan:/home/zhangsansan:/bin/bash

案例2:更改用户主目录 usermod -d

在大多数 Linux 系统上,与用户同名的主目录在该/home目录下创建。

如果出于某种原因,想更改用户的主目录,则使用 usermod -d 命令,-d其后跟新主目录的绝对路径用户名的选项

[root@centos7 home]#usermod -d /a zhangsansan
[root@centos7 home]#cat /etc/passwd | grep zhangsansan
zhangsansan:x:3013:1006:hahahahahahahaha:/a:/bin/bash

[root@centos7 zhangsansan]#ls -a /a
.  ..  f1  f2  hr


只是更改目录,没有复制文件过去。


案例3:更改用户主目录并复制文件 usermod -m -d

默认情况下,该命令不会将用户主目录的内容移动到新目录。要移动内容,请使用该-m选项。如果新目录不存在,则创建它。

[root@centos7 zhangsansan]#usermod -d /b  -m zhangsansan
[root@centos7 zhangsansan]#cat /etc/passwd | grep zhangsansan
zhangsansan:x:3013:1006:hahahahahahahaha:/b:/bin/bash



案例4:修改用户主组 usermod -g

格式:usermod -g 组名 用户

[root@centos7 home]#id zhangwu
uid=3015(zhangwu) gid=3019(zhangwu) groups=3019(zhangwu)
[root@centos7 home]#usermod -g testzu zhangwu
[root@centos7 home]#id zhangwu
uid=3015(zhangwu) gid=3017(testzu) groups=3017(testzu)

另外usermod -g 也可以修改组的GID

[root@centos7 home]#id wuzhang
uid=3456(wuzhang) gid=3017(testzu) groups=3017(testzu),3018(zhangsi),3020(testzu1),3021(testzu2)

[root@centos7 home]#usermod -g 3018 wuzhang
[root@centos7 home]#id wuzhang
uid=3456(wuzhang) gid=3018(zhangsi) groups=3018(zhangsi),3017(testzu),3020(testzu1),3021(testzu2)

案例5:修改用户附加组 usermod -G

格式:usermod -G 组名 用户

[root@centos7 home]#usermod -G testzu1 zhangwu
[root@centos7 home]#id zhangwu
uid=3015(zhangwu) gid=3017(testzu) groups=3017(testzu),3020(testzu1)
[root@centos7 home]#groupadd testzu2
[root@centos7 home]#usermod -G testzu2 zhangwu
[root@centos7 home]#id zhangwu
uid=3015(zhangwu) gid=3017(testzu) groups=3017(testzu),3021(testzu2)

案例6:追加用户附加组 usermod -a -G

-a append
要将现有用户添加到辅助组,使用格式:usermod -a -G 组名 用户名

如果要一次将用户添加到多个附加组,在-G选项后指定组,用 ,(逗号)分隔,中间没有空格。

[root@centos7 home]#usermod -a -G testzu1 zhangwu
[root@centos7 home]#id zhangwu
uid=3015(zhangwu) gid=3017(testzu) groups=3017(testzu),3020(testzu1),3021(testzu2)
[root@centos7 home]#

[root@centos7 home]#usermod -a -G testzu1,testzu,zhangsi zhangwu
[root@centos7 home]#id zhangwu
uid=3015(zhangwu) gid=3017(testzu) groups=3017(testzu),3018(zhangsi),3020(testzu1),3021(testzu2)

每个用户只能属于一个主组和零个或多个附加组。


案例7:修改用户shell usermod -s

使用usermod -s参数修改用户的shell环境,centos默认shell环境是/bin/bash,修改shell环境后,可以通过/etc/passwd文件查看。


自身或者已登录用户,可以通过echo $SHELL查看自身的shell。

格式:usermod -s shell 用户

[root@centos7 home]#usermod -s /bin/sh zhangwu
[root@centos7 home]#cat /etc/passwd |grep zhangwu
zhangwu:x:3015:3017::/home/zhangwu:/bin/sh

[root@centos7 home]#echo $SHELL
/bin/bash

案例8:修改用户名 usrmod -l

使用usrmod -l参数修改用户名,修改后需要使用新的用户登录,其他内容不变,要求修改的时候没有该用户的进程。


格式:usermod -l 新名字 原名字

[root@centos7 home]#cat /etc/passwd |grep zhangwu
zhangwu:x:3015:3017::/home/zhangwu:/bin/sh
原用户名zhangwu

[root@centos7 home]#usermod -l wuzhang zhangwu
[root@centos7 home]#cat /etc/passwd |grep zhangwu
wuzhang:x:3015:3017::/home/zhangwu:/bin/sh
已改名字为wuzhang,但是加目录没有变,还是zhangwu


案例9:修改用户UID usermod -u

使用usermod -u修改用户UID,修改的时候要求该ID没有进程在工作。

[root@centos7 home]#id wuzhang
uid=3015(wuzhang) gid=3017(testzu) groups=3017(testzu),3018(zhangsi),3020(testzu1),3021(testzu2)

[root@centos7 home]#usermod -u 3456 wuzhang
[root@centos7 home]#id wuzhang
uid=3456(wuzhang) gid=3017(testzu) groups=3017(testzu),3018(zhangsi),3020(testzu1),3021(testzu2)


案例10:设置用户到期日期 usermod -e

使用usermod -s设置到期日期是用户帐户将被禁用的日期。


使用格式设置到期日期 YYYY-MM-DD

默认为空,密码账户有效。-e后设置的是有效期日期或者从1970年1月1日开始计算的天数。

[root@centos7 home]#cat /etc/shadow |grep wuzhang
wuzhang:!!:19420:0:99999:7:::

[root@centos7 home]#usermod -e "2023-08-08" wuzhang
[root@centos7 home]#cat /etc/shadow |grep wuzhang
wuzhang:!!:19420:0:99999:7::19577:

要禁用帐户到期,请设置一个空的到期日期:

 [root@centos7 home]#usermod -e "" wuzhang
[root@centos7 home]#cat /etc/shadow |grep wuzhang
wuzhang:!!:19420:0:99999:7:::

案例11:超过后天数锁定账户 usermod -f

格式:usermod -f 天数 用户

[root@centos7 home]#cat /etc/shadow |grep wuzhang
wuzhang:!!:19420:0:99999:7:::

[root@centos7 home]#usermod -f 8  wuzhang
[root@centos7 home]#cat /etc/shadow |grep wuzhang
wuzhang:!!:19420:0:99999:7:8::


案例12:锁定账户 usermod -L

使用usermod -L锁定账户,锁定后root账户可以切换到该用户,但是直接登录会验证失败。

当/etc/shadow文件中的密码字段包含感叹号时,用户将无法使用密码认证登录系统。

[root@centos7 home]#usermod -L wuzhang
[root@centos7 home]#cat /etc/passwd |grep wuzhang
wuzhang:x:3456:3018::/home/zhangwu:/bin/sh

[root@centos7 home]#cat /etc/shadow |grep wuzhang
wuzhang:!!:19420:0:99999:7:8::


案例13:解锁账户 usermod -U

使用usermod -U解锁用户,用户锁定后/etc/shadow中密钥字段自动加了一个叹号,解锁后去除。

[root@centos7 home]#usermod -L wuzhang
[root@centos7 home]#cat /etc/shadow |grep wuzhang
wuzhang:!$6$wYxehQHz$jZT2iZoWL/m5Wy/JhGi.tpTfbgdjpExMp0TWbBh4Hx53xjQomQpVZaFnTDcsmxZTWkxq0salOidDml.VkWgp41:19420:0:99999:7:8::

[root@centos7 home]#usermod -U wuzhang
[root@centos7 home]#cat /etc/shadow |grep wuzhang
wuzhang:$6$wYxehQHz$jZT2iZoWL/m5Wy/JhGi.tpTfbgdjpExMp0TWbBh4Hx53xjQomQpVZaFnTDcsmxZTWkxq0salOidDml.VkWgp41:19420:0:99999:7:8::


附加:
用户密码文件 /etc/shadow解释

用户配置文件 /etc/passwd解释

猜你喜欢

转载自blog.csdn.net/Cyan_Jiang/article/details/129337101