Safely Changing a Linux User's Home Directory with usermod -d

How to safely relocate a Linux user's home directory
July 9, 2026

Safely Changing a Linux User's Home Directory with usermod -d

There are times when you need to change the location of a specific user's home directory due to insufficient disk space or migration to a separate storage.
Although changing the path itself with usermod -d is simple, if the user is currently logged in or data consistency is crucial, it is safer to proceed in order: terminating sessions, backing up, and setting permissions.

1. Check for Active Sessions and Processes

Before relocating the home directory, check if the user is currently logged in or running any processes.
Check processes and sessions owned by the user
sh

2. Terminate the User's Sessions

Terminate all remaining processes and sessions.
Kill all processes and sessions for the user
sh
pkill -KILL -u terminates all processes owned by the user, and on systemd-based distributions, loginctl terminate-user can clean up login sessions as well.

3. Backup the Existing Home Directory

While usermod -d -m automatically moves existing data, it is recommended to create a separate backup first because it is difficult to revert if it fails.
Backup home directory with rsync
sh
rsync -aHAX preserves permissions, hard links, ACLs, and extended attributes (xattrs), making it ideal for home directory backups.
You can also back it up in the same way using cp.
Alternative: backup with cp
sh

4. Set Ownership and Permissions on the New Home Directory

Double-check that the ownership and permissions of the backup copy exactly match the original.
Fix ownership and permissions
sh
Use chown -R to recursively match ownership, and specify 700 (accessible only by the owner)—the default permission for a home directory—using chmod.

5. Change the Home Directory with usermod

Once both data and permissions are ready in the new path, change only the home directory path with usermod -d.
Change the user's home directory
sh
If you have already backed up manually, simply changing the path without the -m option is sufficient.
Using the -m option together allows usermod to directly move the contents of the old home directory to the new path. However, this fails if the new path already exists, so we do not use it in this case where we have already backed up in advance.

6. Verify the Change

Verify that the changed home directory has been correctly applied.
Verify the new home directory
sh

7. Clean Up the Old Home Directory

Only delete the old directory after verifying that logging in and working in the new home directory works without issues.
Remove the old home directory once verified
sh

Bonus: Creating a New User with a Custom Home Directory

Instead of relocating an existing user, you can also create a new user and specify your desired path as their home directory from the start.
Create a new user with a custom home directory
sh
-d specifies the home directory path, -m creates the home directory at that path and copies default files from /etc/skel, and -s specifies the login shell.

Command Summary

CommandDescription
ps -u <user> / who / wCheck active processes and sessions of the user
pkill -KILL -u <user>Force-terminate user-owned processes
loginctl terminate-user <user>Terminate systemd login sessions
rsync -aHAX src/ dst/Backup preserving permissions, hard links, ACLs, and xattrs
chown -R <user>:<group> <dir>Recursively change directory ownership
chmod 700 <dir>Set default permissions for home directory
usermod -d <newhome> <user>Change the home directory path
usermod -d <newhome> -m <user>Change home directory and move data simultaneously
useradd -m -d <newhome> -s <shell> <user>Create a new user with the specified path as their home directory
Jooojub
System S/W engineer
Explore Tags
Series
    Recent Post
    © 2026. jooojub. All right reserved.