create home directories after create users

Solution 1:

Also you can use mkhomedir_helper

Usage: /sbin/mkhomedir_helper <username> [<umask> [<skeldir>]]

Solution 2:

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.


Solution 3:

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.


Solution 4:

mkdir -p /home/john
chown john:john /home/john
usermod -d /home/john john

That should do the trick I believe


Solution 5:

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.