4.2- 添加新用戶 useradd詳解

useradd是添加新用戶的主要工具,此命令爲創建新用戶賬戶並同時建立用戶的HOME目錄結構提供了一種簡便的方法。

useradd使用系統默認值結合命令行參數來定義用戶賬戶,查看useradd所使用的默認值的方法:

方法一:使用 useradd -D命令

[root@hadoop ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

方法二:查看文件 /etc/dafault/useradd

[root@hadoop etc]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

如果在創建新用戶賬戶時未在命令行中指定各屬性,使用-D的默認值,說明:
* GROUP=100 新用戶將被添加到用戶組ID爲100的公共用戶組中
* HOME=/home 新用戶將在/home/loginname目錄下創建一個HOME賬戶
* INACTIVE=-1 當密碼過期時,賬戶將不會被禁用
* EXPIRE= 新賬戶不會在設定日期被設置爲逾期
* SHELL=/bin/bash 新賬戶使用 bash shell 作爲默認shell
* SKEL=/etc/skel 系統將/etc/skel目錄中的內容複製到用戶的HOME目錄
* CREATE_MAIL_SPOOL=yes 系統將在郵件目錄中創建一個文件用於用戶賬戶接收郵件

1、 SKEL=/etc/skel 詳解
/etc/skel目錄中包含以下文件,它們是bash shell環境的標準啓動文件。
系統會自動將這些默認文件複製到創建的每個用戶的HOME目錄中,將會使用/etc/skel中的文件創建一個新的HOME目錄

   [root@hadoop ~]# ls -lia /etc/skel
總用量 24
   33287 drwxr-xr-x.  3 root root   78 6月  11 13:23 .
16777281 drwxr-xr-x. 87 root root 8192 7月   7 15:01 ..
   33292 -rw-r--r--.  1 root root   18 10月 31 2018 .bash_logout
   33293 -rw-r--r--.  1 root root  193 10月 31 2018 .bash_profile
   33294 -rw-r--r--.  1 root root  231 10月 31 2018 .bashrc
17269020 drwxr-xr-x.  4 root root   39 6月  11 13:23 .mozilla

2、 useradd命令行參數詳解
也可以自定義命令行參數而不使用默認值,useradd的命令行參數如下:
在這裏插入圖片描述
在這裏插入圖片描述

3、 如何修改默認值?
使用-D參數和表示要修改的值的參數,可以修改默認的新用戶值,參數如下:
在這裏插入圖片描述

[root@hadoop home]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@hadoop home]# useradd -D -b /home_new
[root@hadoop home]# useradd -D
GROUP=100
HOME=/home_new
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

4、 用戶信息存放位置
/etc/passwd 用戶基本信息存放位置
/etc/shadow 用戶密碼存放位置,用戶名同於 /etc/passwd
/etc/group 用戶組存放位置,用戶名和用戶組ID同於 /etc/passwd
/etc/gshadow 用戶組安全位置
/etc/dafault/useradd 用戶創建的默認值,即爲 useradd -D 看到的默認值
/etc/skel 創建新用戶家目錄下的源文件,useradd -D 中查看到 /etc/skel爲SKEL文件

5、 添加用戶示例
1> 單獨使用useradd
/etc/passwd 增加用戶 dafu1
/etc/shadow 增加用戶 dafu1
/etc/group 增加用戶 dafu1
/etc/gshadow 增加用戶 dafu1

[root@hadoop home]# useradd dafu1
[root@hadoop home]# useradd dafu1
[root@hadoop home]# cat /etc/passwd | grep dafu1
dafu1:x:1000:1000::/home/dafu1:/bin/bash
[root@hadoop home]# cat /etc/shadow | grep dafu1
dafu1:!!:18132:0:99999:7:::
[root@hadoop home]# cat /etc/group | grep dafu1
dafu1:x:1000:
[root@hadoop home]# cat /etc/gshadow  | grep dafu1
dafu1:!::

2> 創建用戶指定UID -u參數

[root@hadoop etc]# useradd dafu2 -u 1100
[root@hadoop etc]# cat /etc/passwd | grep dafu2
dafu2:x:1100:1100::/home/dafu2:/bin/bash
[root@hadoop etc]# cat /etc/shadow | grep dafu2
dafu2:!!:18132:0:99999:7:::
[root@hadoop etc]# cat /etc/group | grep dafu2
dafu2:x:1100:
[root@hadoop etc]# cat /etc/gshadow | grep dafu2
dafu2:!::

3> 創建用戶指定用戶家目錄 -d參數

[root@hadoop etc]# useradd dafu3 -d /home/dafu3_new
[root@hadoop etc]# cat /etc/passwd | grep dafu3
dafu3:x:1101:1101::/home/dafu3_new:/bin/bash

4> 創建用戶指定用戶組(包括用戶組和附加組) -g參數 和 -G參數
指定的用戶組必須已經存在

[root@hadoop etc]# useadd dafu4 -g dafu4_new -G user4_new_add 
bash: useadd: 未找到命令
[root@hadoop etc]# useradd dafu4 -g dafu4_new -G user4_new_add 
useradd:“dafu4_new”組不存在
[root@hadoop etc]# useradd dafu4 -g dafu2 -G user4_new_add 
useradd:“user4_new_add”組不存在
[root@hadoop etc]# useradd dafu4 -g dafu2 -G user2 
useradd:“user2”組不存在
[root@hadoop etc]# useradd dafu4 -g dafu2 -G dafu2
[root@hadoop etc]# cat /etc/passwd | grep dafu4
dafu4:x:1102:1100::/home/dafu4:/bin/bash 
[root@hadoop etc]# cat /etc/group | grep 1100
dafu2:x:1100:dafu4  

備註:使用useradd 命令添加用戶之後,需要使用 passwd 命令設置賬戶的密碼
關於 passwd 的詳細用法見: https://blog.csdn.net/yaoyelinger0912/article/details/100056900

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章