欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > Ubuntu22.04 使用useradd 创建用户时,没有创建家目录时,如何手动创建家目录

Ubuntu22.04 使用useradd 创建用户时,没有创建家目录时,如何手动创建家目录

2025/5/6 21:35:26 来源:https://blog.csdn.net/qq_50247813/article/details/145630790  浏览:    关键词:Ubuntu22.04 使用useradd 创建用户时,没有创建家目录时,如何手动创建家目录

测试案例:

  1. 使用useradd不加参数创建test目录

    如下可以看出使用 useradd 创建用户的时候默认不会创建家目录

    root@local:~# useradd test
    root@local:~# id test
    uid=1001(test) gid=1001(test) groups=1001(test)
    root@local:~# cat /etc/passwd | grep test
    test:x:1001:1001::/home/test:/bin/sh
    root@local:~# ls /home/	
    

    如果使用useradd创建用户的同时并创建家目录,使用useradd -m 能自动创建家目录。

    root@local:~# userdel -r test
    userdel: test mail spool (/var/mail/test) not found
    root@local:~# id test
    id: ‘test’: no such user
    root@local:~# useradd -m test
    root@local:~# id test
    uid=1001(test) gid=1001(test) groups=1001(test)
    root@local:~# ll /home/test/
    total 20
    drwxr-x--- 2 test test 4096 Feb 14 04:49 ./
    drwxr-xr-x 4 root root 4096 Feb 14 04:49 ../
    -rw-r--r-- 1 test test  220 Jan  6  2022 .bash_logout
    -rw-r--r-- 1 test test 3771 Jan  6  2022 .bashrc
    -rw-r--r-- 1 test test  807 Jan  6  2022 .profile
    
  2. 手动创建家目录

    root@local:~# userdel -r test
    root@local:~# useradd test
    root@local:~# ls /home/
    root@local:~# mkdir /home/test
    root@local:~# cp -r /etc/skel/. /home/username
    root@local:~# chown test:test /home/test -R
    root@local:~# ll -d /home/test
    drwxr-xr-x 2 test test 4096 Feb 14 04:52 /home/test/
    
  • .bash_logout : 当用户注销(退出登录)时,bash 会执行此文件中的命令
  • .bashrc: 当用户启动一个新的终端会话时,bash 会执行此文件中的命令。它主要用于设置交互式终端会话的环境。
  • .profile: 当用户登录时(例如通过图形界面或终端登录),bash 会执行此文件中的命令。它主要用于设置登录会话的环境。

useradd 是底层命令默认不会创建家目录,使用 adduser 可以直接创建家目录,并设置密码。

root@local:~# userdel -r test
root@local:~# adduser test
Adding user `test' ...
Adding new group `test' (1001) ...
Adding new user `test' (1001) with group `test' ...
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the defaultFull Name []:Room Number []:Work Phone []:Home Phone []:Other []:
Is the information correct? [Y/n] y
root@local:~# ls /home/
test  

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词