89

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.

cd1
  • 1,514

12 Answers12

152

Also you can use mkhomedir_helper

Usage: /sbin/mkhomedir_helper <username> [<umask> [<skeldir>]]
Rahul Patil
  • 3,111
  • 3
  • 16
  • 11
22

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.

math
  • 453
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.

quanta
  • 52,423
dotwaffle
  • 677
13
mkdir -p /home/john
chown john:john /home/john
usermod -d /home/john john

That should do the trick I believe

davidparks21
  • 1,088
11

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.

jgoldschrafe
  • 4,445
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.

Jenny D
  • 28,400
  • 21
  • 80
  • 117
2

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.

1

This is exactly what the mkhomedir_helper $USERNAME command does.

Amandasaurus
  • 33,461
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.

warren
  • 19,297
-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
Greeblesnort
  • 1,779
-2
usermod -d /home/john john

or

usermod --home /home/john john

and read

man usermod

;)