用戶和組管理類命令
useradd
useradd命令用于Linux中創建的新的系統用戶
語法
useradd(選項)(參數)
選項
-c<備注>:加上備注文字。備注文字會保存在passwd的備注欄位中;
-d<登入目錄>:指定用戶登入時的啟始目錄;
-D:變更預設值;
-e<有效期限>:指定帳號的有效期限;
-f<緩沖天數>:指定在密碼過期后多少天即關閉該帳號;
-g<群組>:指定用戶所屬的群組;
-G<群組>:指定用戶所屬的附加群組;
-m:自動建立用戶的登入目錄;
-M:不要自動建立用戶的登入目錄;
-n:取消建立以用戶名稱為名的群組;
-r:建立系統帳號;
-s<shell>:指定用戶登入后所使用的shell;
-u<uid>:指定用戶id。
參數
用戶名:要創建的用戶名
實例
- 新建用戶
[root@localhost ~]#useradd centos [root@localhost ~]# id centos uid=1000(centos) gid=1000(centos) groups=1000(centos)
- 新建用戶加入組
[root@localhost ~]# useradd test1 -g centos -G test,testuser //-g:加入主要組、-G:加入次要組 [root@localhost ~]# id test1 uid=1004(test1) gid=1000(centos) groups=1000(centos),1001(test),1002(testuser)
- 新建用戶,并制定設置ID
[root@localhost ~]# useradd test2 -u 567 [root@localhost ~]# id test2 uid=567(test2) gid=1004(test2) groups=1004(test2)
設定ID值時盡量要大于500,以免沖突。因為Linux安裝后會建立一些特殊用戶,一般0到499之間的值留給bin、mail這樣的系統賬號。
userdel
userdel命令用于刪除給定的用戶,以及與用戶相關的文件。若不加選項,則僅刪除用戶帳號,而不刪除相關文件。
語法 userdel(選項)(參數)
選項
-f:強制刪除用戶,即使用戶當前已登錄;
-r:刪除用戶的同時,刪除與用戶相關的所有文件。
參數
用戶名:要刪除的用戶名。
實例
userdel test1 //刪除用戶test1,但不刪除其家目錄及文件;
userdel -r test2 //刪除用戶test2,其家目錄及文件一并刪除;
usermod
usermod命令用于修改用戶的基本信息。usermod命令不允許你改變正在線上的使用者帳號名稱。當usermod命令用來改變user id,必須確認這名user沒在電腦上執行任何程序。
語法
usermod(選項)(參數)
選項
-a:僅和-G一起使用,將用戶增加到附屬群組
-c<備注>:修改用戶帳號的備注文字;
-d<登入目錄>:修改用戶登入時的目錄;
-e<有效期限>:修改帳號的有效期限;
-f<緩沖天數>:修改在密碼過期后多少天即關閉該帳號;
-g<群組>:修改用戶所屬的群組;
-G<群組>;修改用戶所屬的附加群組;
-l<帳號名稱>:修改用戶帳號名稱;
-L:鎖定用戶密碼,使密碼無效;
-s<shell>:修改用戶登入后所使用的shell;
-u<uid>:修改用戶ID;
-U:解除密碼鎖定。
參數
登錄名:指定要修改信息的用戶登錄名。
實例
- 將test1 的附屬群組改為linux
[root@localhost ~]# usermod -G linux test1 [root@localhost ~]# id test1 uid=1004(test1) gid=1000(centos) groups=1000(centos),1003(linux)
- 將test1 增加附屬群組testuser
[root@localhost ~]# usermod -aG testuser test1 [root@localhost ~]# id test1 uid=1004(test1) gid=1000(centos) groups=1000(centos),1002(testuser),1003(linux)
- 將test1 所屬群組改為 test
[root@localhost ~]# usermod -g test test1 [root@localhost ~]# id test1 uid=1004(test1) gid=1001(test) groups=1001(test),1002(testuser),1003(linux)
- 將test1 用戶名改為testusers
[root@localhost ~]# usermod -l testusers test1 [root@localhost ~]# id testusers uid=1004(testusers) gid=1001(test) groups=1001(test),1002(testuser),1003(linux) [root@localhost ~]#
- 鎖定賬號,使其密碼無效
usermod -L testusers
- 解鎖賬號
usermod -U testusers
groupadd
groupadd命令用于創建一個新的工作組,新工作組的信息將被添加到系統文件中。
語法
groupadd(選項)(參數)
選項
-g:指定新建工作組的id;
-r:創建系統工作組,系統工作組的組ID小于500;
-K:覆蓋配置文件“/ect/login.defs”;
-o:允許添加組ID號不唯一的工作組。
參數
組名:指定新建工作組的組名。
實例
新建一個組,并設置組ID
[root@localhost ~]# groupadd -g 1234 testgroup
[root@localhost ~]# cat /etc/group | grep "testgroup"
testgroup:x:1234:
groupdel
groupdel命令用于刪除指定的工作組,本命令要修改的系統文件包括/ect/group和/ect/gshadow。若該群組中仍包括某些用戶,則必須先刪除這些用戶后,方能刪除群組。
語法
groupdel(參數)
參數
組:要刪除的工作組名。
實例
root@localhost ~]# groupdel testgroup
[root@localhost ~]# cat /etc/group | grep "testgroup"
groupmod
語法
groupmod(選項)(參數)
選項
-g<群組識別碼>:設置欲使用的群組識別碼;
-o:重復使用群組識別碼;
-n<新群組名稱>:設置欲使用的群組名稱。
參數
組名:指定要修改的工作的組名。
綜合實例
- 創建組distro,其GID為2016;
[root@localhost ~]# groupadd distro -g 2016 [root@localhost ~]# cat /etc/group | grep "distro" distro:x:2016:
- 創建用戶mandriva, 其ID號為1005;基本組為distro;
[root@localhost ~]# useradd mandriva -u 1005 -g distro [root@localhost ~]# id mandriva uid=1005(mandriva) gid=2016(distro) groups=2016(distro)
- 創建用戶mageia,其ID號為1100,家目錄為/home/linux;
[root@localhost ~]# useradd mageia -u 1100 -d /home/linux [root@localhost ~]# cat /etc/passwd | grep "mageia" mageia:x:1100:1100::/home/linux:/bin/bash
- 給用戶mageia添加密碼,密碼為mageedu;
[root@localhost ~]# echo "mageedu" | passwd --stdin mageia Changing password for user mageia. passwd: all authentication tokens updated successfully.
- 刪除mandriva,但保留其家目錄;
[root@localhost ~]# userdel mandriva
- 創建用戶slackware,其ID號為2002,基本組為distro,附加組peguin;
[root@localhost ~]# useradd slackware -u 2002 -g distro -G peguin [root@localhost ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)
- 修改slackware的默認shell為/bin/tcsh;
[root@localhost ~]# usermod slackware -s /bin/tcsh [root@localhost ~]# cat /etc/passwd | grep "slackware" slackware:x:2002:2016::/home/slackware:/bin/tcsh
- 為用戶slackware新增附加組admins;
[root@localhost ~]# usermod slackware -aG admins [root@localhost ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/92650