【原创】Linux 使用 useradd 创建用户

概要

1、简介

Linux 创建用户是常用到的场景之一,对于 Linux 系统来说,一般创建一个用户还需要创建用户的家目录,设置用户的密码,设置用户的 shell 类型等。

本文将简单介绍使用 useradd 创建用户的常见用法。

 

2、操作环境

Ubuntu 18.04.5
Bash 4.4.20

3、注意事项

创建用户必须是 root 用户或者具有 sudo 权限的用户。下文均使用具有 sudo 权限的用户操作。

正文

1、创建用户

$ sudo useradd test -U -m -s /bin/bash

其中:

  • -U(--user-group)无参选项表示创建一个与用户名相同的用户组,这里就是 test 用户组。

  • -m(--create-home)无参选项表示创建用户的 home 目录( 如这里的 Linux 默认是 /home/test)

  • -s (--shell SHELL) 表示设置该用户的 shell 类型,比如:/bin/bash,/bin/sh,/bin/zsh 等。

2、设置用户密码

$ sudo passwd test
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

3、查看用户相关信息

基本信息

➜  ~ id test   
uid=1001(test) gid=1001(test) groups=1001(test)

家目录 /home/test

➜  ~ ll /home    
total 4.0K
drwxr-xr-x  2 test    test    4.0K Oct 25 08:56 test

/etc/passwd 配置

$ cat /etc/passwd
...
test:x:1001:1001::/home/test:/bin/bash

查看创建的 group

$ cat /etc/group
...
test:x:1001:

4、设置 sudo 权限

如何用户需要 sudo 权限,可使用如下方法设置

$ sudo usermod test -G sudo
$ id test
uid=1001(test) gid=1001(test) groups=1001(test),27(sudo)

5、切换到创建的用户

$ su - test                
Password: 
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

6、运行 sudo 命令

test@ubuntu:~$ sudo apt update
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Get:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease [88.7 kB]       
...
Get:14 http://mirrors.aliyun.com/ubuntu bionic-backports/universe amd64 DEP-11 Metadata [9,288 B]

7、删除用户

如果你需要删除用户,也非常简单,只需要使用 userdel 命令即可。

$ sudo userdel test

注:此命令会删除 test 用户,test 用户组,但不会删除 /home/test 的目录。

附录

useradd 命令用法:

$ 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

You may also like...

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注