一、用戶:
在創建用戶時,需要為新建用戶指定一用戶組,如果不指定其用戶所屬的工作組,自動會生成一個與用戶名同名的工作組。創建用戶user1的時候指定其所屬工作組users,例:useradd –g users user1
1、使用命令 useradd 創建用戶
例:
useradd user1——創建用戶user1
useradd –e 12/30/2009 user2——創建user2,指定有效期2009-12-30到期
用戶的缺省UID從500向后順序增加,500以下作為系統保留賬號,可以指定UID,
例:useradd –u 600 user3
2、使用 passwd 命令為新建用戶設置密碼
例:passwd user1
注意:沒有設置密碼的用戶不能使用。
3、命令 usermod 修改用戶賬戶
例:將用戶 user1的登錄名改為 u1,
usermod –l u1 user1
1
例:將用戶 user1 加入到 users組中,
usermod –g users user1
1
例:將用戶 user1 目錄改為/users/us1
usermod –d /users/us1 user1
1
4、使用命令 userdel 刪除用戶賬戶
例:刪除用戶user2
userdel user2
1
例:刪除用戶 user3,同時刪除他的工作目錄
userdel –r user3
1
5、查看用戶信息
id命令查看一個用戶的UID和GID, 例:查看user4的id
id user4
finger命令 ——可以查看用戶的主目錄、啟動shell、用戶名、地址、電話等信息
例:finger user4
二、用戶組:
6、命令 groupadd創建用戶組
groupadd –g 888 users
1
創建一個組users,其GID為888
7、命令 gpasswd為組添加用戶
只有root和組管理員能夠改變組的成員:
例:把 user1加入users組
gpasswd –a user1 users
1
例:把 user1退出users組
gpasswd –d user1 users
1
8、命令groupmod修改組
groupmod –n user users 修改組名user為users
9、groupdel刪除組
groupdel users 刪除組users
三、命令詳解:
◆ useradd命令
1. 作用
useradd命令用來建立用戶帳號和創建用戶的起始目錄,使用權限是超級用戶。
2. 格式
useradd [-d home] [-s shell] [-c comment] [-m [-k template]] [-f inactive] [-e expire ] [-p passwd] [-r] name
1
3. 主要參數
-c:加上備注文字,備注文字保存在passwd的備注欄中。
-d:指定用戶登入時的啟始目錄。
-D:變更預設值。
-e:指定賬號的有效期限,缺省表示永久有效。
-f:指定在密碼過期后多少天即關閉該賬號。
-g:指定用戶所屬的群組。
-G:指定用戶所屬的附加群組。
-m:自動建立用戶的登入目錄。
-M:不要自動建立用戶的登入目錄。
-n:取消建立以用戶名稱為名的群組。
-r:建立系統賬號。
-s:指定用戶登入后所使用的shell。(-s 后面填寫此用戶登錄后使用的shell種類的路徑,shell在/bin目錄下一般有/bin/sh 、 /bin/bash 、 /bin/ksh 、/bin/tcsh、/bin/zsh ;shell是用戶與系統溝通的接口,各種不同的shell只是命令語法有所不同而已。)
-u:指定用戶ID號。
4. 說明
useradd可用來建立用戶賬號,它和adduser命令是相同的。賬號建好之后,再用passwd設定賬號的密碼。使用useradd命令所建立的賬號,實際上是保存在/etc/passwd文本文件中。
應用實例
建立一個新用戶賬戶,並設置ID:
#useradd caojh -u 544
1
需要說明的是,設定ID值時盡量要大於500,以免沖突。因為Linux安裝后會建立一些特殊用戶,一般0到499之間的值留給bin、mail這樣的系統賬號。
◆ groupadd命令
1. 作用
groupadd命令用於將新組加入系統。
2. 格式
groupadd [-g gid] [-o]] [-r] [-f] groupname
1
3. 主要參數
-g gid:指定組ID號。
-o:允許組ID號,不必惟一。
-r:加入組ID號,低於499系統賬號。
-f:加入已經有的組時,發展程序退出。
4. 應用實例
建立一個新組,並設置組ID加入系統:
#groupadd -g 344 cjh
1
此時在/etc/passwd文件中產生一個組ID(GID)是344的項目。
Linux添加用戶(user)到用戶組(group)
將一個用戶添加到用戶組中,千萬不能直接用:
usermod -G groupA
這樣做會使你離開其他用戶組,僅僅做為 這個用戶組 groupA 的成員。
應該用 加上 -a 選項:
usermod -a -G groupA user
(FC4: usermod -G groupA,groupB,groupC user)
-a 代表 append, 也就是 將自己添加到 用戶組groupA 中,而不必離開 其他用戶組。
命令的所有的選項,及其含義:
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
查看用戶所屬的組使用命令:
$ groups user
或者查看文件:
$ cat /etc/group
原文鏈接:https://blog.csdn.net/yajie_china/java/article/details/80745346