Change a Linux User’s Home Directory with usermod -d and -m

Every local Linux account has a home directory stored in the sixth field of /etc/passwd. This guide shows how to change that folder safely with usermod. It works on CentOS, RHEL, AlmaLinux, Rocky and most other distributions.

1. Check the current home directory

getent passwd jdoe
jdoe:x:1001:1001:John Doe:/home/jdoe:/bin/bash

2. Change the path only

If the new folder already exists, or you’ll copy the files yourself, use -d:

usermod -d /data/home/jdoe jdoe

This only updates /etc/passwd. Nothing is copied, so the user gets an empty or missing home at the next login.

3. Change the path and move the files

Most of the time you want -m (--move-home) together with -d. This moves the existing contents to the new location:

usermod -m -d /data/home/jdoe jdoe

Make sure the user is logged out first. Check with who or loginctl list-sessions. usermod won’t change an account with running processes.

4. Fix ownership and permissions

If you created or copied the folder by hand, set the owner and mode:

chown -R jdoe:jdoe /data/home/jdoe
chmod 700 /data/home/jdoe

5. Fix SELinux labels (RHEL family)

On SELinux systems, a home directory outside /home gets the wrong file context. SSH key logins then fail. Add an equivalence rule and relabel:

semanage fcontext -a -e /home /data/home
restorecon -Rv /data/home

If usermod doesn’t work: edit /etc/passwd

As a last resort you can edit the sixth field directly. Use vipw instead of opening the file in vi, because it locks the file and checks the syntax:

vipw
# jdoe:x:1001:1001:John Doe:/data/home/jdoe:/bin/bash

Then move the files yourself and fix ownership as in step 4.

LDAP and AD users

usermod only changes local accounts in /etc/passwd. Directory users keep their homeDirectory attribute in LDAP, so change it there instead. See Change home directory of LDAP user command line.

Verify

getent passwd jdoe
su - jdoe -c pwd

The second command should print the new path. For other account changes, such as groups, shells and expiry dates, see our usermod command examples.

Related guides: Change home directory of LDAP user command line | 16 usermod Command Examples for Linux User Administration

Leave a Reply

Your email address will not be published. Required fields are marked *