Linux 用戶管理工具介紹
贊助:
來自:LinuxSir.Org
摘要: 本文主要介紹Linux系統的用戶和用戶組的查詢、添加、修改及刪除等管理工具;
- 1、/etc/passwd 和/etc/groups
- 2、超級權限控制 sudo 的配置文件/etc/sudoers ;
- 3、添加用戶規則文件 /etc/login.defs 和 /etc/default/useradd
目錄索引
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
正文
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
一、與用戶管理相關的配置文件;
1、/etc/passwd 和/etc/groups
2、超級權限控制sudo 的配置文件 /etc/sudoers ;
3、添加用戶規則文件 /etc/login.defs 和 /etc/default/useradd
二、添加用戶工具和方法;
1、useradd 工具;
usage: useradd [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire ] [-p passwd] name
useradd -D [-g group] [-b base] [-s shell]
[-f inactive] [-e expire ]
[root@localhost beinan]# useradd beinanlinux
[root@localhost beinan]# ls -ld /home/beinanlinux/
drwxr-xr-x 3 beinanlinux beinanlinux 4096 11月 2 15:20 /home/beinanlinux/
[root@localhost beinan]# more /etc/passwd | grep beinanlinux
beinanlinux:x:509:509::/home/beinanlinux:/bin/bash
1)/etc/default/useradd 配置文件的定義;
useradd -D [-g group] [-b base] [-s shell] [-f inactive] [-e expire ]
useradd -D [-g group] [-b base] [-s shell] [-f inactive] [-e expire ]
當-D選項出現時,useradd秀出現在的預設值,或是藉由命令列的方式更新預設值。可用選項爲∶
-b default_home 注:定義用戶所屬目錄的前一個目錄。用戶名稱會附加在default_home後面用來建立新用戶的目錄。當然使用-d後則此選項無效。
-e default_expire_date 注:用戶帳號停止日期。
-f default_inactive 注:帳號過期幾日後停權。
-g default_group 注:新帳號起始用戶組名或ID。用戶組名須爲現有存在的名稱。用戶組ID也須爲現有存在的用戶組。
-s default_shell 注:用戶登入後使用的shell名稱。往後新加入的帳號都將使用此shell.
如不指定任何參數,useradd顯示目前預設的值。
useradd -D 如不指定任何參數,useradd顯示目前預設的值;
[root@localhost beinan]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
實例三:
我想把添加用戶時的默認SHELL /bin/bash 改爲 /bin/tcsh ,則應該用下面的命令;
[root@localhost beinan]# useradd -D -s /bin/tcsh 注:把添加用戶時的SHELL 改爲tcsh ;
[root@localhost beinan]# more /etc/default/useradd 注:查看是否成功;
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/tcsh 注:成功;
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
2)useradd 添加用戶;
useradd [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire ] [-p passwd] name
useradd 可使用的選項爲
-c comment 注:新帳號 password 檔的說明欄 。
-d home_dir 注:新帳號每次登入時所使用的home_dir。預設值爲default_home內login名稱,並當成登入時目錄名稱。
-e expire_date 注:帳號終止日期。日期的指定格式爲MM/DD/YY。
-f inactive_days 注:帳號過期幾日後永久停權。當值爲0時帳號則立刻被停權。而當值爲-1時則關閉此功能,預設值爲-1
-g initial_group 注:group名稱或以數字來做爲用戶登入起始用戶組(group)。用戶組名須爲現有存在的名稱。用戶組數字也須爲現有存在的用戶組。預設的用戶組數字爲1。
-G group,[...]
注:定義此用戶爲此一堆groups的成員。每個用戶組使用","區格開來,不可以夾雜空白字元。用戶組名同-g選項的限制。定義值爲用戶的起始用戶組。。
-m 注: 用戶目錄如不存在則自動建立。如使用-k選項skeleton_dir內的檔案將複製至用戶目錄下。然而在/etc/skel目錄下的檔案也會複製過去取代。任何在skeleton_dir or /etc/skel的目錄也相同會在用戶目錄下一一建立。The-k同-m不建立目錄以及不復制任何檔案爲預設值。
-M 不建立用戶目錄,即使/etc/login.defs系統檔設定要建立用戶目錄。
-n 預設值用戶用戶組與用戶名稱會相同。此選項將取消此預設值。
-r 此參數是用來建立系統帳號。系統帳號的UID會比定義在系統檔上/etc/login.defs.的UID_MIN來的小。注意useradd此用法所建立的帳號不會建立用戶目錄,也不會在乎紀錄在/etc/login.defs.的定義值。如果你想要有用戶目錄須額外指定-m參數來建立系統帳號。
-s shell 注:用戶登入後使用的shell名稱。預設爲不填寫,這樣系統會幫你指定預設的登入shell。
-u uid uid用戶的ID值。必須爲唯一的ID值,除非用-o選項。數字不可爲負值。預設爲以/etc/login.defs中的UID_MIN的值爲準,0到UID_MIN的值之間,爲系統保留的UID ;
[root@localhost ~]# useradd longcpu
[root@localhost ~]# useradd -c ChinaCpu longcpu 注:添加一個新用戶amdcpu ,使用參數-c;
[root@localhost ~]# more /etc/passwd |grep longcpu 注:查看/etc/passwd 文件,並抽取longcpu的記錄;
longcpu:x:510:510:ChinaCpu:/home/longcpu:/bin/bash
[root@localhost ~]# finger longcpu 注:我們查詢一下amdcpu 用戶的信息;
Login: longcpu Name: ChinaCpu 注:-c ChinaCpu 表示用戶真實的名字或全名;
Directory: /home/longcpu Shell: /bin/bash
Never logged in.
No mail.
No Plan.
[root@localhost ~]# useradd -c ChinaCpu -d /opt/longcpu -G linuxsir,root,beinan -s /bin/tcsh longcpu
注:添加用戶longcpu ,真實名是ChinaCpu ,家目錄設置在 /opt/longcpu ,是linuxsir,root,beinan 用戶組成員, SHELL是tcsh ;
[root@localhost ~]# ls -ld /opt/longcpu/ 注:是不是自動創建了longcpu的家目錄?
drwxr-xr-x 3 longcpu longcpu 4096 11月 4 22:30 /opt/longcpu/
[root@localhost ~]# more /etc/passwd |grep longcpu 注:查看 /etc/passwd 中是否有longcpu用戶記錄;
longcpu:x:510:510:ChinaCpu:/opt/longcpu:/bin/tcsh
[root@localhost beinan]# finger longcpu 注:查詢longcpu用戶的信息 ;
Login: longcpu Name: ChinaCpu
Directory: /opt/longcpu Shell: /bin/tcsh
Never logged in.
No mail.
No Plan.
[root@localhost beinan]# id longcpu 注:查詢UID和GID 以及所歸屬的用戶組;
uid=510(longcpu) gid=510(longcpu) groups=510(longcpu),0(root),500(beinan),502(linuxsir)
實例七:練習用戶有效期限;
[root@localhost ~]# useradd -e 11/04/2005 cooler 注:添加用戶cooler,並設置其有效期爲2005年11月04日;
[root@localhost ~]# passwd cooler 注:設置用戶cooler密碼;
Changing password for user cooler.
New UNIX password: 注:設定cooler的密碼;
Retype new UNIX password: 注:覈實設定密碼;
passwd: all authentication tokens updated successfully. 注:設置成功;
2、adduser 工具;
[root@localhost ~]# adduser 注:運行adduser命令;
Login name for new user []: bluemoon 注:添加新用戶 bluemoon
User ID ('UID') [ defaults to next available ]: 1200 注:用戶的UID ,UID 是唯一的;如果有提示說被佔用,就選比較大的UID ,比如1300
Initial group [ users ]: users 注:初始化用戶組(或主用戶組)爲users,這個用戶組也是可以自己定義的,但用戶組必須存在,如果不存在,您可以用groupadd來添加
Additional groups (comma separated) []: root,beinan 注:附加用戶組,這個也是自己定義的,多個用戶組之間用,號分割;
Home directory [ /home/bluemoon ] 注:定義用戶的家目錄位置,也是可以自己定義的,比如/opt/bluemoon ;
Shell [ /bin/bash ] 注:所用SHELL ,此處用的是bash ;
Expiry date (YYYY-MM-DD) []: 注:用戶的有效日期,如果不設置就直接回車,表示從不過期;如果設置就以2005-11-05這樣的格式來輸入;
New account will be created as follows: 注:創建的用戶情況如下;
---------------------------------------
Login name.......: bluemoon
UID..............: 1200
Initial group....: users
Additional groups: root
Home directory...: /home/bluemoon
Shell............: /bin/bash
Expiry date......: [ Never ]
This is it... if you want to bail out, hit Control-C. Otherwise, press
ENTER to go ahead and make the account.
注:在這裏按回車就開始創建,如果認爲這樣不合理,就按CTRL+C 來中斷;
Creating new account... 這樣就創建好了;系統會自動提示我們修改用戶的信息,比如用戶的全名、房間號、電話等...... 以及用戶的密碼;
Changing the user information for bluemoon
Enter the new value, or press ENTER for the default
Full Name []: bluemoon Linux
Room Number []: 503
Work Phone []: 0411-8888888
Home Phone []: 0411-9999999
Other []:
Changing password for bluemoon
Enter the new password (minimum of 5, maximum of 127 characters)
Please use a combination of upper and lower case letters and numbers.
New password: 注:設置用戶bluemoon的密碼;
Re-enter new password: 注:驗證一次;
Password changed. 注:設置密碼成功
3、通過修改/etc/passwd 管理(添加、刪除、修改)用戶的方法;
4、添加用戶組命令groupadd ;
groupadd [-g gid [-o]] [-r] [-f] group
groupadd 可指定用戶組名稱來建立新的用戶組帳號,需要時可從系統中取得新用戶組值。groupadd有下列選項可用。
-g 後接GID值,除非使用-o參數不然該值必須是唯一,不可相同,數值不可爲負,預設值以/etc/login.defs爲準;
-r
此參數是用來建立系統帳號的GID會比定義在系統檔文件上/etc/login.defs 的GID_MIN來的小。注意useradd此用法所建立的帳號不會建立使用者目錄,也不會紀錄在/etc/login.defs.的定義值。如果你想要有使用者目錄須額外指定-m參數來建立系統帳號,它會自動幫你選定一個小於的GID_MIN的值,不需要再加上-g參數。
-f This is force flag.新增一個已經存在的用戶組帳號,系統會出現錯誤訊息然後結束groupadd。如果是這樣的情況,不會新增這個用戶組(如果是這個情況下,系統不會再新增一次) 也可同時加上-g選項,當你加上一個GID,此時GID就不用是唯一值,可不加-o參數,建好用戶組後會顯結果(adding a group as neither -g or -o options were specified)。
[root@localhost ~]# groupadd -g 666 google
三、passwd 設置或修改用戶密碼;
1、passwd 簡單說明;
passwd [OPTION...]
[root@localhost ~]# passwd 注:沒有加任何用戶,我是用root用戶來執行的passwd 表示修改root用戶的密碼;下面也有提示;
Changing password for user root.
New UNIX password: 注:請輸入新密碼;
Retype new UNIX password: 注:驗證新密碼;
passwd: all authentication tokens updated successfully. 注:修改root密碼成功;
[root@localhost ~]# passwd beinan 注:更改或創建beinan用戶的密碼;
Changing password for user beinan.
New UNIX password: 注:請輸入新密碼;
Retype new UNIX password: 注:再輸入一次;
passwd: all authentication tokens updated successfully. 注:成功;
[beinan@localhost ~]$ passwd
Changing password for user beinan. 注:更改beinan用戶的密碼;
(current) UNIX password: 注:請輸入當前密碼;
New UNIX password: 注:請輸入新密碼;
Retype new UNIX password: 注:確認新密碼;
passwd: all authentication tokens updated successfully. 注:更改成功;
2、passwd 幾個比較重要的參數;
[root@localhost beinan]# passwd --help
Usage: passwd [OPTION...] <accountName>
-k, --keep-tokens keep non-expired authentication tokens
注:保留即將過期的用戶在期滿後能仍能使用;
-d, --delete delete the password for the named account (root only)
注:刪除用戶密碼,僅能以root權限操作;
-l, --lock lock the named account (root only)
注:鎖住用戶無權更改其密碼,僅能通過root權限操作;
-u, --unlock unlock the named account (root only)
注:解除鎖定;
-f, --force force operation
注:強制操作;僅root權限才能操作;
-x, --maximum=DAYS maximum password lifetime (root only) 注:兩次密碼修正的最大天數,後面接數字;僅能root權限操作;
-n, --minimum=DAYS minimum password lifetime (root only) 注:兩次密碼修改的最小天數,後面接數字,僅能root權限操作;
-w, --warning=DAYS number of days warning users receives before 注:在距多少天提醒用戶修改密碼;僅能root權限操作;
password expiration (root only)
-i, --inactive=DAYS number of days after password expiration when an 注:在密碼過期後多少天,用戶被禁掉,僅能以root操作;
account becomes disabled (root only)
-S, --status report password status on the named account (root 注:查詢用戶的密碼狀態,僅能root用戶操作;
only)
--stdin read new tokens from stdin (root only)
[root@localhost ~]# passwd -l beinan 注:鎖定用戶beinan不能更改密碼;
Locking password for user beinan.
passwd: Success 注:鎖定成功;
[beinan@localhost ~]# su beinan 注:通過su切換到beinan用戶;
[beinan@localhost ~]$ passwd 注:beinan來更改密碼;
Changing password for user beinan.
Changing password for beinan
(current) UNIX password: 注:輸入beinan的當前密碼;
passwd: Authentication token manipulation error 注:失敗,不能更改密碼;
再來一例:
[root@localhost ~]# passwd -d beinan 注:清除beinan用戶密碼;
Removing password for user beinan.
passwd: Success 注:清除成功;
[root@localhost ~]# passwd -S beinan 注:查詢beinan用戶密碼狀態;
Empty password. 注:空密碼,也就是沒有密碼;
3、chage 修改用戶密碼有效期限的命令;
chage [-l] [-m 最小天數] [-M 最大天數] [-W 警告] [-I 失效日] [-E 過期日] [-d 最後日] 用戶
四、刪除用戶和用戶組的工具, userdel和 groupdel 介紹;
1、userdel 用法;
userdel [-r] 名稱
[root@localhost ~]# userdel bnnb 注:刪除用戶bnnb,但不刪除其家目錄及文件;
[root@localhost ~]# ls -ld /home/bnnb 注:查看其家目錄是否存在;
drwxr-xr-x 14 501 501 4096 8月 29 16:33 /home/bnnb 注:存在;
[root@localhost ~]# ls -ld /home/lanhaitun 注:查看lanhaitun家目錄是否存在;
drwx------ 4 lanhaitun lanhaitun 4096 11月 5 14:50 /home/lanhaitun 注:存在;
[root@localhost ~]# userdel -r lanhaitun 注:刪除用戶lanhaitun,其家目錄及文件一併刪除;
[root@localhost ~]# ls -ld /home/lanhaitun 注:查看是否在刪除lanhaitun 用戶的同時,也一併把其家目錄和文件一同刪除;
ls: /home/lanhaitun: 沒有那個文件或目錄 注:已經刪除;
2、groupdel的用法;
[root@localhost ~]# groupdel lanhaitun
五、修改用戶的工具介紹;
1、chfn 修改用戶信息工具;
chfn [ -f full-name ] [ -o office ] [ -p office-phone ] [ -h home-phone ] [ -u ] [ -v ] [ username ]
[root@localhost ~]# chfn beinanlinux 注:更改用戶beinanlinux的信息;
Changing finger information for beinanlinux.
Name []: BeiNan.Linux 注:用戶全名BeiNan.Linux ,隨便寫一個就行;
Office []: ChinaDL
Office Phone []: 66666666
Home Phone []: 99999999
Finger information changed. 注:更改完成;
[root@localhost ~]# finger beinanlinux
Login: beinanlinux Name: BeiNan.Linux
Directory: /home/beinanlinux Shell: /bin/bash
Office: ChinaDL Home Phone: 99999999
Last login Sat Nov 5 11:27 (CST) on tty2
No mail.
No Plan.
[root@localhost ~]# more /etc/passwd |grep beinanlinux beinanlinux:x:509:509:BeiNan.Linux,ChinaDL,66666666,99999999:/home/beinanlinux:/bin/bash
2、chsh 改變用戶的SHELL類型;
[root@localhost ~]# chsh --help
Usage: chsh [ -s shell ] [ --list-shells ] [ --help ] [ --version ] [ username ]
[root@localhost ~]# chsh --list-shells 注:列出當前系統中所有的SHELL;
/bin/sh
/bin/bash
/sbin/nologin 注:這個是不允許用戶登錄系統所用,是極爲有用的;您可以通過查看/etc/passwd 中看哪些用戶是nologin;
/bin/ksh
/bin/tcsh
/bin/csh
/bin/zsh
[root@localhost ~]# finger beinanlinux |grep Shell 注:查看用戶beinanlinux 所用的SHELL類型;
Directory: /home/beinanlinux Shell: /bin/bash 注:beinanlinux用的是bash ;
[root@localhost ~]# chsh -s /bin/ksh beinanlinux 注:更改beinanlinux所用的shell 爲ksh ;
Changing shell for beinanlinux.
Shell changed. 注:更改完成;
[root@localhost ~]# finger beinanlinux |grep Shell 注:再次查詢beinanlinux所用的SHELL;
Directory: /home/beinanlinux Shell: /bin/ksh 注:看來已經改過來了;
[root@localhost ~]# more /etc/passwd |grep nologin
3、usermod 用戶修改工具(極其強大)
usermod [-u uid [-o]] [-g group] [-G group,...]
[-d 主目錄 [-m]] [-s shell] [-c 註釋] [-l 新名稱]
[-f 失效日] [-e 過期日] [-p 密碼] [-L|-U] 用戶名
usermod 命令會參照你命令列上指定的部份修改系統帳號檔。下列爲usermod可選用的參數。
-c comment
更新用戶帳號password檔中的註解欄,一般是使用chfn(1)來修改。
-d home_dir
更新用戶新的登入目錄。如果給定-m選項,用戶舊目錄會搬到新的目錄去,如舊目錄不存在則建個新的。
-e expire_date 加上用戶帳號停止日期。日期格式爲MM/DD/YY.
-f inactive_days 帳號過期幾日後永久停權。當值爲0時帳號則立刻被停權。而當值爲-1時則關閉此功能。預設值爲-1。
-g initial_group 更新用戶新的起始登入用戶組。用戶組名須已存在。用戶組ID必須參照既有的的用戶組。用戶組ID預設值爲1。
-G group,[...] 定義用戶爲一堆groups的成員。每個用戶組使用","區格開來,不可以夾雜空白字元。用戶組名同-g選項的限制。如果用戶現在的用戶組不再此列,則將用戶由該用戶組中移除。
-l login_name 變更用戶login時的名稱爲login_name。其它不變。特別是,用戶目錄名應該也會跟着更動成新的登入名。
-s shell 指定新登入shell。如此欄留白,系統將選用系統預設shell。
-u uid用戶ID值。必須爲唯一的ID值,除非用-o選項。數字不可爲負值。預設爲最小不得小於/etc/login.defs中定義的UID_MIN值。0到UID_MIN值之間是傳統上保留給系統帳號使用。用戶目錄樹下所有的檔案目錄其userID會自動改變。放在用戶目錄外的檔案則要自行手動更動。
警告:usermod不允許你改變正在線上的用戶帳號名稱。當usermod用來改變userID,必須確認這名user沒在電腦上執行任何程序。你需手動更改用戶的crontab檔。也需手動更改用戶的at工作檔。採用NISserver須在server上更動相關的NIS設定。
[root@localhost ~]# usermod -d /opt/linuxfish -m -l fishlinux -U linuxfish
注:把linuxfish 用戶名改爲fishlinux ,並且把其家目錄轉移到 /opt/linuxfish ;
[root@localhost ~]# ls -la /opt/linuxfish/ 注:查看用戶fishlinux的家目錄下的文件及屬主;
總用量 48
drwxr-xr-x 3 fishlinux linuxfish 4096 11月 5 16:46 .
drwxrwxrwx 29 root root 4096 11月 5 16:48 ..
-rw-r--r-- 1 fishlinux linuxfish 24 11月 5 16:46 .bash_logout
-rw-r--r-- 1 fishlinux linuxfish 191 11月 5 16:46 .bash_profile
-rw-r--r-- 1 fishlinux linuxfish 124 11月 5 16:46 .bashrc
-rw-r--r-- 1 fishlinux linuxfish 5619 11月 5 16:46 .canna
-rw-r--r-- 1 fishlinux linuxfish 438 11月 5 16:46 .emacs
-rw-r--r-- 1 fishlinux linuxfish 120 11月 5 16:46 .gtkrc
drwxr-xr-x 3 fishlinux linuxfish 4096 11月 5 16:46 .kde
-rw-r--r-- 1 fishlinux linuxfish 0 11月 5 16:46 mydoc.txt
-rw-r--r-- 1 fishlinux linuxfish 658 11月 5 16:46 .zshrc
[root@localhost ~]# more /etc/passwd |grep fishlinux 注:查看有關fishlinux的記錄;
fishlinux:x:512:512::/opt/linuxfish:/bin/bash
[root@localhost ~]# usermod -p 123456 fishlinux 注:修改fishlinux的口令是123456 ;
[root@localhost ~]# more /etc/shadow |grep fishlinux 注:查詢/etc/shadow文件中fishlinux的口令;我們看到明顯是沒有加密;
fishlinux:123456:13092:0:99999:7:::
4、userinfo 圖形介面的修改工具;
[root@localhost ~]# userinfo
六、後記;
七、關於本文;
八、致謝;
九、參考文檔;
十、相關文檔;
- 由 北南南北 在 2005/11/05 - 20:29 發表