linux用户管理

我们在登录linux主机时,在输入完帐号和密码时,linux会先查找/etc/passwd文件中是否有这个帐号,如果没有则跳出,如果有的话,他会读取该帐号的user IDgroup ID同时该帐号的根目录和shell也读了出来。然后在去核对密码表,在/etc/shadow中找出我们刚刚输入的帐号和userID,核对我们输入密码是否正确。一切正确我们可以登录到当前用户shell。

1、添加新的用户账号:useradd

语法如下:
useradd 选项 用户名
其中各选项含义如下:
-c comment 指定一段注释性描述。
-d 目录 指定用户主目录,如果此目录不存在,则同时使用-m选项,能创建主目录。
-g 用户组 指定用户所属的用户组。
-G 用户组 指定用户所属的附加组。
-s Shell文件 指定用户的登录Shell。
-u 用户号 指定用户的UID(1000以下的为系统内容使用,所以必须使用1000以上的用户UID)
-p这个命令是需求提供md5码的加密口令,普通数字是不行的。

[root@rhel7 ~]# useradd --help
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

[root@rhel7 ~]# 

useradd -g root -d /home/rusky -m testuser   该命令添加用户testsuer, 指定所属组为root,指定testuser的目录为/home/rusky,-m自动创建用户登录目录。

useradd -g root -G wheel,testgroups  -d /home/rusky -m testuser  ---命令同时,-G参数指定用户同时也属于另外一个组wheel组和testgroups。

只能属于一个主组,但是可以属于多个附组。

2、用户密码管理 passwd 

用户账号刚创建时没有口令,是被系统锁定的,无法使用,必须为其指定口令后才能使用,即使是空口令。
指定和修改用户口令的Shell命令是passwd。终极用户能为自己和其他用户指定口令,普通用户只能修改自己的口令。

[root@rhel7 ~]# passwd --help
Usage: passwd [OPTION...] <accountName>
  -k, --keep-tokens       keep non-expired authentication tokens
  -d, --delete            delete the password for the named account (root only)
  -l, --lock              lock the password for the named account (root only)
  -u, --unlock            unlock the password for the named account (root only)
  -e, --expire            expire the password for the named account (root only)
  -f, --force             force operation
  -x, --maximum=DAYS      maximum password lifetime (root only)
  -n, --minimum=DAYS      minimum password lifetime (root only)
  -w, --warning=DAYS      number of days warning users receives before password expiration (root only)
  -i, --inactive=DAYS     number of days after password expiration when an account becomes disabled (root only)
  -S, --status            report password status on the named account (root only)
  --stdin                 read new tokens from stdin (root only)

Help options:
  -?, --help              Show this help message
  --usage                 Display brief usage message
[root@rhel7 ~]# 

命令的格式为:
代码:
passwd 选项 用户名
可使用的选项:
-l 锁定口令,即禁用账号。
-u 口令解锁。
-d 使账号无口令。
-f 强迫用户下次登录时修改口令。
如果默认用户名,则修改当前用户的口令。
例如:假设当前用户是sam,则下面的命令修改该用户自己的口令:
$ passwd
Old password:******
New password:*******
Re-enter new password:*******
如果是终极用户,能用下列形式指定任意用户的口令:
# passwd sam
New password:*******
Re-enter new password:*******
普通用户修改自己的口令时,passwd命令会先询问原口令,验证后再需求用户输入两遍新口令,如果两次输入的口令一致,则将这个口令指定给用户;而终极 用户为用户指定口令时,就不必知道原口令。为了安全起见,用户应该选择比较复杂的口令,最佳使用不少于8位的口令,口令中包含有大写、小写字母和数字,并且应该和姓名、生日等不相同。

为用户指定空口令时,执行下列形式的命令:
代码:
# passwd -d sam
此命令将用户sam的口令删除,这样用户sam下一次登录时,系统就不再询问口令。
passwd命令还能用-l(lock)选项锁定某一用户,使其不能登录,例如:
代码:
# passwd -l sam

3、修改用户账号-usermod

[root@rhel7 ~]# usermod --help
Usage: usermod [options] LOGIN

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
  -R, --root CHROOT_DIR         directory to chroot into
  -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
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

语法:usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G <群组>][-l <帐号名称>][-s ][-u ][用户帐号]

参数:
-c  修改用户帐号的备注文字。
-d  修改用户登入时的目录。
-e  修改帐号的有效期限。
-f   修改在密码过期后多少天即关闭该帐号。
-g  修改用户所属的群组。
-G    修改用户所属的附加群组。
-l   修改用户帐号名称。
-L  锁定用户密码,使密码无效。
-s  修改用户登入后所使用的shell
-u  修改用户ID。
-U  解除密码锁定。

例: usermod -d /home/rusky2 -m test-user 将用户test-user有主目录修改为/home/rusky2  

---------------
-a, --append           append the user to the supplemental GROUPS  mentioned by the -G option without removing him/her from other groups

usermod还有一个-a参数,用户把用户添加到-G指定的子组中。

例:usermod -aG wheel rusky    --修改用户rusky,将其添加到wheel组里。

4、用户删除 usedel

如果一个用户账号不再使用,能从系统中删除。删除用户账号就是要将/etc/passwd等系统文件中的该用户记录删除,必要时还要删除用户的主目录。删除一个已有的用户账号使用userdel命令。
userdel [-r , -f] username 

-f 表示强制删除该用户,即使该用户已经登录到linux系统中。
常用的选项是-r,他的作用是把用户的主目录一起删除。
例如:
# userdel -r sam
此命令删除用户sam在系统文件(主要是/etc/passwd,/etc/shadow,/etc/group等)中的记录,同时删除用户的主目录。

5、其它

finger命令不加任何选项时,查看当前系统已登录的用户

finger命令查看用户的基本信息
finger 用户名1 用户名2

groups命令查看用户所属组
groups 用户名1

------------------------

用户授权操作:

chmod g+w /目录 
chmod u+w /目录
chown -R rusky:root /home/ldap
chown -R rusky:root /home/apache-tomcat-6.0.37-ldap

=========

1、建用户:
adduser phpq                             //新建phpq用户
passwd phpq                               //给phpq用户设置密码

2、建工作组
groupadd test                          //新建test工作组

3、新建用户同时增加工作组
useradd -g test phpq                      //新建phpq用户并增加到test工作组

注::-g 所属组 -d 家目录 -s 所用的SHELL


4、给已有的用户增加工作组
usermod -G groupname username

或者:gpasswd -a user group

5、临时关闭:在/etc/shadow文件中属于该用户的行的第二个字段(密码)前面加上*就可以了。想恢复该用户,去掉*即可。

或者使用如下命令关闭用户账号:
passwd peter –l

重新释放:
passwd peter –u

6、永久性删除用户账号
userdel peter

groupdel peter

usermod –G peter peter   (强制删除该用户的主目录和主目录下的所有文件和子目录)

7、从组中删除用户
编辑/etc/group 找到GROUP1那一行,删除 A
或者用命令
gpasswd -d A GROUP

8、显示用户信息
id user
cat /etc/passwd

====================

 

posted on 2014-05-18 18:14  遠離塵世の方舟  阅读(394)  评论(0编辑  收藏  举报

导航