linux环境中通过useradd命令,创建用户的时候指定用户的base-dir

Posted QA-3K

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux环境中通过useradd命令,创建用户的时候指定用户的base-dir相关的知识,希望对你有一定的参考价值。

需求说明:

  今天一个同事,问了一个这样的问题,在linux环境中,创建用户的时候,默认的是在/home目录下创建一个与用户名相同的家目录,

  如何能够将这个/home更换成一个其他的,比如/opt/app下,研究了下,在此记录下

操作过程:

1.通过查看useradd命令的帮助文档,知道创建用户base_dir的配置在/etc/default/useradd文件中

[[email protected] ~]# cat /etc/default/useradd 
# useradd defaults file
GROUP=100
HOME=/home  #这个就是BASE_DIR,那么每次都是在/home下创建一个与用户名同名的家目录.
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

2.修改这个配置

[[email protected] ~]# cat /etc/default/useradd 
# useradd defaults file
GROUP=100
HOME=/opt/app   #目录已经进行修改
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

3.创建新的用户,查看用户的家目录位置

[[email protected] ~]# useradd testhome
[[email protected] ~]# cat /etc/passwd | grep testhome
testhome:x:803:803::/opt/app/testhome:/bin/bash
[[email protected] ~]# su - testhome
[[email protected] ~]$ pwd
/opt/app/testhome

备注:测试成功,新建的用户都是在/opt/app下建的目录,切换用户之后,通过pwd看到的也是相同的目录

 

useradd的帮助命令:

       -b, --base-dir BASE_DIR
           The default base directory for the system if -d HOME_DIR is not specified.  BASE_DIR is concatenated with the account name to define the home directory. The
           BASE_DIR must exist otherwise the home directory cannot be created.

           If this option is not specified, useradd will use the base directory specified by the HOME variable in /etc/default/useradd, or /home by default.

 

注意:对于不熟悉的操作,可以首先查看man获得一些基本的信息.

 

文档创建时间:2018年10月12日15:07:32

以上是关于linux环境中通过useradd命令,创建用户的时候指定用户的base-dir的主要内容,如果未能解决你的问题,请参考以下文章

linux5中创建用户,无法使用useradd命令,提示没有发现此命令,bin和sbin中都没有此命令.....

linux环境中通过/dev/urandom设备产生随机密码

一起来学习linux创建用户useradd命令

linux基本命令用户管理-useradd

Linux用户创建命令useradd

linux 命令:创建用户 useradd和adduser