標籤:
在Linux系統字元介面下建立、修改以及刪除使用者賬戶主要使用useradd,usermod和userdel這3個命令。
一.建立使用者賬戶
建立使用者賬戶就是在系統中建立一個新賬戶,然後為新賬戶分配使用者UID、使用者組群、主目錄和登入shell等資源,新建立的使用者賬戶預設是被鎖定的,無法使用,需要使用passwd命令設定密碼後才能使用。建立使用者賬戶就是在/etc/passwd檔案中為新使用者增加一條記錄,同時更新其他系統檔案如/etc/shadow,/etc/group等。
使用 useradd命令可以在Linux系統下建立使用者賬戶。
命令文法:
[[email protected] ~]# useradd
Usage: useradd [options] LOGIN
Options:
-b, --base-dir BASE_DIR base directory for the home directory of the
new account
-c, --comment COMMENT GECOS field of the new account
-d, --home-dir HOME_DIR home directory of the new account
-D, --defaults print or change default useradd configuration
-e, --expiredate EXPIRE_DATE expiration date of the new account
-f, --inactive INACTIVE password inactivity period of the new account
-g, --gid GROUP name or ID of the primary group of the new account
-G, --groups GROUPS list of supplementary groups of the new account
-h, --help display this help message and exit
-k, --skel SKEL_DIR use this alternative skeleton directory
-K, --key KEY=VALUE override /etc/login.defs defaults
-l, --no-log-init do not add the user to the lastlog and faillog databases
-m, --create-home create the user‘s home directory
-M, --no-create-home do not create the user‘s home directory
-N, --no-user-group do not create a group with the same name as the user
-o, --non-unique allow to create users with duplicate (non-unique) UID
-p, --password PASSWORD encrypted password of the new account
-r, --system create a system account
-s, --shell SHELL login shell of the new account
-u, --uid UID user ID of the new account
-U, --user-group create a group with the same name as the user
-Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping
例1:建立使用者賬戶zhangsan並設定口令。
[[email protected] ~]# useradd lisi
[[email protected] ~]# useradd wangwu
[[email protected] ~]# passwd wangwu
Changing password for user wangwu.
New password:
BAD PASSWORD: it is WAY too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[[email protected] ~]# cat /etc/passwd |grep lisi
lisi:x:502:502::/home/lisi:/bin/bash
[[email protected] ~]# cat /etc/shadow |grep lisi
lisi:!!:16442:0:99999:7:::
//查看/etc/shadow檔案,可以看到在使用者lisi的口令欄位上顯示的是“!!”,表示該使用者還沒有設定口令,不能登入到Linux系統。
[[email protected] ~]# cat /etc/passwd |grep wangwu
wangwu:x:503:503::/home/wangwu:/bin/bash
[[email protected] ~]# cat /etc/shadow |grep wangwu
wangwu:$1$MC.MaDLW$KFpEpYbJb6mwiAH72ixbb/:16442:0:99999:7:::
[[email protected] ~]#
//查看/etc/shadow檔案,可以看到在使用者wangwu的口令欄位上顯示的是加密的口令,表示該使用者已經設定口令,能登陸到Linux系統。
例2:建立使用者moon,並設定該使用者UID為510。
[[email protected] ~]# useradd -u 510 moon
[[email protected] ~]# cat /etc/passwd | grep moon
moon:x:510:510::/home/moon:/bin/bash
[[email protected] ~]#
//查看/etc/passwd檔案,可以看到使用者moon的UID是510
例3:建立使用者newuser,並設定該使用者主目錄為/home/www。
[[email protected] ~]# useradd -d /home/www newuser
[[email protected] ~]# cat /etc/passwd|grep newuser
newuser:x:511:511::/home/www:/bin/bash
//查看/etc/passwd檔案,可以看到使用者newuser的主目錄是/home/www。
[[email protected] ~]# ls -l /home
total 24
drwx------ 4 lisi lisi 4096 Jan 7 05:29 lisi
drwx------ 4 moon moon 4096 Jan 7 05:45 moon
drwx------ 4 wangwu wangwu 4096 Jan 7 05:29 wangwu
drwx------ 4 newuser newuser 4096 Jan 7 05:53 www
drwx------. 4 yang yang 4096 Jun 2 2014 yang
drwx------ 4 zhangsan zhangsan 4096 Jan 7 05:26 zhangsan
[[email protected] ~]#
//使用者newuser的主目錄/home/www在建立使用者時已經建立了。
例4:建立使用者pp,並指定該使用者是屬於組群root的成員。
[[email protected] ~]# useradd -g root pp
[[email protected] ~]# cat /etc/passwd |grep pp
pp:x:512:0::/home/pp:/bin/bash
//查看/etc/passwd檔案,可以看到pp使用者GID欄位為0,0為root組群的GID。
[[email protected] ~]# id pp
uid=512(pp) gid=0(root) groups=0(root)
[[email protected] ~]#
//使用id命令,可以看到使用者pp是屬於組群root的成員。
例5:建立使用者abc,並設定該使用者的shell類型是/bin/ksh。
[[email protected] ~]# useradd -s /bin/ksh abc
[[email protected] ~]# cat /etc/passwd | grep abc
abc:x:513:513::/home/abc:/bin/ksh
[[email protected] ~]#
例:[[email protected] ~]# su - abc
$ vi yang
#!/bin/ksh
echo "nihao!"
$ ksh yang
nihao!
$
//查看/etc/passwd檔案,可以看到使用者abc的shell類型是/bin/ksh。
二.修改使用者賬戶
使用usermod命令能更改使用者的shell類型、所屬的使用者組群、使用者口令的有效期間,還能更改使用者的登入名稱。
[[email protected] ~]# usermod
Usage: usermod [options] LOGIN
Options:
-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
-a, --append append the user to the supplemental GROUPS
mentioned by the -G option without removing
him/her from other 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
例1:修改使用者zhangsan的主目錄為/home/kkk,並手動建立/home/kkk目錄。
[root[email protected] ~]# usermod -d /home/kkk zhangsan
[[email protected] ~]# cat /etc/passwd|grep zhangsan
zhangsan:x:501:501::/home/kkk:/bin/bash
[[email protected] ~]#
//查看/etc/passwd檔案,可以看到使用者zhangsan的主目錄已經更改為/home/kkk
[[email protected] ~]# mkdir /home/kkk
//必須使用mkdir命令建立/home/kkk目錄,這樣使用者zhangsan才能使用該主目錄。
例2:修改使用者wangwu的主目錄/home/opop,並自動建立/home/opop目錄。
[[email protected] ~]# usermod -d /home/opop wangwu
[[email protected] ~]# cat /etc/passwd|grep wangwu
wangwu:x:503:503::/home/opop:/bin/bash
[[email protected] ~]#
例3:修改使用者wangwu的登入名稱為zhaoliu
[[email protected] ~]# usermod -l jiacheng zhangsan
[[email protected] ~]# cat /etc/passwd|grep jiacheng
jiacheng:x:501:501::/home/kkk:/bin/bash
例4:修改使用者zhangsan在口令到期後20天就禁用該帳號。
[[email protected] home]# useradd zhang
[[email protected] home]# cat /etc/shadow|grep zhang
zhang:!!:16443:0:99999:7:::
//使用者zhang在口令到期後幾天禁用該帳號預設是沒有設定的
[[email protected] home]# usermod -f 20 zhang
[[email protected] home]# cat /etc/shadow |grep zhang
zhang:!!:16443:0:99999:7:20::
//查看/etc/passwd檔案,可以看到使用者zhang將口令到期後20天就禁用該帳號。
例5:修改使用者yang所屬的組群為root,該組群必須事先存在。
[[email protected] home]# usermod -g root yang
[[email protected] home]# cat /etc/passwd |grep yang
yang:x:500:0:yang:/home/yang:/bin/bash
[[email protected] home]#
例6:鎖住使用者yang口令,使口令無效。
[[email protected] home]# usermod -L yang
[[email protected] home]# passwd -S yang
yang LK 2014-06-01 0 99999 7 -1 (Password locked.)
//查看使用者yang口令狀態,可以看到該使用者口令已經鎖住,該使用者不能在系統上登入,但是卻可以從其他使用者賬戶切換到該賬戶。
例7:修改使用者yang帳號的到期日期是2008年12月12號。
[[email protected] ~]# cat /etc/shadow |grep yang
yang:!$1$gUMyUM/y$derjmWWPUVY1/qisPvaQ6/:16223:0:99999:7:::
[[email protected] ~]# usermod -e 12/12/2008 yang
[[email protected] ~]# cat /etc/shadow |grep yang
yang:!$1$gUMyUM/y$derjmWWPUVY1/qisPvaQ6/:16223:0:99999:7::14225:
[[email protected] ~]#
例8:修改使用者yang的shell類型為/bin/ksh
[[email protected] ~]# cat /etc/passwd|grep yang
yang:x:500:0:yang:/home/yang:/bin/bash
[[email protected] ~]# usermod -s /bin/ksh yang
[[email protected] ~]# cat /etc/passwd |grep yang
yang:x:500:0:yang:/home/yang:/bin/ksh
[[email protected] ~]#
//查看/etc/shadow檔案,可以看到使用者yang的SHELL類型已經更改為/bin/ksh
三.刪除使用者賬戶
使用userdel命令可以在LINUX系統下刪除使用者賬戶。
[[email protected] ~]# userdel
Usage: userdel [options] LOGIN
Options:
-f, --force force removal of files, even if not owned by user
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
-Z, --selinux-user remove SELinux user from SELinux user mapping
[[email protected] ~]#
例1:刪除使用者lisi
[[email protected] ~]# cd /home
[[email protected] home]# ls
lisi moon pp wangwu www yang zhang
[[email protected] home]# userdel lisi
[[email protected] home]# cat /etc/passwd |grep lisi
[[email protected] home]# ls /home
lisi moon pp wangwu www yang zhang
[[email protected] home]#
例2:刪除使用者moon,並且在刪除該使用者的同時一起刪除主目錄。
[[email protected] home]# userdel -r moon
[[email protected] home]# ls /home
lisi pp wangwu www yang zhang
[[email protected] home]#
//查看/home目錄的內容,可以看到使用者moon的主目錄隨該使用者一起刪除啦。
Linux字元介面下使用者賬戶的設定