How can I retrospectively create a default home directory for an existing user in terminal?

I created a user without a home directory and now I want to create a home directory for them. Not just a folder called /home/new-user, but a complete default home directory with all the normal folders and hidden files, etc.

How can I do that?


Solution 1:

Use the following (as root, or with sudo if not root):

mkhomedir_helper username

For this to work, folder /home/username must not exist.

For X-related folders (Desktop, Downloads, etc), you will need to login in a graphics environment; they will be automatically generated the first time you login.

Solution 2:

The subdirectories (Documents, Downloads, etc...) are automatically created when the user first logs in through GNOME, provided that the home directory is created with the correct permissions. Here's a demonstration:

alaa@aa-lu:~$ sudo useradd testinguser
alaa@aa-lu:~$ sudo passwd testinguser
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
alaa@aa-lu:~$ sudo ls -l /home
total 20
drwxr-xr-x 55 alaa alaa  4096 Aug 22 22:00 alaa
drwx------  2 root root 16384 Jun  5 09:46 lost+found
alaa@aa-lu:~$ sudo mkdir /home/testinguser
alaa@aa-lu:~$ sudo chown testinguser:testinguser /home/testinguser
alaa@aa-lu:~$ ls -l /home
total 24
drwxr-xr-x 55 alaa        alaa         4096 Aug 22 22:00 alaa
drwx------  2 root        root        16384 Jun  5 09:46 lost+found
drwxr-xr-x  2 testinguser testinguser  4096 Aug 23 10:03 testinguser
alaa@aa-lu:~$ ls -l /home/testinguser/
total 0
alaa@aa-lu:~$

You can check that the user's home directory is correctly set by checking the entry in /etc/passwd. You should, by default, see the home directory set to /home/testinguser:

alaa@aa-lu:~$ grep testinguser /etc/passwd
testinguser:x:1001:1001::/home/testinguser:/bin/sh

If you don't see the home directory /home/testinguser there, you'll need to execute the command sudo usermod -d /home/testinguser testinguser to update it, although you should not need to use this command because it should be set by default (according to useradd's manpages).

I then logged out of my account, and logged back in with testinguser, and here are the subdirectories automatically created:

alaa@aa-lu:~$ ls -l /home/testinguser/
total 36
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Desktop
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Documents
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Downloads
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Music
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:07 Pictures
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Public
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Templates
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Videos

I didn't need to copy the contents of /etc/skel.

If possible, can you please try following these steps, creating another new user? Once you're done, you can remove this new user by sudo deluser testinguser && sudo rm -r /home/testinguser.

If all of this did not work with you, then I'm guessing it's a bug.

Solution 3:

UPDATE: The solution is broken and not working for me too.

If you want to create the user's home directory if it does not exist, then run the useradd command with the -m flag. This will copy all files from the /etc/skel directory.

useradd -m username

You might need to configure settings for your system. According to the man page :

  -m, --create-home
    Create the user's home directory if it does not exist. The files and directories
    contained in the skeleton directory (which can be defined with the -k option)
    will be copied to the home directory.

    By default, if this option is not specified and CREATE_HOME is not enabled, no
    home directories are created.

and further indicates :

CONFIGURATION

   The following configuration variables in /etc/login.defs change the behavior
   of this tool:

   CREATE_HOME (boolean)
       Indicate if a home directory should be created by default for new users.