Command to change the default home directory of a user

LinuxShellUnix

Linux Problem Overview


I would like to know whether there is any simple shell command to change the user home directory in Linux/Unix (one similar to chsh which changes the default login shell of an existing valid user) without touching the /etc/passwd file. Thanks

Linux Solutions


Solution 1 - Linux

Ibrahim's comment on the other answer is the correct way to alter an existing user's home directory.

Change the user's home directory:
usermod -d /newhome/username username

usermod is the command to edit an existing user.
-d (abbreviation for --home) will change the user's home directory.

Change the user's home directory + Move the contents of the user's current directory:
usermod -m -d /newhome/username username

-m (abbreviation for --move-home) will move the content from the user's current directory to the new directory.

Solution 2 - Linux

From Linux Change Default User Home Directory While Adding A New User:

> Simply open this file using a text editor, type: > > vi /etc/default/useradd > > The default home directory defined by HOME variable, find line that > read as follows: > > HOME=/home > > Replace with: > > HOME=/iscsi/user > > Save and close the file. Now you can add user using regular useradd > command: > > # useradd vivek > # passwd vivek > > Verify user information: > > # finger vivek

Solution 3 - Linux

The accepted answer is faulty, since the contents from the initial user folder are not moved using it. I am going to add another answer to correct it:

sudo usermod -d /newhome/username -m username

You don't need to create the folder with username and this will also move your files from the initial user folder to /newhome/username folder.

Solution 4 - Linux

In case other readers look for information on the adduser command.

Edit /etc/adduser.conf

Set DHOME variable

Solution 5 - Linux

You can do it with:

/etc/passwd

Edit the user home directory and then move the required files and directories to it:

cp/mv -r /home/$user/.bash* /home/newdir

.bash_profile
.ssh/ 

Set the correct permission

chmod -R $user:$user /home/newdir/.bash*

Solution 6 - Linux

Found out that this breaks some applications, the better way to do it is

In addition to symlink, on more recent distros and filesystems, as root you can also use bind-mount:

mkdir /home/username 
mount --bind --verbose /extra-home/username /home/username

This is useful for allowing access "through" the /home directory to subdirs via daemons that are otherwise configured to avoid pathing through symlinks (apache, ftpd, etc.).

You have to remember (or init script) to bind upon restarts, of course.

An example init script in /etc/fstab is

/extra-home/username /home/username none defaults,bind 0 0

Solution 7 - Linux

usermod -m -d /newhome username

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIbrahim QuraishView Question on Stackoverflow
Solution 1 - LinuxSTWView Answer on Stackoverflow
Solution 2 - LinuxMaximinView Answer on Stackoverflow
Solution 3 - LinuxSukrit GuptaView Answer on Stackoverflow
Solution 4 - LinuxrafaelvalleView Answer on Stackoverflow
Solution 5 - LinuxGaristarView Answer on Stackoverflow
Solution 6 - LinuxWilkenView Answer on Stackoverflow
Solution 7 - LinuxAhmed KayView Answer on Stackoverflow