How to Create Home Directories for Existing Users in Linux (Like useradd -m for Existing Users)


2 views

When creating users with useradd without the -m flag, you might end up with users that don't have their home directories. This is a common oversight, especially when batch-creating multiple users. The manual approach would be:


$ sudo mkdir /home/john
$ sudo cp -r /etc/skel/. /home/john/
$ sudo chown -R john:john /home/john

While this works, it's tedious when dealing with multiple users. Let's explore better solutions.

Most Linux distributions include mkhomedir_helper, a tool specifically designed for this purpose:


$ sudo mkhomedir_helper username

This single command will:

  • Create /home/username
  • Copy skeleton files from /etc/skel
  • Set proper ownership and permissions

To process all users without home directories:


$ getent passwd | awk -F: '{ if ($6 !~ /\/home\/.+/ && $6 !~ /\/nonexistent|\/var\/.+/) print $1 }' | xargs -I{} sudo mkhomedir_helper {}

This command:

  1. Lists all users
  2. Filters those without proper home directories
  3. Creates home directories for them

For future-proofing, you can configure PAM to automatically create home directories on first login:


$ sudo nano /etc/pam.d/common-session

Add this line:


session required pam_mkhomedir.so skel=/etc/skel umask=0022

After running any of these solutions, verify with:


$ ls -la /home
$ getent passwd | grep username

You should see the new home directory with proper permissions and skeleton files.


Many Linux administrators encounter this situation: you've created users with useradd but forgot the crucial -m flag, leaving users without home directories. This happens frequently in automated scripts or when managing multiple users.

By default, useradd without -m only creates the user account without setting up the home directory structure. The -m flag triggers:

  1. Creation of /home/username directory
  2. Copying of skeleton files from /etc/skel
  3. Proper ownership assignment

While there's no direct useradd -m equivalent for existing users, we can use mkhomedir_helper:

sudo mkhomedir_helper username

This single command from the libuser package handles all required steps. If the package isn't installed:

sudo apt-get install libuser  # Debian/Ubuntu
sudo yum install libuser      # RHEL/CentOS

For more customization, here's the manual process:

sudo mkdir /home/john
sudo cp -r /etc/skel/. /home/john/
sudo chown -R john:john /home/john
sudo chmod 700 /home/john

When dealing with multiple users, automate with:

for user in john mary bob; do
    sudo mkhomedir_helper $user
done

Always verify the results:

ls -la /home/john
getent passwd john | cut -d: -f6

While not ideal, you can move existing users:

sudo usermod -m -d /home/john john
  • Ensure no processes are running as the user when modifying
  • Backup any existing data in potential home directories
  • Check for custom skel directories in /etc/default/useradd