usermod [options] LOGIN
modify a user account 更改用户属性
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
注意一旦使用-s /sbin/nologin指定用户的bash为nologin,则代表该用户不能登陆系统
-
常用选项
-a:Add the user to the supplementary group 把用户追加到某些组中,仅与-G一起使用 -d:HOME_DIR 修改用户的家目录通常和-m选项一起使用 -e:EXPIRE_DATE指定用户帐号禁用的日期,格式YY-MM-DD -f:INACTIVE 用户密码过期多少天后采用就禁用该帐号,0表示密码已过期就禁用帐号,-1表示禁用此功能,默认值是-1 -g:force use GROUP as new primary group 修改用户的gid,改组一定存在 -G:new list of supplementary GROUPS 把用户追加到某些组中,仅与-a选项一起使用 -l:new value of the login name 修改用户的登录名称 -L:lock the user account 锁定用户的密码 -m:move contents of the home directory to the new location 修改用户的家目录通常和-d选项一起使用 -s:new login shell for the user account 修改用户的shell -u:new UID for the user account 修改用户的uid,该uid必须唯一 -U:unlock the user account 解锁用户的密码
-
新建用户test,密码test,另外添加usertest组
useradd test echo "test" | passwd --stdin test groupadd usertest
-
把test用户加入usertest组
usermod -aG usertest test ##多个组之间用空格隔开
-
修改test用户的家目录
usermod -md /home/usertest
-
修改用户名
usermod -l testnew(新用户名称) test(原来用户名称)
-
锁定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:::
-
解锁testnew的密码
usermod -U testnew sed -n '$p' /etc/shadow testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999: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
-
修改用户的UID
usermod -u 578 testnew (UID必须唯一) id testnew uid=578(testnew) gid=500(test) groups=500(test),501(usertest)
-
修改用户的GID
groupadd -g 578 test1 usermod -g 578 testnew (578组一定要存在) id testnew uid=578(testnew) gid=578(test1) groups=578(test1),501(usertest)
-
指定帐号过期日期
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:
-
指定用户帐号密码过期多少天后,禁用该帐号
usermod -f 0 testnew sed -n '$p' /etc/shadow testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:0:15594: