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.

link|improve this question

38% accept rate
Did this happen with a large list of users? – Dave Rickman Sep 9 '09 at 15:04
I had around 10 users with this problem. – CD1 Sep 9 '09 at 19:35
feedback

6 Answers

up vote 3 down vote accepted

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.

link|improve this answer
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 '09 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. – Dave Rickman Sep 9 '09 at 15:18
What about passwords, do they remain the same? I fear not! – math May 12 at 15:57
feedback
usermod -d /home/john john

That should do the trick I believe

link|improve this answer
1  
it says: usermod: no changes. and the directory isn't created. it doesn't work either with the -m option. – CD1 Sep 9 '09 at 14:56
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. – Dave Rickman Sep 9 '09 at 15:09
Nevermind that doesn't work either. – Dave Rickman Sep 9 '09 at 15:10
usermod didn't work for this case, but I didn't know about this command, it'll be useful to me :) – CD1 Sep 9 '09 at 15:19
1  
Well at least you learned something new. – Dave Rickman Sep 9 '09 at 15:38
feedback

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.

link|improve this answer
feedback

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 /etc/skel/.* /etc/skel/* . But check for directories you might have missed to copy
    • 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 also have and adduser command which I would recommend over useradd.

link|improve this answer
feedback

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.

link|improve this answer
feedback

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
link|improve this answer
They users were already created. The home directories weren't created because he forgot a switch. – Dave Rickman Sep 9 '09 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 '09 at 19:37
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.