欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > linux >内容正文

linux

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

发布时间:2025/3/19 linux 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 linux环境中通过useradd命令,创建用户的时候指定用户的base-dir 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

需求说明:

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

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

操作过程:

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

[root@testvm01 ~]# 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.修改这个配置

[root@testvm01 ~]# 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.创建新的用户,查看用户的家目录位置

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

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

 

useradd的帮助命令:

-b, --base-dir BASE_DIRThe 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. TheBASE_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

转载于:https://www.cnblogs.com/chuanzhang053/p/9778239.html

总结

以上是生活随笔为你收集整理的linux环境中通过useradd命令,创建用户的时候指定用户的base-dir的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。