Меню Рубрики

Change home directory linux

Change Home Directory in Linux

Change the home directory of a Linux user with a simple usermod command. While creating a user if you didn’t specify any –home parameter Linux assumes the home directory of the user to be /home/username even if you did specify you can later change it to something else according to your needs. Apart from changing the home directory using the usermod command you’ll have to assign proper ownership and permissions to the new folder. You can also change the home directory by editing the /etc/passwd file. I’ll outline both the steps here.

Change the home directory using usermod

This method is for command line warriors. Before you use the usermod command the new home directory should be created, ownership should be assigned to the new user and the folder should be chmoded correctly so that no one else can access it. Run the following commands to do it.
mkdir /home/new_home_directory
chown username:username /home/new_home_directory
chmod 700 /home/new_home_directory
usermod –home /home/new_home_directory username

Change the home directory by editing /etc/passwd

Alternatively you can also edit the /etc/passwd to change the home directory. But you should be careful not to edit anything else. Before editing this file it is always better to create the new home directory and assign proper permissions and ownership to it. Execute the following commands.
mkdir /home/new_home_directory
chown username:username /home/new_home_directory
chmod 700 /home/new_home_directory

Open the /etc/passwd file using a text editor and locate the line containing the required username it should look something like this

change it to

Save the file.

Finally copy all the old content to the new home directory
cp -f /home/username/* /home/new_home_dir/


Command to change the default home directory of a user

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

6 Answers 6

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 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:

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

Simply open this file using a text editor, type:

The default home directory defined by HOME variable, find line that read as follows:

Save and close the file. Now you can add user using regular useradd command:

Verify user information:

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:

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.

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

Set DHOME variable

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:

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.


How to change the Home directory of the currently logged In user

I am currently logged in into a CentOS server and I would like to change my home directory from /home/myuserName/ to /var/www/html/

I tried the below command :

But this gives me an error:

5 Answers 5

short answer : you can’t.

long answer:

HOME dir is set in /etc/passwd , 6th field. It is read upon login; your shell is started with this home dir.

The proper way to change home dir for joe is :

  • have joe log off.
  • use usermod -d /new/home joe to change home dir for subsequent session.

Once session is run, you must do two things:

  • edit $HOME to change home dir for session (to be repeated on all active session).
  • use sudo vipw to edit home dir for next session

Also, be aware you might have an issue with permissions/ownership on /var/www/html .

You need to edit the /etc/passwd file to change home directory of users that are currently logged in.

Edit the /etc/passwd with sudo vipw and change home directory of the user.

vipw highly recommended other than vim or other editors since vipw will set lock to prevent any data corruption.

The usermod command won’t work if you’re logged in with the user you are trying to make changes on.

From the manual page on usermod it says:

CAVEATS usermod will not allow you to change the name of a user who is logged in. You must make certain that the named user is not executing any processes when this command is being executed if the user’s numerical user ID is being changed. You must change the owner of any crontab files manually. You must change the owner of any at jobs manually. You must make any changes involving NIS on the NIS server.

Try logging in with a different user and running the command again.

If that isn’t possible then you can manually edit the /etc/passwd file (which is actually what the usermod command is doing). If you do that make sure you back the file up in case you inadvertently do something silly.


Can’t change to home directory

When logging into my virtual server as a non-root user, I suddenly get the following error:

“Could not chdir to home directory /var/www/vhosts/prosodylab.org/web_users/username: Permission denied -bash: /var/www/vhosts/prosodylab.org/web_users/username/.bash_profile: Permission denied”

I checked the permissions in the user directory (they’re set to 700, and I also changed to 777 to check, with now effect).

This affects all users (including if I create a new user), but as root user I can still change into the relevant directories without problem.

echo $HOME returns the right directory.

Here is the directory entry of the home directory of the user (when set to 700):

grep username /etc/passwd returns the following output, so the directory seems to be set correctly:

The server is a 1&1 dedicated virtual linux server, and it’s worked fine for many years. I might have inadvertently changed something, but if so the I don’t know what.

Does anyone know what might be the problem?


Linux Change Default User Home Directory While Adding A New User

B y default base directory for the system user is set to /home directory. I’d like to add user to /iscsi/home/$ directory instead of the default /home. How do I force useradd command to add user to /iscsi/home under CentOS / RHEL / Fedora Linux server?

Default values for account creation defined in /etc/default/useradd file under CentOS / RHEL / Fedora / Debian / Ubuntu and other Linux distros. 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:
Replace with:
Save and close the file. Now you can add user using regular useradd command:
# useradd vivek
# passwd vivek

Verify user information:
# finger vivek

How Do I Change Existing User’s Home Directory?

You need to use the usermod command to set the user’s new login directory. The syntax is as follows:

usermod -m -d /path/to/new/home/dir userNameHere

  1. -d dirnanme : Path to new login (home) directory.
  2. -m : The contents of the current home directory will be moved to the new home directory, which is created if it does not already exist.

In this example set the user’s new login directory to /users/v/vivek from /home/vivek, enter:
# usermod -m -d /users/v/vivek vivek


How to change my home directory?

I have a current /home/user/ directory for

but I want to change it to be at /user/home/

/user/home already exists.

The option of using usermod is not going to work because I don’t have access to the system as root or as another user.

I am asking for a solution along the lines of modifying some .bashrc file and changing some environment variable or smth similar. I log in via ssh.

I’m running Ubuntu 14.04.

Thank you in advance

Solutions like the ones below unfortunately aren’t applicable to my case:


I thought I’d give some more info here rather than respond to the comments.

Currently the folder structure is a lot stranger than my example above, but the jist of it is the same. Ie currently when I do:

so when I use things like pip with the –user tag it will install things locally.

Because there are some memory limitations as well as ssh issues with writing to that location (after some time I can no longer write) I would like to have the following behaviour:

/path/of/new/home/ already exists and doesn’t have the limitations set above.

1 Answer 1

Well, you could just add this line to your

However, that really isn’t a good idea. Problems it would cause include (but are probably not limited to):

That will only work if /home/user is owned by your user. If it isn’t, you won’t even be able to log in.

This will work for your user only. For everyone else, your home directory will be whatever is stored in /etc/passwd . This means that, for example, cd

user will fail. In other words, if I log in as bob and bob has the line HOME=/home/bob/foo in

/.profile , then bob thinks that his home directory is /home/bob/foo but nobody else knows that:

So far so good. But:

This will drive your sysadmin insane. You do not want to anger your sysadmin for you are crunchy and taste good with ketchup.

In any case, it’s rarely a good idea to mess with variables like $HOME , it can often have unintended consequences. Instead, a much cleaner solution would be to make sure every new shell session starts in the target directory. Just add this line to your

Now, each time you log 2 in or open a terminal, you will find yourself in /user/home .

/.bash_profile if it exists.

2 Log in to Debian-based systems like Ubuntu, anyway. For other distributions/OSs, you might need to add it to