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
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
I am unsure what you meant, `HOME=/var/www/html` will change *for the session* your home dir.
short answer : you can't.
HOMEdir 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.
usermod -d /new/home joeto change home dir for subsequent session.
Once session is run, you must do two things:
$HOMEto change home dir for session (to be repeated on all active session).
sudo vipwto edit home dir for next session
Also, be aware you might have an issue with permissions/ownership on
You need to edit the
/etc/passwdfile to change home directory of users that are currently logged in.
sudo vipwand change home directory of the user.
vipwhighly recommended other than
vimor other editors since
vipwwill 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.
A couple possible workarounds, depending on what you're hoping to solve:
Option 1. Add
HOME=/var/www/htmlto your .bashrc
Option 2. Rename
/home/myusernameand then create a symlink to the desired directory.
mv /home/myusername /home/myusername-old ln -s /var/www/html /home/myusername
You can only do this temporarily. This means that every time you login as that user you have to run this one command:
This way, you can make most applications think that your home directory is whatever location you use above.
~will also start to point to
The applications this worked with for me were NPM and Maven.