87

I created some users with:

$ useradd john

and I forgot to specify the parameter -m to create the home directory and to have the skeleton files copied to each user. now I want to do that, and I don't want to recreate all users (there must be an easier way). so, is there any way to create the user directories and copy the skeleton files?

I thought about creating the directories, chowning them to the corresponding user, copying all the skeleton files and chowning them to the corresponding user. but if there's a command like useradd -m that doesn't create the user again, but create the directories, it'd be better.

3
  • Did this happen with a large list of users? Sep 9, 2009 at 15:04
  • I had around 10 users with this problem.
    – cd1
    Sep 9, 2009 at 19:35
  • 2
    I can't help but feel Rahul has the best answer to your question. Maybe you should revisit your accepted answer?
    – user145545
    Dec 21, 2015 at 6:08

12 Answers 12

17

This might sound like a silly idea, but if the users aren't actually doing anything, you could do:

cat /etc/passwd | cut -f 1 -d : >/tmp/users.list

Then edit /tmp/users.list to only contain the users you want. Then do:


for i in `cat /tmp/users.list`
do
    userdel $i
    useradd -m $i
done

However, many Redhat based distributions will create you a new home directory when you first login, providing it is specified in /etc/passwd where the directory should be.

To test that, do an "su - " and see if it does "the right thing". If it doesn't, the above script will work quite nicely, I think.

7
  • 1
    yes, it worked, although it created new UIDs and GIDs (but that wasn't a problem). but I forgot to backup the passwords from /etc/shadow, now the users will have to set their passwords again =/
    – cd1
    Sep 9, 2009 at 15:18
  • If he created them recently he might be able to do: cat /etc/passwd | egrep '^\:[0-9]{4}\:' | cut -f 1 -d : >/tmp/users.list That should only grab UID of valid users, and not system users. Sep 9, 2009 at 15:18
  • What about passwords, do they remain the same? I fear not!
    – math
    May 12, 2012 at 15:57
  • for i in $(awk -F: '{print $1 }' /etc/passwd) Jul 9, 2013 at 6:48
  • why use grep or cut with cat and with pipe, why not directly in this way? cut -f 1 -d : < /etc/passwd > passwtmp
    – c4f4t0r
    Dec 12, 2013 at 12:01
144

Also you can use mkhomedir_helper

Usage: /sbin/mkhomedir_helper <username> [<umask> [<skeldir>]]
4
  • 20
    This is the only answer that actually answers the question without some 10 line script.
    – SineSwiper
    Jan 6, 2014 at 23:58
  • This and the pam answer are the best here, thanks. Never mess with those files by hand if you can avoid it.
    – h4unt3r
    Jan 21, 2014 at 1:02
  • 2
    dka@dev-04:/$ /sbin/mkhomedir_helper dka dka@dev-04:/$ cd bash: cd: /home/dka: No such file or directory Nov 19, 2017 at 22:19
  • 1
    @DimitriKopriwa have you tried it as root e.g. sudo /sbin/mkhomedir_helper dka ? the /home directory belongs to the root user, and so I would imagine one would have to be root to create a subdirectory thereof.
    – Jonathan
    Apr 30, 2019 at 20:54
21

You will need to create the users directory manually. This requires three steps:

  1. Create directory in compliance to /etc/passwd, usually there will be already a /home/login entry.
  2. Copy initial files from /etc/skel
  3. And finally set right permissions:

    • mkdir /home/YOU
    • cd /home/YOU
    • cp -r /etc/skel/. .
    • chown -R YOU.YOURGROUP .
    • chmod -R go=u,go-w .
    • chmod go= .

BTW: I always miss the -m option for useradd too. At least Debian based systems should have an adduser command, which I recommend over useradd. If you missed -m option it might also be worth considering to deluser and then recreate the user with proper options.

Edit: Added -r for copying also directories.

7
  • Did that: usernameh=myusername; mkdir /home/$usernameh ; cp /etc/skel/* /etc/skel/.* /home/$usernameh; chown -R $usernameh:$usernameh /home/$usernameh; chmod -R 755 /home/$usernameh;
    – Aki
    Sep 26, 2013 at 15:11
  • Personally I don't like the 755 chmod, as it allows users to spy into other users home's. I think each user should explicitly grant access to others. As public_html needs at least x bit set, I would recommend chmod 0711 on each home, public_html and similar directories as default.
    – math
    Sep 27, 2013 at 7:51
  • also, use cp -r for skel, as else it won't copy directories (.ssh).
    – Aki
    Sep 27, 2013 at 12:22
  • On my system (Arch Linux on ARM) cp -r /etc/skel/.* recurse back into /etc/ and copies all data from here (/etc/skel/.* matches /etc/skel/.. I expect). The solution from superuser.com/a/61619/22153 seem to work: cp -r /etc/skel /home/user (/home/user must not exist before running the command)
    – zpon
    Dec 18, 2014 at 20:10
  • I guess you shell globbing expands * with . (dot). So .. is matched. Am I right? Many shells disable this per default, because of such behavior. Which shell do you use?
    – math
    Jan 20, 2015 at 8:10
14
mkdir -p /home/john
chown john:john /home/john
usermod -d /home/john john

That should do the trick I believe

8
  • 2
    it says: usermod: no changes. and the directory isn't created. it doesn't work either with the -m option.
    – cd1
    Sep 9, 2009 at 14:56
  • 1
    Alright. I figured out why that didn't work, useradd used to only put $HOME_DIR in /home unless you specified otherwise. It now seems to automatically put it in /home/$USER instead. A cheap way might be to usermod -d /home/john2 -m john then run usermod -d /home/john -m. Sep 9, 2009 at 15:09
  • Nevermind that doesn't work either. Sep 9, 2009 at 15:10
  • 1
    Well at least you learned something new. Sep 9, 2009 at 15:38
  • 1
    You forgot to copy the contents of /etc/skel/ + chown recursive for those new files. usermod wouldn't work since here the directory was registered but not created, usermod won't do anything.
    – Aki
    Sep 26, 2013 at 15:12
10

You can use something like pam_mkhomedir to prevent this from ever being an issue with any users in the future. pam_mkhomedir is a PAM module that automatically creates a user's home directory on login if it doesn't exist, and populates it with files from /etc/skel (or whatever skel directory you specify).

This is also a nicely scalable approach because it will continue to solve this problem if you ever switch your user repository over to a directory service like LDAP in the future.

4

In my case, the home volume was corrupted and I decided just rebuild it from scratch since not much data involved but I want to keep users' login information, so I recreated the home directories manually with this script:

#!/bin/bash
cat /etc/passwd | while IFS=: read n x i g c d r
do
  # my system has uid started at 1000, but has nfsnobody at 65534:
  if [[ "$i" -ge 1000 && "$i" -le 65000 && ! -x "$d" ]]
  then
    cp -av /etc/skel "$d"
    chown -R "$i:$g" "$d"
    # may needed in SELinux system:
    restorecon -R "$d"
    # add your chmod as your need:
    chmod -R o-rw "$d"
  fi
done
2

If you edit /etc/login.defs to contain

CREATE_HOME yes

then home directories will be automatically created for any future users, unless you tell the system not to do so.

Another option is to use PAM for logins, and use the pam_mkhomedir module to automagically create the homedir on the first login.

1

Login with the user john and write from a shell:

xdg-user-dirs-update

That's it! Don't use sudo or su, you don't need root access to create some directories. From a root account, you can use:

sudo -u john xdg-user-dirs-update

That way, you will execute the command as john, that can be useful if you made the mistake with more than one user.

0

My first step after doing a useradd is to su - <user>.

Creates the home directories, copies skeletons, etc - at least on the CentOS 4 box I do that on most frequently.

0

This is exactly what the mkhomedir_helper $USERNAME command does.

1
  • 1
    No value is added by repeating what another answer already explained two years ago.
    – kasperd
    Jun 18, 2015 at 9:57
-2

You could simply edit /etc/passwd. The second to the last field is the user's home directory.

greeblesnort:x:1000:1000:greeblesnort,,,:/home/greeblesnort:/bin/bash
2
  • They users were already created. The home directories weren't created because he forgot a switch. Sep 9, 2009 at 19:27
  • I didn't mean to change the user's home directory, I meant to create the directory and copy the skeleton files to it with the appropriate permissions after the user has been added.
    – cd1
    Sep 9, 2009 at 19:37
-2
usermod -d /home/john john

or

usermod --home /home/john john

and read

man usermod

;)

1
  • 2
    The question is more complex than just that - and already has better answers. Apr 6, 2015 at 10:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .