Linux基礎(chǔ)命令:usermod

usermod [options] LOGIN

  • modify a user account 更改用戶屬性

  • usermod不允許你改變正在線上的使用者帳號名稱。當(dāng)usermod用來改變userID,必須確認(rèn)這名user沒在電腦上執(zhí)行任何程序

  • 查看用戶配置 /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,則代表該用戶不能登陸系統(tǒng)

  • 常用選項(xiàng)

    -a:Add the user to the supplementary group 把用戶追加到某些組中,僅與-G一起使用 
    -d:HOME_DIR 修改用戶的家目錄通常和-m選項(xiàng)一起使用 
    -e:EXPIRE_DATE指定用戶帳號禁用的日期,格式Y(jié)Y-MM-DD 
    -f:INACTIVE 用戶密碼過期多少天后采用就禁用該帳號,0表示密碼已過期就禁用帳號,-1表示禁用此功能,默認(rèn)值是-1 
    -g:force use GROUP as new primary group 修改用戶的gid,改組一定存在
    -G:new list of supplementary GROUPS 把用戶追加到某些組中,僅與-a選項(xiàng)一起使用 
    -l:new value of the login name 修改用戶的登錄名稱 
    -L:lock the user account 鎖定用戶的密碼 
    -m:move contents of the home directory to the new location 修改用戶的家目錄通常和-d選項(xiàng)一起使用 
    -s:new login shell for the user account 修改用戶的shell 
    -u:new UID for the user account 修改用戶的uid,該uid必須唯一 
    -U:unlock the user account 解鎖用戶的密碼
    
  1. 新建用戶test,密碼test,另外添加usertest組

    useradd test
    echo "test" | passwd --stdin test
    groupadd usertest
    
  2. 把test用戶加入usertest組

    usermod -aG usertest test ##多個組之間用空格隔開 
    
  3. 修改test用戶的家目錄

    usermod -md /home/usertest 
    
  4. 修改用戶名

    usermod -l testnew(新用戶名稱)  test(原來用戶名稱) 
    
  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 
    
  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:
    
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容