linux 添加用户

管理使用者

(重定向自Add user)

 

目录

 [隐藏]

新增使用者

大部份 Unix/Linux 系统都是使用 /etc/passwd 档案来存放所有使用者的资讯。此档案是一个格式非常简单的文字档案,对大部份 Unix/Linux 您甚至可以直接使用一般的文字编辑器直接编辑此档案来增减使用者。当然为安全和方便起见,我们一般都会使用系统提供的工具去进行使用者管理的工作而非直接编辑此档案。大部份 GNU/Linux 都提供 useradd 或 adduser 命令来进行新增使用者的工作。留意不同 GNU/Linux 中的 useradd 和 adduser 命令表现会有分别。

adduser (Debian/Ubuntu)

Debian 系 GNU/Linux (包括 Ubuntu, Linux Mint 等) 的系统管理员最好使用 adduser 命令来新增使用者,因为它们的 adduser 提供较佳的接口。在超级使用者 root 身份下输入命令 adduser 新使用者名称:

# adduser johndoe
Adding user `johndoe' ...
Adding new group `johndoe' (1003) ...
Adding new user `johndoe (1003) with group `johndoe ...
Creating home directory `/home/johndoe' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 输入新密码
Retype new UNIX password: 重新输入密码
passwd: password updated successfully
Changing the user information for johndoe
Enter the new value, or press ENTER for the default
	Full Name []: John Doe
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] y

adduser 会自动编排使用者识别码 (user identifer, UID,如例子中的 1003)、和使用者名称同名的缺省使用者群组 (user group) 以及家目录 (home directory, 如例子中的 /home/johndoe) 给新使用者,并要求设定新使用者的密码、全名、办公室编号、办公室电话号码、住宅电话号码等资讯。除密码外,其他资料都可以漏空。基于安全和私隐问题,现代一般人都不会填上办公室编号、电话号码等资讯。

useradd (CentOS/Fedora/RHEL)

Red Hat 系 GNU/Linux 的系统管理员主最好使用命令 useradd 来新增使用者。在超级使用者 root 身份下输入命令 useradd 新使用者名称:

# useradd johndoe

如果没有任何错误讯息,即表示已成功新增新使用者了。useradd 会自动编排使用者识别码 (user identifer, UID,如例子中的 1003)、和使用者名称同名的缺省使用者群组 (user group) 以及家目录 (home directory, 如例子中的 /home/johndoe) 给新使用者。留意这个新使用者并没有设定密码,任何人都无法登入。您需要使用命令 password 使用者名称 设定密码

useradd (SuSE/OpenSuSE)

OpenSuSE 同样使用命令 useradd,不过缺省不会建立家目录。要建立家目录需要加入选项 -m,例如:

# useradd -m johndoe

和 Red Hat 系一样,useradd 不会设定密码,您仍要使用命令 password 使用者名称 设定密码,使用者才可以登入。

# passwd johndoe
Changing password for user cent.
New Password: 输入新密码
Reenter New Password: 重新输入密码
Password changed.
# 

adduser (FreeBSD)

FreeBSD 的系统管理员最好使用 adduser 命令来新增使用者,因为它们的 adduser 提供较佳的接口。在超级使用者 root 身份下输入命令 adduser:

# adduser
Username: johndoe
Full name: John Doe
Uid (Leave empty for default):
Login group [johndoe]:
Login group is johndoe. Invite johndoe into other groups? []: wheel
Login class [default]:
Shell (sh csh tcsh zsh nologin) [sh]: tcsh
Home directory [/home/johndoe]:
Use password-based authentication? [yes]:
Use an empty password? (yes/no) [no]:
Use a random password? (yes/no) [no]:
Enter password:
Enter password again:
Lock out the account after creation? [no]:
Username   : johndoe
Password   : ****
Full Name  : John Doe
Uid        : 1003
Class      :
Groups     : johndoe wheel
Home       : /home/johndoe
Shell      : /usr/local/bin/zsh
Locked     : no
OK? (yes/no): yes
adduser: INFO: Successfully added (johndoe) to the user database.
Add another user? (yes/no): no
Goodbye!
#

adduser 会自动编排使用者识别码 (user identifer, UID,如例子中的 1003)、和使用者名称同名的缺省使用者群组 (user group) 以及家目录 (home directory, 如例子中的 /home/johndoe) 给新使用者。除密码外,其他资料都可以漏空。

删除使用者

userdel (GNU/Linux)

在超级使用者 root 身份下输入命令 userdel 使用者名称

userdel johndoe

如果没有任何错误讯息,即表示已成功删除使用者了。

rmuser (FreeBSD)

在超级使用者 root 身份下输入命令 rmuser 使用者名称

# rmuser johndoe
Matching password entry:
johndoe:*:1003:1003::0:0:John Doe:/home/johndoe:/usr/local/bin/zsh
Is this the entry you wish to remove? y
Remove user's home directory (/home/johndoe)? y
Updating password file, updating databases, done.
Updating group file: trusted (removing group johndoe -- personal group is empty) done.
Removing user's incoming mail file /var/mail/johndoe: done.
Removing files belonging to johndoe from /tmp: done.
Removing files belonging to johndoe from /var/tmp: done.
Removing files belonging to johndoe from /var/tmp/vi.recover: done.
#

如果没有任何错误讯息,即表示已成功删除使用者了。

更改使用者密码 (passwd)

要更改使用者密码可以使用命令 passwd。一般使用者输只可以更改自己帐户的密码,而超级使用者 root 则可以更改任何使用者的密码。当一般使用者输入命令 passwd 后,passwd 首先会要求使用者输入当前的密码以确认身份,然后让输入两次新密码来确认新密码。留意输入新旧密码时,Unix/Linux 不会像现代一般软件每按一键显示星号 (“*”),请不要担心。

$ passwd
Changing password for user johndoe.
Current UNIX password: 输入目前密码
New Password: 输入新密码
Reenter New Password: 重新输入密码
Password changed.
$

超级使用者 root 可以在 passwd 后加上使用者名称设定或更改任何使用者的密码 (和一般使用者不同,passwd 自然要会问该使用者目前的密码):

passwd johndoe
Changing password for user johndoe.
New Password: 输入新密码
Reenter New Password: 重新输入密码
Password changed.
#

更改使用者资料

设定使用者全名 (chfn)

您可以使用命令 chfn 更改自己帐户的使用者全名、办公室编号、办公室电话号码、住宅电话号码等资讯:

chfn 使用者 更改使用者全名、办公室编号、办公室电话号码、住宅电话号码等资讯:

$ chfn
Changing the user information for johndoe
Enter the new value, or press ENTER for the default
	Full Name [John Doe]: Dr. John Doe
	Room Number []: 1302
	Work Phone []: 413 6823
	Home Phone []: 
	Other []: 

超级管理员root 可以在 chfn 后加上使用者名称更改任何使用者的资料:

chfn johndoe

亦可以使用选项 -f 使用者全名-r 办公室编号-w 办公室电话号码-h 住宅电话号码 和 -o 其他资讯 直接在命令上指定要更改的资料,较适合在 shell script 中使用:

chfn -f "Dr. John Doe" -r 1302 -w "413 6823" johndoe

此外,命令 usermod -c 全名 使用者 亦可以有相同的作用:

usermod -c "Dr. John Doe, 1302, 413 6823" johndoe

chpass (FreeBSD)

FreeBSD 一般使用 chpass 命令更改使用者资料。一般使用者使用 chpass 时只可以更改自己帐户的 shell、全名、办公室编号、办公室电话号码和住宅电话号码等资料。chpass 会弹出文字编辑器并在当中显示可以更改的帐户资料,让使用者在文字编辑器中编辑帐户资料:

#Changing user database information for jru.
Shell: /usr/local/bin/zsh
Full Name: J. Random User
Office Location:
Office Phone:
Home Phone:
Other information:

离开文字编辑器时 chpass 会要求使用者输入密码确认,然后更改相关资料。

超级使用者 root 可以在 chpass 后加上使用者名称更改任何使用者的资料:

chpass johndoe

除了一般资料,chpass 让超级使用者更改包括密码、使用者识别编号 (UID)、首要群组等重要资料。

#Changing user database information for jru.
Login: johndoe
Password: *
Uid [#]: 1003
Gid [# or name]: 1003
Change [month day year]:
Expire [month day year]:
Class:
Home directory: /home/johndoe
Shell: /usr/local/bin/zsh
Full Name: John Doe
Office Location:
Office Phone:
Home Phone:
Other information:
posted @ 2014-12-06 16:20  清晰-模块-组合-优化  阅读(518)  评论(0编辑  收藏  举报