管理用户和组(一)-建立本地用户和本地组

 首先,我们可以通过 id 来查询用户信息,比如要查询一个叫 jwang 的用户,

id jwang

会得到uid,gid 和 groups。

一般当你创建系统时,新建的那个用户会自动新建一个同名的group,并且group id 和用户id一样。

ls -l /tmp

如果你要查询文件/tmp,你能在-l命令下发现文件会属于不同的用户和组, 如 root 和 jwang。

ps au

查询进程也是一样,每一个进程都有对于的 user。

cat /etc/passwd

当你查看password信息时,你能在里面找到user name, user id, group id

cat /etc/group

可以通过上面的命令查看组信息,里面列出了各种组,以 group name, password, gid, user name, 顺序存储。

su

上面的命令可以切换到root权限用户,当你要添加用户时,必须以root权限操作。

添加和删除用户

useradd ali

这条命令给用户添加一个 user 叫做ali, 同时 也建立了一个叫 ali 的 group。这个时候 /etc/passwd 里也有了ali的信息。

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
      --extrausers              Use the extra users database
vi /etc/login.defs

在login.defs里,你可以给你以后创建的每一个user设置默认设置。但不会影响现有用户。

passwd ali

你可以通过上面的命令为刚刚创建的 ali 设置一个密码。

useradd skrrr

usrdel skrrr

我们用 userdel 可以删除用户。但是,对于新建的skrrr的home dir我们并没有移除。

userdel --help

查看文档,我们能看到,加-r就可以删掉用户 home和 mail。

Usage: userdel [options] LOGIN

Options:
  -f, --force                   force removal of files,
                                even if not owned by user
  -h, --help                    display this help message and exit
  -r, --remove                  remove home directory and mail spool
  -R, --root CHROOT_DIR         directory to chroot into
      --extrausers              Use the extra users database
  -Z, --selinux-user            remove any SELinux user mapping for the user

为了删掉刚刚的用户home和mail,我们需要再添加一遍用户,然后用-r删除:

useradd skrrr
userdel -r skrrr

添加和删除组

groupadd sales

和用户添加几乎一样,就可以添加group, 可以查看 /etc/group。

查看帮助文档,看看有哪些选项:

groupadd --help
Usage: groupadd [options] GROUP

Options:
  -f, --force                   exit successfully if the group already exists,
                                and cancel -g if the GID is already used
  -g, --gid GID                 use GID for the new group
  -h, --help                    display this help message and exit
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -o, --non-unique              allow to create groups with duplicate
                                (non-unique) GID
  -p, --password PASSWORD       use this encrypted password for the new group
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
      --extrausers              Use the extra users database
groupadd -g 3000 itdep

上面的命令会创建一个 group 叫 itdep,它的 gid 是3000。

删除一个group似乎没有那么麻烦:

groupdel --help
Usage: groupdel [options] GROUP

Options:
  -h, --help                    display this help message and exit
  -R, --root CHROOT_DIR         directory to chroot into
  -f, --force                   delete group even if it is the primary group of a user
groupdel itdep

就可以删除掉刚刚创建的group。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值