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
username:x:500:500::/home/username:/bin/bash
change it to
username:x:500:500::/home/new_home_directory:/bin/bash
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.
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/$
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:
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
Output:
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
- -d dirnanme : Path to new login (home) directory.
- -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 directory in Linux using cd command
How to change directory in Linux
The procedure changes a directory or folder in Linux as follows:
- Let us change to your home directory in Linux, run: cd
- If you want to change to the /etc/security/ directory on Linux, execute: cd /etc/security/
- Want to go up one level of the directory tree in Linux? Try: cd ..
Let us see some examples and usage in details.
Changes the current directory in Linux
Let us change the current working directory to the home (login) directory in Linux, run:
cd
To print the current working directory, use the pwd command:
pwd
Next change to an arbitrary directory named /etc/ufw/, type:
cd /etc/ufw
pwd
At this stage, you may want to list the directory contents. Hence, try the ls command:
ls
ls -l
Say you need to go down one level of the directory tree (say you want to change to the applications.d), run:
cd applications.d
pwd
ls -l
How do I go back to my home folder or directory in Linux?
Simply type any one of the following command:
cd
OR
cd
Changing to another directory in Linux using the cd command
How to change a folder in Linux
Get list of directories only in the current directory using the following:
ls -d */
Once you know the directory names, change it as per your needs:
cd linux
pwd
ls
To move back to a parent directory directory, type:
cd ..
Verify it:
pwd
ls
Linux change directory using the cd command
Linux change directory and symbolic links
One can force symbolic links to be followed. The syntax is:
cd -L dir1
cd -L link2
To use the physical directory structure without following symbolic links:
cd -P link2
Force symbolic links with the -L option and he physical directory structure with the -P option
Conclusion
This page explained the cd command that allows you to change directories. For more info type the following command at the terminal:
cd —help
OR
help cd