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

532,166

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.

Solution 4

If you created the new user with adduser command, you don't need to create a home directory for the new user. A home directory named with the name of the user in /home directory, with the following subdirectories: Desktop, Downloads, Documents, Music, Pictures, Videos (and others), for the new user will be created automatic.

If you created the new user with useradd command, then run the following commands in terminal:

sudo -i     #enter your user password when you are asked
gedit /etc/passwd

to edit /etc/passwd file as root (this is the most important step). Find the line with the name of the new user and set the default home directory for him something like this:

new_username:x:1001:1001::/home/new_username:/bin/sh

At this line you can also to add a real name for the new user, or set the default shell. Something like this:

new_username:x:1001:1001:Real New Username,,,:/home/new_username:/bin/bash

Be careful, don't make any other changes.

After you save the file, before to go out from the root account, run the following commands:

mkdir /home/new_username                                #to create the directory /home/new_username
cp -r /etc/skel/. /home/new_username                    #to copy skeleton files to /home/new_username
chown -R new_username:new_username /home/new_username   #to change the owner of /home/new_username to the new user

After all of these the home folder for the new user will automatically be populated after first login.

See also: How to make user home folder after account creation?

Solution 5

All you need realy is the home folder to make it work. As root,

cd /home
cd mkdir username
chown username username
chgrp username username

Logging in with the first time should create all necessary files, that seems to be those:

drwxr-xr-x 16 fotanus fotanus 4096 Jun  4 17:46 .
drwxr-xr-x  9 root    root    4096 Jun  4 17:43 ..
drwx------ 11 fotanus fotanus 4096 Jun  4 17:46 .cache
drwx------ 13 fotanus fotanus 4096 Jun  4 17:46 .config
drwxr-xr-x  2 fotanus fotanus 4096 Jun  4 17:45 Desktop
drwxr-xr-x  2 fotanus fotanus 4096 Jun  4 17:45 Documents
drwxr-xr-x  2 fotanus fotanus 4096 Jun  4 17:45 Downloads
drwx------  3 fotanus fotanus 4096 Jun  4 17:46 .gconf
drwxrwxr-x  2 fotanus fotanus 4096 Jun  4 17:46 .gstreamer-0.10
-rw-------  1 fotanus fotanus  334 Jun  4 17:45 .ICEauthority
drwxr-xr-x  3 fotanus fotanus 4096 Jun  4 17:45 .local
drwx------  4 fotanus fotanus 4096 Jun  4 17:45 .mozilla
drwxr-xr-x  2 fotanus fotanus 4096 Jun  4 17:45 Music
drwxr-xr-x  2 fotanus fotanus 4096 Jun  4 17:45 Pictures
drwxr-xr-x  2 fotanus fotanus 4096 Jun  4 17:45 Public
drwxr-xr-x  2 fotanus fotanus 4096 Jun  4 17:45 Templates
drwxr-xr-x  2 fotanus fotanus 4096 Jun  4 17:45 Videos
-rw-------  1 fotanus fotanus   55 Jun  4 17:45 .Xauthority
-rw-------  1 fotanus fotanus  711 Jun  4 17:45 .xsession-errors
Share:
532,166

Related videos on Youtube

Jo Rijo
Author by

Jo Rijo

Updated on September 18, 2022

Comments

  • Jo Rijo
    Jo Rijo over 1 year

    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?

    • Alaa Ali
      Alaa Ali over 10 years
      When the user logs in, those subdirectories (Documents, Downloads, etc...) will be automatically created. Although I'm searching for ways to "simulate" his login through the terminal.
    • Radu Rădeanu
      Radu Rădeanu over 10 years
      @Alaa su new_user :D
    • Alaa Ali
      Alaa Ali over 10 years
      @RaduRădeanu yeah I already tried that, also tried sudo -i -u new_user but it doesn't work. I think we need to "simulate an X login"...don't know how to do that.
    • Jo Rijo
      Jo Rijo over 10 years
      logging in doesn't create the subdirectories in home.
  • Alaa Ali
    Alaa Ali over 10 years
    As far as I remember, the home folder is not automatically created, you have to manually create it and update the user's entry in /etc/passwd (if it's not already there). Once the user logs in, the home folder will automatically be populated with those subdirectories. However, all of this is taken care of is someone adds a user using adduser, instead of useradd.
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    @Alaa I just tested a few days ago and I used adduser: the home folder is automatically created. And indeed, the home folder will automatically be populated after first login.
  • Jo Rijo
    Jo Rijo over 10 years
    this seems to try to create a new user, because it tells me "user 'new-user' already exists".
  • Jo Rijo
    Jo Rijo over 10 years
    this seems to do nothing.maybe I'll try the bash script from the other question, but couldn't I just create a folder in /home/ called new_user, and then copy the contents of /etc/skel to it? is that what that bash-script does?
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    @JoRijo I edited my answer.
  • crafter
    crafter over 10 years
    @Jo: And how is this incorrect, because according the initial post, the user does already exist.
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    In some situation when you use useradd, the default home directory for the new user is not automatic set to /home/username. So, first ypu must to be sure about that.
  • Alaa Ali
    Alaa Ali over 10 years
    Can you give me an example of these situations =)? From the man pages of useradd: "useradd will use the base directory specified by the HOME variable in /etc/default/useradd, or /home by default". The HOME variable in /etc/default/useradd is not defined by default, so useradd will always use /home. Anyways, I'll edit my answer and move the part to check /etc/passwd up.
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    An example here. Another example: You delete an user, but you didn't delete his directories and files; and then you add again a user with the same name. And there are many, and there are no bugs :)
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    @Alaa Well, thanks for observation. Also you can edit yourself an answer when you see some mistakes like these.
  • Alaa Ali
    Alaa Ali over 10 years
    This answer does not solve the issue. Copying /etc/skel does not create the subdirectories in the home directory. Also, useradd -m is used when creating a new user, not when the user is already added.
  • gerlos
    gerlos about 9 years
    Simple and useful. This way you also can be sure that the newly created home dir is compliant with your system policies, without ever knowing them or any manual tinkering.
  • Uzair Gheewala
    Uzair Gheewala almost 9 years
    This is the correct answer, at least it worked for me on Ubuntu 14.04.2 LTS.
  • user79878
    user79878 over 8 years
    I had to have my username in /etc/passwd first before this worked for me.
  • Newtopian
    Newtopian about 8 years
    Simple.. just works (tm). Should be the accepted answer
  • ntwrkguru
    ntwrkguru over 5 years
    Not all systems have GNOME and suggesting someone to remove and re-add a user simply to create a home directory is bad advice.
  • Don Kirkby
    Don Kirkby over 5 years
    Don't forget to check the directory's permissions with ls -l /home. I found that the new directory was readable by all other users, so I removed those permissions with chmod go-r-x /home/username.
  • Desik
    Desik about 4 years
    This doesn't create the files insde home, like .bashrc. Didn't work for me.
  • Themelis
    Themelis almost 4 years
    That's the best answer
  • Jesse Nickles
    Jesse Nickles almost 3 years
    @Desik make sure you run as root or sudo it seems to work.
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev over 2 years
    @Desik Have a look with ls -a. Perhaps, the files are there, but a simple ls doesn't show them. Also I had the same impression (that it didn't work), because /home/ was mounted read-only when I did it.