34

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 :

> sudo usermod -d /var/www/html myuserName

But this gives me an error:

usermod: user myUserName is currently logged in
2
  • 3
    I am unsure what you meant, HOME=/var/www/html will change for the session your home dir.
    – Archemar
    Commented Feb 4, 2016 at 8:23
  • That helps, But i would like to change my home directory permanently, not for just the current session.
    – Mohan
    Commented Feb 4, 2016 at 8:28

5 Answers 5

26

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.

3
  • 5
    edit /etc/passwd always worked for me. See @7171u below Commented Mar 30, 2017 at 11:35
  • editing /etc/passwd without logoff/logon ?
    – Archemar
    Commented Mar 30, 2017 at 12:02
  • editing /etc/passwd for an account that is logged in, and then start a new session with that same account, and it obeys the new home dir. For the already logged in account, that session still has the old home location in the Environment. Commented Mar 31, 2017 at 9:12
13

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.

9

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.

4

A couple possible workarounds, depending on what you're hoping to solve:

Option 1. Add HOME=/var/www/html to your .bashrc

Option 2. Rename /home/myusername and then create a symlink to the desired directory.

mv /home/myusername /home/myusername-old
ln -s /var/www/html /home/myusername
1
  • bad habbit. after that, myusername-old can not be used by other user
    – GeoMint
    Commented Jun 3, 2018 at 9:52
-3

You can only do this temporarily. This means that every time you login as that user you have to run this one command:

export HOME=/var/www/html

This way, you can make most applications think that your home directory is whatever location you use above.

~ will also start to point to /var/www/html.

The applications this worked with for me were NPM and Maven.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .