用户和组管理类常用命令

1、groupadd : create a new group

            groupadd [options] group

             -g, --gid GID :创建用户组时指定gid

             -r, --system: Create a system group.

2、goupdel:delete a group        

             groupdel [options] GROUP

3、useradd: create a new user or update default new user information

            useradd [options] LOGIN_NAME

           useradd -D

           useradd -D [options]

            常用选项:

            -c, --comment COMMENT:添加用户描述

            -d, --home-dir HOME_DIR:设置用户家目录

            -e, --expiredate EXPIRE_DATE:设置用户过期时间,格式:YYYY-MM-DD

            -g, --gid GROUP:设置用户主组

            -G, --groups GROUP1[,GROUP2,...[,GROUPN]]],指定用户的附加组,多个组之间用逗号

                    分割

            -r, --system :     Create a system account. 创建系统用户

            -s, --shell SHELL:指定用户默认shell

            -u, --uid UID: 指定用户的uid

4、passwd: update user's authentication tokens

            passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin] [username]

            -l, --lock : 锁定用户密码

            -u, --unlock :解锁用户密码

            -d, --delete :清空用户密码

            -e, --expire : 设置用户密码过期日期,Available to  root only.

            -n, --minimum DAYS , 设置用户密码最短期限,Available to  root only.

            -x, --maximum DAYS ,设置用户密码最长期限,Available to  root only.

            -w, --warning DAYS,设置用户密码告警期限,Available to  root only.

 5、userdel:  delete a user account and related files

            userdel [options] LOGIN_NAME

             -r, --remove , 删除用户时一并删除其家目录           

6、usermod: modify a user account

            usermod [options] LOGINNAME

            -u, --uid UID

            -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]:修改用户的附加组;原来的附加组将被覆盖;可与-a 结合使用,则表示给用户追加新的附加组

            -a, --append,给用户添加新组. 只与 -G 选项一起使用

            -c, --comment COMMENT:修改用户描述

            -d, --home HOME_DIR:修改用户家目录,但家目录中的文件不会移动,此时结合-m 实现;

            -m, --move-home:只能与-d 选项一同使用,用于将原来的家目录中文件移动到新的家目录中

            -s, --shell SHELL:修改用户的默认shell

            -L, --lock:Lock a user's password. This puts a '!' in front of the encrypted password

            -U, --unlock: Unlock a user's password. This removes the '!' in front of the encrypted password. 

            -l, --login NEW_LOGIN:修改用户名

            -f, --inactive INACTIVE

            -e, --expiredate EXPIRE_DATE

7、gpasswd:  administer /etc/group and /etc/gshadow

        -a, --add user

          Add the user to the named group.

        -d, --delete user

          Remove the user from the named group.

      -r, --remove-password: 清空用户组密码,只在使用newgrp 临时切换到一个组时。

          Remove the password from the named group. The group password will be empty. Only group members will be allowed to use newgrp to join the named group.

      -M, --members user,...

          Set the list of group members.

8、 id : print real and effective user and group IDs/显示用户真实有效的ID

            id [OPTION]... [USER]

            -g, --group,print only the effective group ID , 只显示有效的gid

            -G, --groups , print all group IDs,显示所有的用户组,包括主组和附加组

            -n, --name , print a name instead of a number, for -ugG ,显示用户组的名字而不是id

            -u, --user , print only the effective user ID , 只显示有效的uid

示例:

(1)、创建组distro,其GID为2016;

            >>>groupadd -g 2016 distro

           >>> tail -1 /etc/group

                    distro:x:2016:

  (2)、创建用户mandriva, 其ID号为1005;基本组为distro;

            >>>useradd mandriva -u 1005 -g distro

            >>>tail -1 /etc/passwd

            >>>mandriva:x:1005:2016::/home/mandriva:/bin/bash

  (3)、创建用户mageia,其ID号为1100,家目录为/home/linux;

            >>>useradd -u 1100 -d /home/linux mageia

            >>>tail -1 /etc/passwd

            >>>mageia:x:1100:1100::/home/linux:/bin/bash

  (4)、给用户mageia添加密码,密码为mageedu;

            >>>echo "mageedu" | passwd --stdin mageia

                    Changing password for user mageia.

                    passwd: all authentication tokens updated successfully.

  (5)、删除mandriva,但保留其家目录;

           >>> userdel mandriva

          >>> ll /home/mandriva -d

            drwx------. 2 1005 distro 62 Aug 30 17:47 /home/mandriva

        >>> id mandriva

            id: mandriva: no such user

  (6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;

        >>>groupadd peguin

        >>>useradd slackware -u 2002 -g distro -G peguin

        >>>tail -1 /etc/passwd

                slackware:x:2002:2016::/home/slackware:/bin/bash

         >>>tail -1 /etc/group

                peguin:x:5003:slackware

  (7)、修改slackware的默认shell为/bin/tcsh;

         >>>usermod -s /bin/tcsh slackware

        >>> tail -1 /etc/passwd

            slackware:x:2002:2016::/home/slackware:/bin/tcsh

  (8)、为用户slackware新增附加组admins;

         >>>groupadd admins

                usermod -G admins slackware

        >>>tail -1 /etc/group

                admins:x:5004:slackware

你可能感兴趣的:(用户和组管理类常用命令)