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

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

[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

[ 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日算起,多少天后账号失效

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

  1. Linux Top 命令参数解析

    转载自:http://www.jb51.net/LINUXjishu/34604.html TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户 ...

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

    linux traceroute命令参数及用法详解--linux跟踪路由命令 原文:http://blog.csdn.net/liyuan_669/article/details/25362505 通 ...

  3. [转帖]linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习)

    linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习) https://www.cnblogs.com/caozy/p/9261224.html 总结的挺好的 ...

  4. 【转】linux expr命令参数及用法详解

    在抓包过程中,查看某个设定时间内,数据上下行多少,用命令expr 计算! --------------------------------------------------------------- ...

  5. linux sed命令参数及用法详解

    linux sed命令参数及用法详解 http://blog.csdn.net/namecyf/article/details/7336308 1. Sed简介 sed 是一种在线编辑器,它一次处理一 ...

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

    linux dd命令参数及用法详解---用指定大小的块拷贝一个文件 日期:2010-06-14 点击:3830 来源: 未知 分享至:            linux dd命令使用详解 dd 的主要 ...

  7. (转)linux expr命令参数及用法详解

    linux expr命令参数及用法详解 原文:http://blog.csdn.net/tianmohust/article/details/7628694 expr用法 expr命令一般用于整数值, ...

  8. linux mount命令参数及用法详解

    linux mount命令参数及用法详解 非原创,主要来自 http://www.360doc.com/content/13/0608/14/12600778_291501907.shtml. htt ...

  9. linux dmesg命令参数及用法详解(linux显示开机信息命令)

    linux dmesg命令参数及用法详解(linux显示开机信息命令) http://blog.csdn.net/zhongyhc/article/details/8909905 功能说明:显示开机信 ...

随机推荐

  1. Mybatis 入门 -- 最简单的引入和使用

    参考:http://www.mybatis.org/mybatis-3/zh/getting-started.html 从今天开始学习官方文档. 1.项目搭建 项目结构: 首先,搭建一个maven项目 ...

  2. JS验证图片格式和大小并预览

    用于上传图片的js验证: <%@ page language="java" contentType="text/html; charset=UTF-8"p ...

  3. C# 热敏打印机 Socket 网络链接 打印 图片 (一)

    using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Syste ...

  4. Visual Studio 2013 Update 3 RTM 正式发布

    VS2013.3 RTM已发布! 完整安装包:http://download.microsoft.com/download/6/F/0/6F0777D3-3541-465F-8639-A8F9D36B ...

  5. C#根据句柄改变窗体控件值

    需求是这样,有个程序界面我们需要通过自己的程序持续输入数据,界面如图. 可以获得控件的句柄而用钩子写入值.这里需要用到spy++工具.在VS的工具下有个spy++工具,打开如下图 通过这个工具可以获得 ...

  6. WEB架构师成长系列索引

    WEB架构师成长系列索引 http://www.cnblogs.com/seesea125/archive/2012/04/17/2453256.html

  7. Git合并分支操作

    1. 添加自己的文件 git add .; 2. 缓存自己的文件 git stash; 3. 查看状态 git status; 4. 获取别的分支 git pull origin master(分支名 ...

  8. UDS(ISO14229-2006) 汉译(No.4 术语和缩写)

    A_PCI                    Application layer Protocol Control Information应用层协议控制消息. A_PDU              ...

  9. LZW压缩算法——简明原理与实现

    LZW和哈夫曼编码一样,是无损压缩中的一种.该算法通过建立字典,实现字符重用与编码,适用于source中重复率很高的文本压缩.本文首先讲下LZW的编解码原理,然后给出LZW的实现code. ***** ...

  10. 瞄准SMART目标

    瞄准SMART目标 SMART代表具体的/可度量的/可实现的/相关的和时间可控的. 1.具体的  (一个目标任务应该是具体的/事物的具体化) 2.可度量的  (如何知道你何时完成?确贴的数字,度量具体 ...