1. 程式人生 > >linux新增用戶和刪除用戶

linux新增用戶和刪除用戶

相同 最簡 ati pam 什麽 director shel 用戶密碼 sse

新增用戶

新增用戶命令:useradd

參數:

參數

說明

-u

指定UID,也就是自定義UID

-g

知道GID,也就是初始化用戶組,/etc/passwd文件中的第四個字段。

-G

後面接用戶組的名字,也就是指定其他用戶組。給用戶指定要加入的非初始化用戶組。

-M

強制!不要創建用戶主文件夾。(系統有默認值)

-m

強制!要創建用戶主文件夾。(一般默認賬號)

-c

用戶的說明,/etc/passwd的第五個字段。

-d

指定用戶的主文件夾,一定要寫絕對路徑

-r

創建一個系統賬號,這個賬號UID

限制在1~499

-s

後跟一個shell腳本,沒有這系統默認/bin/bash。這個還是很關鍵的,在作特殊賬號時會使用。

-e

後跟日期,格式為“YYYY-MM-DD”,/etc/shadow的第八個字段。

-f

指定密碼是否失效,/etc/shadow的第七個字段。0為馬上失效-1為永遠不失效。

使用useradd命令:

技術分享

已創建一個為tiger的用戶。使用grep命令查詢相關的文件。

Tiger的UID為502.

主文件夾為/home/tiger

初始化用戶組為tiger組。

且密碼是無效。

說下useradd命令的默認設置,存放在useradd文件中/etc/default/useradd

技術分享

GROUP=100新增的用戶默認的用戶組為GID為100的user用戶組內。

剛才創建tiger用戶是發現用戶組為GID為502的tiger用戶組內。這是為什麽,因為用戶組創建機制有2種:

私有用戶組機制:系統在創建賬號是會給用戶一個相同名字的用戶組作為初始化用組。這樣新創建的賬號就是獨立的用戶獨立的用戶組。相對於權限上更安全。

公共用戶組機制:創建用戶時系統默認用戶的初始化用戶組為100的user組內,這樣每個新建的用戶都存在與user組內。這樣不安全,同一個組內的用戶就相互共享了數據。

HOME=/home:用戶創建主文件的基目錄。

INACTIVE=-1:密碼的過期設置。/etc/shadow文件裏第七字段。創建用戶後多久密碼過期,-1時永遠有效。0立即失效,其他非負數字,就是多少天後失效,例如10,就是10後失效。

EXPIRE= 賬號失效日期。/etc/shadow文件裏的第八個字段。一般不設置。

SHELL=/bin/bash:默認使用的shell程序文件名。

SKEL=/etc/skel:用戶主文件夾參考基準目錄。也就是在創建主文件夾是的模板。用戶創建時將/etc/skel裏的文件內容作為用戶主文件夾的模板,給復制到用戶的主文件夾中。當我們要在每個新建用戶裏添加一個文件夾或文件時,就可以添加到skel文件夾裏作為模板。

CREATE_MAIL_SPOOL=yes:創建用戶的mailbox。創建用戶的郵件箱。郵件箱的地址:/var/spool/mail/tiger

UID/GID與密碼參數的參考文件:/etc/login.defs

使用cat命令查看文件內容如下:

每個值的描述已經相當清楚。如果英文看不懂就去百度吧。

#

# Please note that the parameters in this configuration file control the

# behavior of the tools from the shadow-utils component. None of these

# tools uses the PAM mechanism, and the utilities that use PAM (such as the

# passwd command) should therefore be configured elsewhere. Refer to

# /etc/pam.d/system-auth for more information.

#

# *REQUIRED*

# Directory where mailboxes reside, _or_ name of file, relative to the

# home directory. If you _do_ define both, MAIL_DIR takes precedence.

# QMAIL_DIR is for Qmail

#

#QMAIL_DIR Maildir

MAIL_DIR /var/spool/mail

#MAIL_FILE .mail

# Password aging controls:

#

# PASS_MAX_DAYS Maximum number of days a password may be used.

# PASS_MIN_DAYS Minimum number of days allowed between password changes.

# PASS_MIN_LEN Minimum acceptable password length.

# PASS_WARN_AGE Number of days warning given before a password expires.

#

PASS_MAX_DAYS 99999

PASS_MIN_DAYS 0

PASS_MIN_LEN 5

PASS_WARN_AGE 7

#

# Min/max values for automatic uid selection in useradd

#

UID_MIN 500

UID_MAX 60000

#

# Min/max values for automatic gid selection in groupadd

#

GID_MIN 500

GID_MAX 60000

#

# If defined, this command is run when removing a user.

# It should remove any at/cron/print jobs etc. owned by

# the user to be removed (passed as the first argument).

#

#USERDEL_CMD /usr/sbin/userdel_local

#

# If useradd should create home directories for users by default

# On RH systems, we do. This option is overridden with the -m flag on

# useradd command line.

#

CREATE_HOME yes

# The permission mask is initialized to this value. If not specified,

# the permission mask will be initialized to 022.

UMASK 077

# This enables userdel to remove user groups if no members exist.

#

USERGROUPS_ENAB yes

# Use SHA512 to encrypt password.

ENCRYPT_METHOD SHA512

Useradd命令參考的/etc/default/userdd;/etc/skel/*;/etc/login.defs

創建好用戶後那密碼怎麽辦啊!

使用passwd命令:

參數:

參數

說明

--stdin

通過一個管道命令的數據,作為密碼:例子:echo “abcd1112” | passwd --stdin tiger

-l

Lock的意思。將/etc/shadow文件裏的第二個字段前加!

-u

Unlock的意思。

-S

列出密碼相關的參數。顯示/etc/shadow裏的相關信息。

-n

多久不可修改密碼,/etc/shadow裏的第四個字段。

-x

多久必須修改密碼,/etc/shadow 裏的第五個字段。

-w

密碼過期前幾天警告,/etc/shadow裏的第六給字段。

-i

密碼失效日期,/etc/shadow裏的第七個字段。

我們來使用下passwd:

查看密碼參數的命令chage:

參數

參數

說明

-l

列出賬號的詳細密碼參數

-d

最近一次更改密碼日期,格式“YYYY-MM-DD”,/etc/shadow的第三字段。

-E

賬號失效日期,格式YYYY-MM-DD /etc/shadow的第八字段。

-I

密碼失效日期,/etc/shadow的第七字段。

-m

密碼最短保留天數,/etc/shadow的第四字段。

-M

密碼多久修改,/etc/shadow的第五字段。

-W

密碼失效前警告日期,/etc/shadow的第六字段。

使用下chage命令:

[[email protected] etc]# chage -l tiger

Last password change : Jun 17, 2017

Password expires : never

Password inactive : never

Account expires : never

Minimum number of days between password change : 0

Maximum number of days between password change : 99999

Number of days of warning before password expires : 7

如果在創建用戶時,設置了錯誤的操作怎麽辦?

使用usermod命令來解決:

參數:

參數

說明

-c

賬號說明,/etc/passwd的第五字段

-d

賬號的主文件夾變更,/etc/passwd的第六字段

-e

賬號失效日期,/etc/shadow的第八字段

-f

密碼過期的寬限天數,/etc/shadow的第七字段

-g

初始化用戶組,/etc/passwd的第四字段,GID

-G

修改用戶的支持用戶組,/etc/group的第四個字段

-a

-G合用,添加次要用戶組的支持,但不是設置。

-l

修改賬號的名稱,/ect/passwd的第一字段

-s

修改shell腳本,後跟實際的腳本路徑。/etc/passwd的第七字段

-u

修改UID/etc/passwd的第三字段

-L

將用戶密碼鎖定,修改/etc/shadow的第二個字段。前加入!

-U

將密碼列的內容去掉

使用下usermod:

[[email protected] etc]# usermod -c "OK" tiger

[[email protected] etc]# grep tiger /etc/passwd

tiger:x:502:502:OK:/home/tiger:/bin/bash

第五列變成了OK。

刪除用戶,最簡單了。

使用userdel命令:

參數:-r 用戶的主文件夾一起刪除。

-r一般不要用,因為有時候用戶都是被禁用的,可能還需要將用戶重新恢復使用。

而且userdel也不建議使用,因為如果想讓用戶失效,直接使用失效密碼的方式就可以。

使用下userdel:

[[email protected] etc]# userdel tiger

[[email protected] etc]# grep tiger /etc/passwd

[[email protected] etc]#

刪除用戶後,就已經在/etc/passwd文件中找不到tiger的信息。

linux新增用戶和刪除用戶