添加用户 useradd

本文详细介绍了Linux中useradd命令的用途,包括新建用户、设置主目录、指定用户组、用户ID等,并提供了多个使用示例,如创建普通用户、指定主组、附加组、用户ID、家目录、系统用户以及指定Shell等。
摘要由CSDN通过智能技术生成

一、命令详解


1.命令说明

Linux 系统中,可以使用 useradd 命令新建用户,同时创建新用户的家目录。默认情况下,还将为新用户创建一个组。


2.语法格式

useradd       [option]      [username]
useradd       [选项]         [用户名]

3.选项描述

  -b, --base-dir BASE_DIR       base directory for the home directory of the new account             
                                #新帐户的主目录的基本目录
  -c, --comment COMMENT         GECOS field of the new account
                                #新建帐户的GECOS字段,加上备注。备注保存在passwd的备注栏位中;
  -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
                                #覆盖/etc/login.defs默认值
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
                                #不要将用户添加到lastlog和faillog数据库
  -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
                                #允许创建具有重复(非唯一)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
                                #指定用户登入后所使用的shell
  -u, --uid UID                 user ID of the new account
                                #指定用户id
  -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               
  	                            #为SELinux用户映射使用特定的SEUSER        

二、命令示例

查询用户: id 用户名
修改密码: passwd 用户名
添加组: groupadd 组名 组名用字母,不要用数字

案例1:创建普通用户 useradd

[root@centos7 home]#useradd nannannan
[root@centos7 home]#ls /home
cyan  gougou  hr02   nannannan  test1    wangcai  xulei

[root@centos7 home]#cat /etc/passwd | tail -1
nannannan:x:3011:3015::/home/nannannan:/bin/bash

[root@centos7 home]#grep nannannan /etc/passwd
nannannan:x:3011:3015::/home/nannannan:/bin/bash

Linux默认情况创建用户,会创建同名组 (主组).


案例2:添加用户指定主组 useradd -g

[root@centos7 home]#useradd zhangsansan -g zu1
[root@centos7 home]#id zhangsansan
uid=3013(zhangsansan) gid=1006(zu1) groups=1006(zu1)

可以看到新建的用户zhangsansan的主组变成zu1。

案例3:添加用户指定附件组 useradd -G

[root@centos7 home]#useradd zhangsi -G zuzu
[root@centos7 home]#id zhangsi
uid=3014(zhangsi) gid=3017(zhangsi) groups=3017(zhangsi),3016(zuzu)

可以看到新建的用户zhangsi 的附加组,增加了zuzu。

案例4:创建用户指定id useradd -u

需要说明的是,设定ID值时要大于1000,以免冲突。因为Linux安装后会建立一些特别用户,一般0-499之间的值留给系统账号,500-100是预留账号。

[root@centos7 home]#useradd zhangwu -u 3333
[root@centos7 home]#id zhangwu
uid=3333(zhangwu) gid=3333(zhangwu) groups=3333(zhangwu)

案例5:创建用户指定家目录 useradd -d

[root@centos7 ~]#useradd zhangliu -d /ddd
[root@centos7 ~]#ls /
app  boot  ddd  dir1  home  lib64  mnt    proc  run   srv   testdir1  usr
[root@centos7 home]#cat /etc/passwd |grep zhangliu
zhangliu:x:3334:3334::/ddd:/bin/bash

创建用户指定目录,没有则会自动创建。


案例6:创建一个系统用户 useradd -r

创建一个系统用户,UID小于1000。
由于是系统用户,没有家目录。

[root@centos7 ~]#useradd  -r  zhangqi
[root@centos7 ~]#id zhangqi
uid=988(zhangqi) gid=982(zhangqi) groups=982(zhangqi)
[root@centos7 ~]#ll -d  /home/zhangqi
ls: cannot access /home/zhangqi: No such file or directory
[root@centos7 ~]#ll -d  /home/zhangsi
drwx------. 3 zhangsi zhangsi 78 Mar  4 15:34 /home/zhangsi

案例7:创建用户指定shell useradd -s

创建用户时指定shell环境,默认bash

[root@centos7 ~]#useradd zhangba -s /bin/sh
[root@centos7 ~]#cat /etc/passwd |grep zhangba
zhangba:x:3335:3335::/home/zhangba:/bin/sh
[root@centos7 ~]#cat /etc/passwd |grep zhangqi
zhangqi:x:988:982::/home/zhangqi:/bin/bash

案例8:创建用户不要加目录 useradd -M

[root@centos7 ~]#useradd zhangjiu -M
[root@centos7 ~]#ll -d /home/zhangjiu
ls: cannot access /home/zhangjiu: No such file or directory

案例9:查看创建用户时的默认值 useradd -D

[root@centos7 home]#useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=ye

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值