在 Linux 系统中,与用户管理有关的文件主要有如下几个:分别是/etc/passwd,/etc/shadow,/etc/gfoup,/etc/gshadow 。它们分别与用户的账号,密码,用户组及用户组密码有关。我们创建用户也就是跟这几个文件打交道。
主要是下面这两个步骤:
一. 通过useradd命令完成一个新用户的初始化设置工作
二. 通过passwd为这个新用户设置密码
给系统添加一个用户叫wirelessqa,密码为12345
1. 先添加用户wirelessqa
bixiaopeng@ubuntu:~$ useradd wirelessqa useradd: cannot lock /etc/passwd; try again later.
#oh,my god,需要root权限
bixiaopeng@ubuntu:~$ su Password:
再试一次添加用户wirelessqa
root@ubuntu:/home/bixiaopeng# user add wirelessqa
没有任何结果说明是没错误
2. 为wirelessqa设置密码
root@ubuntu:/home/bixiaopeng# passwd wirelessqa Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
上面说明密码设置成功了,当然我们也可以为上面的用户修改密码,方法是一样的
此时虽然我们已经新建了帐号,但是在home目录下是看不到的
root@ubuntu:/home# pwd /home root@ubuntu:/home# ls -al total 12 drwxr-xr-x 3 root root 4096 8��� 17 00:31 . drwxr-xr-x 23 root root 4096 6��� 17 18:14 .. drwxr-xr-x 29 bixiaopeng bixiaopeng 4096 8��� 16 19:45 bixiaopeng
3. 如果想同时在home目录(默认的)创建用户目录,需要用到 -m参数
root@ubuntu:/home# useradd -m bilaoshi root@ubuntu:/home# passwd bilaoshi Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully root@ubuntu:/home# ls -al total 16 drwxr-xr-x 4 root root 4096 8��� 17 00:38 . drwxr-xr-x 23 root root 4096 6��� 17 18:14 .. drwxr-xr-x 2 bilaoshi bilaoshi 4096 8��� 17 00:38 bilaoshi drwxr-xr-x 29 bixiaopeng bixiaopeng 4096 8��� 17 00:34 bixiaopeng
useradd -d /home/account alex3
useradd -g bilaoshi alex还可以使用-G参数使他同时加入多个组
useradd -G bilaoshi1,bilaoshi2 alex
root@ubuntu:/home/bixiaopeng# grep 888 /etc/passwd root@ubuntu:/home/bixiaopeng# useradd -u 888 -g bilaoshi alex root@ubuntu:/home/bixiaopeng# grep alex /etc/passwd /etc/shadow /etc/group /etc/passwd:alex:x:888:1002::/home/alex:/bin/sh /etc/shadow:alex:!:15934:0:99999:7:::
root@ubuntu:/home/bixiaopeng# useradd -r alex2 root@ubuntu:/home/bixiaopeng# grep alex2 /etc/passwd /etc/shadow /etc/group /etc/passwd:alex2:x:999:999::/home/alex2:/bin/sh /etc/shadow:alex2:!:15934:::::: /etc/group:alex2:x:999:
root@ubuntu:/home/bixiaopeng# cat /etc/default/useradd # Default values for useradd(8) # # The SHELL variable specifies the default login shell on your # system. # Similar to DHSELL in adduser. However, we use "sh" here because # useradd is a low level utility and should be as general # as possible SHELL=/bin/sh //老毕注:默认使用的shell # # The default group for users # 100=users on Debian systems # Same as USERS_GID in adduser # This argument is used when the -n flag is specified. # The default behavior (when -n and -g are not specified) is to create a # primary user group with the same name as the user being added to the # system. # GROUP=100 //老毕注:默认的用户组 # # The default home directory. Same as DHOME for adduser # HOME=/home //老毕注:账户目录地址 # # The number of days after a password expires until the account # is permanently disabled # INACTIVE=-1 //老毕注:密码过期的宽限时间,对应 /etc/shadow 的第七栏 # # The default expire date # EXPIRE= //老毕注:账号失效日期,对应 /etc/shadow 的第八栏 # # The SKEL variable specifies the directory containing "skeletal" user # files; in other words, files such as a sample .profile that will be # copied to the new user's home directory when it is created. # SKEL=/etc/skel //老毕注:用户家目录里面的内容参照文件(里面基本上全为隐藏文件——>以“.”开头的文件) # # Defines whether the mail spool should be created while # creating the account # CREATE_MAIL_SPOOL=yes //老毕注:建立使用者的mailbox
root@ubuntu:~# userdel wirelessqa
oot@ubuntu:/home/account# cat /etc/passwd root:x:0:0:root:/root:/bin/bash …….. saned:x:114:123::/home/saned:/bin/false bixiaopeng:x:1000:1000:bixiaopeng,,,:/home/bixiaopeng:/bin/bash bilaoshi:x:1002:1002::/home/bilaoshi:/bin/sh alex:x:888:1002::/home/alex:/bin/sh alex2:x:999:999::/home/alex2:/bin/sh
root@ubuntu:/home/account# cat /etc/group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:bixiaopeng …... pulse-access:x:120: utempter:x:121: rtkit:x:122: saned:x:123: bixiaopeng:x:1000: sambashare:x:124:bixiaopeng bilaoshi:x:1002: alex2:x:999:
useradd -g bilaoshi bilaoshi在/etc/group文件里bilaoshi的组员列表将不显示这个组员,而只是在passwd文件中的GID被设置为1002。而使用如下命令:
root@ubuntu:/home/account# usermod -G bilaoshi,alex2 bilaoshi root@ubuntu:/home/account# cat /etc/group root:x:0: …. utempter:x:121: rtkit:x:122: saned:x:123: bixiaopeng:x:1000: sambashare:x:124:bixiaopeng bilaoshi:x:1002:bilaoshi alex2:x:999:bilaoshi