- Command to change the default home directory of a user [closed]
- 7 Answers 7
- Change the user’s home directory:
- Change the user’s home directory + Move the contents of the user’s current directory:
- Linux Home Directory
- How Linux Home Directory Command Works?
- Examples to Implement Linux Home Directory
- 1. Home Directory
- 2. Home Directory with cd Command
- 3. Home Directory with Tilde Option
- 4. Home Directory with HOME Variable
- Conclusion
- Recommended Articles
- Create Home Directory for Existing Users in Linux
- Reason why home directory was not created
- Create the home directory for existing user
- Bonus: Creating new users with the home directory
- Wrapping Up
Command to change the default home directory of a user [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
stackoverflow.com/questions/20071714/…, says that usermod -d /home/userxx /home/useryy to change linux default login directory to /home/useryy
Why do you ask about the default home directory? Do you want to change the home directory of one particular existing user, or of all users added in the future? Notice that chsh don’t change the default login shell, but just the login shell of a given user. To change the HOME of a particular user, just edit /etc/passwd
I’m voting to close this question because it has nothing to do with programming and would have been best suited for Unix & Linux or Super User.
7 Answers 7
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.
Not only move the content, I see it more like move whole folder = original folder ( /home/username typically) is removed.
@STW Is this a reversible task? Meaning if I just run it once and then flip the parameters, will my home directory be exactly the way it was before?
The order of options for the second command is wrong. usermod -m -d /newhome/username username works.
@KolonUK because that’s the only correct way really. When you are logged in as the user, you have applications opened which rely on that home directory. You shouldn’t be changing the location while everything is running. So yeah, creating a new user account and deleting is not that bad at all. Or you could set a password for root and login as root in a new TTY.
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:
# useradd vivek # passwd vivek
usermod -m -d /path/to/new/login/home/dir user changes existing user home directory to a new login directory which is created if it does not already exist, option -m moves the contents of the current home directory to the new home dir
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.
In case other readers look for information on the adduser command.
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*
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
Linux Home Directory
In the Linux ecosystem, the home directory is also called as the home directory. It is the primary entry point of the user when they are login into the Linux environment. It is responsible to store files, folders, data, and software on /home directory with the respective individual user profile.
Web development, programming languages, Software testing & others
Syntax of Home Directory
- /home: We can use the /home as the default path for Linux Home Directory. It is the primary or starting path for entering the individual user profile.
- USER NAME: We need to pass the user name in the “/home” path. It will help to enter in the individual user profile path (in terms of the file system).
How Linux Home Directory Command Works?
When we are creating any user in the Linux system. While creating the user 5 different steps will happen. The user directory creation it comes under the same 5 different steps. When any user will be added in the Linux operating system, by default, the user directory will create in the “/home” path with the same user name.
The user directory is also known as the user home directory. It will provide the basic environment of shell and bash. It will help to execute the shell or some application-level jobs.
By default, the list of files will create in the user “/home” directory.
- bash_logout: The file is responsible to perform any action when the user or the terminal will logout.
- bash_profile: The file is responsible to perform any action when the user profile will load the in the Linux environment.
- Bashrc: The file is responsible for the necessary kinds of stuff in it. It will help to the bash or shell or other applications. We can put functions, alias, shell or bash options etc.
As we have seen, the default home directory path will present in the “/home” path. But is not mandatory that, it will always be present over there. As per the requirement, we can change the home directory for the individual user. We can change the default home directory with the help of “usermod” command (as per user level).
Examples to Implement Linux Home Directory
Following are the examples are given below:
1. Home Directory
When we are creating any user on the Linux level, the user home directory will automatically create in the “/home” path with the same user name.
Explanation: As per the above command, we are able to list out the number of user home directories. Before adding any user, there is no home directory available in the environment (refer to screenshot 1 (a)). When we are adding the user in the Linux environment. The home directory will create in “/home” path. The home directory name is similar to the user name only (refer to screenshot 1 (a)). The user can use the same directory to store the number of files, folders, data, etc. It is having the necessary permissions on the files system to store or retrieve the data from the home directory.
As discussed earlier, when any user or home directory will create in the “/home” path. It will create default 3 files in the same working directory i.e. bash_logout, bash_profile and bashrc.
2. Home Directory with cd Command
In the Linux environment, we are having the functionality to come directly in the home directory. For that, we just need to run the simple “cd” command in the shell prompt.
Command:
Explanation: As per the above command, we are simply using the “cd” command. It is a simple command but usable command in the Linux environment. When we are working in any current directory and we need to move from the current working directory to the home directory. We just simply use the “cd” command. As per the below screenshot 2 (a), we are login with the “user1” user and the current working directory is “/home/user1/data”. Now I need to move or go back to the home directory. I have just used the “cd” command in the shell window (refer to screenshot 2 (b)).
3. Home Directory with Tilde Option
We can use the “cd” command to come in the home directory from any location. Similarly, we can achieve the same functionality via the “tilde (~)” option.
Explanation: As have seen the “cd” command concept in the home directory. Similarly, we can see the concept with a tilde sign or option. As per the below screenshot 3 (a), the current working directory is “/home/user1/data”. No matter, in which location we are. If we would need to move from the current working directory to the home directory. We can use the tilde sign with the cd command. As per the above command, we are moving from the current working directory to the home directory (refer to screenshot 3 (b)).
4. Home Directory with HOME Variable
In the Linux environment, we are having the functionality to use the environment variables. With the help of the “HOME” variable, we can directly able to go the home directory.
Note: It will help to call the home directly in different shell jobs or other applications.
Explanation: In Linux, there are lots of global variables. The “#HOME” variable is one of the global variables present in the Linux operating system. As per the above command, we are able to move from any working directory to the home directory. As per the below screenshot 4 (a), we are in “/home/user1/data” directory. As per the current working directory, we are able to move from “/home/user1/data” directory to the home directory (refer screenshot 4 (b)).
Conclusion
We have seen the uncut concept of “Linux Home Directory Command” with the proper example, explanation and command with different outputs. The home directory will define the proper skeleton of the structure. It is the primary authorized storage for the Linux user. When any user will add in the Linux operating system. By default, the home directory will allocate to that user.
Recommended Articles
We hope that this EDUCBA information on “Linux Home Directory” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
Create Home Directory for Existing Users in Linux
Created a user but without the home directory? Worry not. You can add home directory for existing users too. Here’s how to do that.
Looking for a way to create a home directory for the existing user? Well, here’s a quick guide for you.
But before that, let’s have a look at why there was no home directory for the user in the first place.
Reason why home directory was not created
So if you used the useradd command to add the new user in Linux, it won’t add the home directory by default.
And if you insist on using the useradd command to create the new user, you just have to append the -m option and it will create a home directory by default.
Create the home directory for existing user
When the existing user does not have a home directory and tries login with the su — option, it will get you the following error:
Which clearly states that there is no home directory.
The first step is to log out from the user that does not have the home directory using the given command:
Now, all you need to do is append the mkhomedir_helper and the username with the useradd command:
sudo mkhomedir_helper username
My user is named as Abhiman so my command would look like this:
sudo mkhomedir_helper Abhiman
And it will create a home directory for the user. For me, it will be named as /home/Abhiman:
And if you are using desktop environments such as GNOME, KDE, etc. you will need to reboot your system to have sub-directories such as Downloads, Documents, and so on.
Bonus: Creating new users with the home directory
I prefer the adduser command for the reason that it allows the proper creation of a new user in Linux.
The regular useradd command is also capable of creating a new user with the home directory in this fashion:
Wrapping Up
This was a quick tutorial on how you can create a home directory for the existing user.
I won’t recommend you to go with recreating the user (shown as a bonus tip) unless the user is recently made and you got to know that there is no home directory.
I hope you will find this helpful and if you have any queries, let me know in the comments.