Linux – Usermod命令参数解析和实例说明

usermod 命令修改系统帐户文件来反映通过命令行指定的变化

1. 首先看看usermod都是有哪些参数

[[email protected] ~]$ usermod --help
Usage: usermod [options] LOGIN

Options:
  -a, --append                 append the user to the supplemental GROUPS
                               (use only with -G)
  -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
  -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)
  -o, --non-unique             allow using duplicate (non-unique) UID
  -p, --password PASSWORD      use encrypted password for the new password
  -s, --shell SHELL            new login shell for the user account
  -u, --uid UID                new UID for the user account
  -U, --unlock                 unlock the user account
  -Z, --selinux-user           new selinux user mapping for the user account

[ root @ hxweb101 ~ ] $ usermod -- help

Usage : usermod [ options ] LOGIN

Options :

- a , -- append                 append the user to the supplemental GROUPS

( use only with - G )

- 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

- 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 )

- o , -- non - unique             allow using duplicate ( non - unique ) UID

- p , -- password PASSWORD       use encrypted password for the new password

- s , -- shell SHELL             new login shell for the user account

- u , -- uid UID                 new UID for the user account

- U , -- unlock                 unlock the user account

- Z , -- selinux - user           new selinux user mapping for the user account

下面是网友的翻译,我直接拿来了:

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

- a | -- append      ##把用户追加到某些组中,仅与-G选项一起使用

- c | -- comment      ##修改/etc/passwd文件第五段comment

- d | -- home        ##修改用户的家目录通常和-m选项一起使用

- e | -- expiredate ##指定用户帐号禁用的日期,格式YY-MM-DD

- f | -- inactive    ##用户密码过期多少天后采用就禁用该帐号,0表示密码已过期就禁用帐号,-1表示禁用此功能,默认值是-1

- g | -- gid          ##修改用户的gid,改组一定存在

- G | -- groups      ##把用户追加到某些组中,仅与-a选项一起使用

- l | -- login        ##修改用户的登录名称

- L | -- lock        ##锁定用户的密码

- m | -- move - home    ##修改用户的家目录通常和-d选项一起使用

- s | -- shell        ##修改用户的shell

- u | -- uid          ##修改用户的uid,该uid必须唯一

- U | -- unlock      ##解锁用户的密码

实例说明:

比如我有hexu 和 www 用户和用户组:

  1. www创建为内部用户禁止远程登陆,用于运行web service。
  2. hexu创建为外部使用用户,用于登陆、调试或是上传代码等。

这时可以考虑hexu添加到www用户组,并将代码目录允许组读写:

$ usermod -a -G www hexu   # 将hexu添加到www用户组
$ chmod -R ug+w /data/code  # 将目录添加用户组可写

$ usermod - a - G www hexu    # 将hexu添加到www用户组

$ chmod - R ug + w / data / code    # 将目录添加用户组可写

下面是网友的整理,转过来了: 示例(Examples):

#1,新建用户test,密码test,另外添加usertest组
$ useradd test
$ echo "test" | passwd --stdin test
$ groupadd usertest
#2,把test用户加入usertest组
$ usermod -aG usertest test ##多个组之间用空格隔开
$ id test
  uid=500(test) gid=500(test) groups=500(test),501(usertest)
#3,修改test用户的家目录
$ usermod -md /home/usertest
$ ls /home
  usertest
#4,修改用户名
$ usermod -l testnew(新用户名称)  test(原来用户名称)
$ id testnew
  uid=500(testnew) gid=500(test) groups=500(test),501(usertest)
#5,锁定testnew的密码
$ sed -n ‘$p‘ /etc/shadow
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:::
$ usermod -L testnew
$ sed -n ‘$p‘ /etc/shadow
  testnew:!$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:::
#6,解锁testnew的密码
$ usermod -U testnew
$ sed -n ‘$p‘ /etc/shadow
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:::
#7,修改用户的shell
$ sed ‘$!d‘ /etc/passwd
  testnew:x:500:500::/home/usertest:/bin/bash
$ usermod -s /bin/sh testnew
$ sed -n ‘$p‘ /etc/passwd
  testnew:x:500:500::/home/usertest:/bin/sh
# 也可以手动编辑 vi /etc/passwd 找到testnew编辑保存即可
$ vi /etc/password

#8,修改用户的UID
$ usermod -u 578 testnew (UID必须唯一)
$ id testnew
  uid=578(testnew) gid=500(test) groups=500(test),501(usertest)
#9,修改用户的GID
$ groupadd -g 578 test1
$ usermod -g 578 testnew (578组一定要存在)
$ id testnew
  uid=578(testnew) gid=578(test1) groups=578(test1),501(usertest)
#10,指定帐号过期日期
$ sed -n ‘$p‘ /etc/shadow
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:::
$ usermod -e 2012-09-11 testnew
$ sed -n ‘$p‘ /etc/shadow
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7::15594:
11,指定用户帐号密码过期多少天后,禁用该帐号
$ usermod -f 0 testnew
$ sed -n ‘$p‘ /etc/shadow
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:0:15594:

#1,新建用户test,密码test,另外添加usertest组

$ useradd test

$ echo "test" | passwd -- stdin test

$ groupadd usertest

#2,把test用户加入usertest组

$ usermod - aG usertest test ##多个组之间用空格隔开

$ id test

uid = 500 ( test ) gid = 500 ( test ) groups = 500 ( test ) , 501 ( usertest )

#3,修改test用户的家目录

$ usermod - md / home / usertest

$ ls / home

usertest

#4,修改用户名

$ usermod - l testnew ( 新用户名称 )    test ( 原来用户名称 )

$ id testnew

uid = 500 ( testnew ) gid = 500 ( test ) groups = 500 ( test ) , 501 ( usertest )

#5,锁定testnew的密码

$ sed - n ‘$p‘ / etc / shadow

testnew : $ 6 $ 1PwPVBn5 $o .MIEYONzURQPvn / YqSp69kt2CIASvXhOnjv / t

Z5m4NN6bJyLjCG7S6vmji / PFDfbyITdm1WmtV45CfHV5vux / : 15594 : 0 : 99999 : 7 :: :

$ usermod - L testnew

$ sed - n ‘$p‘ / etc / shadow

testnew : ! $ 6 $ 1PwPVBn5 $o .MIEYONzURQPvn / YqSp69kt2CIASvXhOnjv / t

Z5m4NN6bJyLjCG7S6vmji / PFDfbyITdm1WmtV45CfHV5vux / : 15594 : 0 : 99999 : 7 :: :

#6,解锁testnew的密码

$ usermod - U testnew

$ sed - n ‘$p‘ / etc / shadow

testnew : $ 6 $ 1PwPVBn5 $o .MIEYONzURQPvn / YqSp69kt2CIASvXhOnjv / t

Z5m4NN6bJyLjCG7S6vmji / PFDfbyITdm1WmtV45CfHV5vux / : 15594 : 0 : 99999 : 7 :: :

#7,修改用户的shell

$ sed ‘$!d‘ / etc / passwd

testnew : x : 500 : 500 :: / home / usertest : / bin / bash

$ usermod - s / bin / sh testnew

$ sed - n ‘$p‘ / etc / passwd

testnew : x : 500 : 500 :: / home / usertest : / bin / sh

# 也可以手动编辑 vi /etc/passwd 找到testnew编辑保存即可

$ vi / etc / password

#8,修改用户的UID

$ usermod - u 578 testnew ( UID 必须唯一 )

$ id testnew

uid = 578 ( testnew ) gid = 500 ( test ) groups = 500 ( test ) , 501 ( usertest )

#9,修改用户的GID

$ groupadd - g 578 test1

$ usermod - g 578 testnew ( 578 组一定要存在 )

$ id testnew

uid = 578 ( testnew ) gid = 578 ( test1 ) groups = 578 ( test1 ) , 501 ( usertest )

#10,指定帐号过期日期

$ sed - n ‘$p‘ / etc / shadow

testnew : $ 6 $ 1PwPVBn5 $o .MIEYONzURQPvn / YqSp69kt2CIASvXhOnjv / t

Z5m4NN6bJyLjCG7S6vmji / PFDfbyITdm1WmtV45CfHV5vux / : 15594 : 0 : 99999 : 7 :: :

$ usermod - e 2012 - 09 - 11 testnew

$ sed - n ‘$p‘ / etc / shadow

testnew : $ 6 $ 1PwPVBn5 $o .MIEYONzURQPvn / YqSp69kt2CIASvXhOnjv / t

Z5m4NN6bJyLjCG7S6vmji / PFDfbyITdm1WmtV45CfHV5vux / : 15594 : 0 : 99999 : 7 :: 15594 :

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

$ usermod - f 0 testnew

$ sed - n ‘$p‘ / etc / shadow

testnew : $ 6 $ 1PwPVBn5 $o .MIEYONzURQPvn / YqSp69kt2CIASvXhOnjv / t

Z5m4NN6bJyLjCG7S6vmji / PFDfbyITdm1WmtV45CfHV5vux / : 15594 : 0 : 99999 : 7 : 0 : 15594 :

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

/etc/passwd user_name:x:uid:gid:commnet:home:shell

/etc/shadow username:passwd:lastchg:min:max:warn:inactive:expire:flag

  • –用户名
  • –密码
  • –从1970年1月1日起到上次修改密码所经过的天数
  • –密码再过几天可以被变更(0表示随时可以改变)
  • –密码再过几天必须被变更(99999表示永不过期)
  • –密码过期前几天提醒用户(默认为一周)
  • –密码过期几天后帐号被禁用
  • –从1970年1月1日算起,多少天后账号失效
时间: 2024-08-24 17:10:34

Linux – Usermod命令参数解析和实例说明的相关文章

(转)linux traceroute命令参数及用法详解--linux跟踪路由命令

linux traceroute命令参数及用法详解--linux跟踪路由命令 原文:http://blog.csdn.net/liyuan_669/article/details/25362505 通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一样,但基本上来说大部分时候所走的路由是相同的.linux系统中,我们称之为traceroute,在MS

linux dd命令参数及用法详解---用指定大小的块拷贝一个文件(也可整盘备份)

linux dd命令参数及用法详解---用指定大小的块拷贝一个文件 日期:2010-06-14 点击:3830 来源: 未知 分享至: linux dd命令使用详解 dd 的主要选项: 指定数字的地方若以下列字符结尾乘以相应的数字: b=512, c=1, k=1024, w=2, xm=number m if=file 输入文件名,缺省为标准输入. of=file 输出文件名,缺省为标准输出. ibs=bytes 一次读入 bytes 个字节(即一个块大小为 bytes 个字节). obs=b

Linux tar 命令参数及用法详解--Linux打包备份命令

linux tar命令参数及用法详解--linux打包备份命令 tar命令 tar - tar 档案文件管理程序的 GNU 版本.下面将逐个介绍其含义tar [-cxtzjvfpPN] 文件与目录 ....常用参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个压缩文件的参数指令!-t :查看 tarfile 里面的文件!特别注意,在参数的下达中, c/x/t 仅能存在一个!不可同时存在!因为不可能同时压缩与解压缩.-z :是否同时具有 gzip 的属性?亦即是否需

Linux rpm 命令参数使用详解[介绍和应用]

RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的"添加/删除程序" rpm 执行安装包 二进制包(Binary)以及源代码包(Source)两种.二进制包可以直接安装在计算机中,而源代码包将会由RPM自动编译.安装.源代码包经常以src.rpm作为后缀名. 常用命令组合: -ivh:安装显示安装进度--install--verbose--hash -Uvh:升级软件包--Update: -qpl:列出RPM软件包内的文件信息[Q

<Docker>01 命令参数解析

最近Docker1.0稳定版发布了,这给paas注入新鲜的血液. Docker是一个功能强大的自动化分布式系统:大规模的Web部署.数据库集群.持续部署系统.私有PaaS.面向服务的体系结构等. Docker是一种增加了高级API的LinuX Container(LXC)技术,提供了能够独立运行Unix进程的轻量级虚拟化解决方案. 它提供了一种在安全.可重复的环境中自动部署软件的方式. 关于Docker运行在Centos的操作,下面由cantgis进行讲解. Cantgis的实验环境都是在Cen

linux cp命令参数及用法详解---linux 复制文件命令cp

linux cp命令参数及用法详解---linux 复制文件命令cp [[email protected]Linux ~]# cp [-adfilprsu] 来源档(source) 目的檔(destination)[[email protected]linux ~]# cp [options] source1 source2 source3 -. directory参数:-a :相当于 -pdr 的意思:-d :若来源文件为连结文件的属性(link file),则复制连结文件属性而非档案本身:-

linux基础命令参数详解之mkdir

在linux里面基本的命令写法为: 命令  [参数选项]  [文件或路径] 所要使用的命令  [这个是根据需要可变动的但是必须要和使用的命令相匹配的]  [所要处理或查阅的文件或路径] 1:make directorys 中文意思创建目录,在命令使用中的体现方式mkdir. 例1:mkdir  /data 这个命令的意思是在/(根目录)这个目录下创建data的目录. 例2:cd  /;mkdir data 这个命令的意思的先使用cd命令跳转到/(根目录)里面在使用 mkdir 创建data的命令

Linux rpm 命令参数使用详解[

RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的"添加/删除程序" rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种.二进制包可以直接安装在计算机中,而源代码包将会由RPM自动编译.安装.源代码包经常以src.rpm作为后缀名. 常用命令组合: -ivh:安装显示安装进度--install--verbose--hash-Uvh:升级软件包--Update:-qpl:列出RPM软件包内的文件信息[Quer

Linux rpm 命令参数使用详解[介绍和应用]

RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种.二进制包可以直接安装在计算机中,而源代码包将会由RPM自动编译.安装.源代码包经常以src.rpm作为后缀名. 常用命令组合: -ivh:安装显示安装进度--install--verbose--hash-Uvh:升级软件包--Update:-qpl:列出RPM软件包内的文件信息[Query Pac