当前位置: 首页 > article >正文

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 default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y
root@local:~# ls /home/
test  

http://www.kler.cn/a/546536.html

相关文章:

  • 2.14寒假
  • 使用DeepSeek建立一个智能聊天机器人0.08
  • HDFS核对迁移的历史数据是否正确
  • Go语言扩展包x/sync使用指南
  • 详解Cookie和Session
  • Windchill 成套的解决方案
  • DeepSeek接入网络安全领域,AI高效驱动,重新定义网络防御边界!
  • Vim 退出编辑模式
  • 2月14日情人节,致挚爱
  • Linux进阶——selinux
  • 【Linux网络编程】华为云开放端口号
  • Django ORM:外键字段的命名与查询机制解析
  • 推荐的、好用的线性稳压器
  • SQL联合查询
  • CRMEB 多商户版v3.0.1源码全开源+PC端+Uniapp前端+搭建教程
  • VoIP之音视频会议中的混音技术
  • 2025-2-14算法打卡
  • Java--IO流详解(中)--字节流
  • C++ Primer 函数基础
  • 网络编程(tcp线程池)