Before we can jump into changing the default user home directory on a Linux operating system environment, we should brief through some theoretical and practical information related to the Linux home directory.
By definition, Linux is a multi-user operating system, which creates the need for a universal directory called the Home directory where different OS users can store and create personalized/user-centered files and directories.
These files and directories are only accessible to the homeowner (currently logged-in user). Therefore, each time a new user is created on a Linux environment, the user is associated with a unique home directory accessible only to that user.
On the other hand, the existence of these universal directories also permits a universal user (other than the homeowner) to have access to them. This user is known as root and can access all homeowner directories within a Linux system.
The syntax associated with a user’s Home directory is as follows:
/home/directory_name
For instance, if a Linux system has a user called dnyce, then the absolute path to this user’s Home directory will be:
/home/dnyce
We can even list the permission settings associated with the user in the following manner:
$ ls -ld /home/dnyce
This article will walk us through changing the default user home directory on a Linux operating system environment.
Benefits of Having a Linux Home Directory
We can summarize the benefits of having a Linux Home directory in the following manner.
- User Access Restriction – It is only the homeowner and root user that can access this directory making it impossible for other users to tamper with existing customization settings and stored configuration files. Therefore, privacy and security are guaranteed.
- Controlled Environment – Since multiple homeowners can exist within a single Linux system, their restriction on their Home directories is very advantageous. For instance, if a homeowner’s Home directory contains malicious code in stored executable files, the execution of this code will only affect that user environment and not the entire system.
- Controlled Flexibility – The user’s Home directory is automatically created during the Linux installation. During this installation process, a user can decide to assign this directory an exclusive partition. This step makes it easier to execute backup and restoration tasks.
Changing Default User Home Directory in Linux
We will need to create a sample Linux user account for this tutorial to be more practical. Since creating a new user in Linux is associated with a Home directory, let us create one.
# useradd -m homeowner # passwd homeowner
The -m
flag makes sure that a default user Home directory is created and set a password to the user.
We can now switch to the newly created user account and confirm the default user home directory.
# su - homeowner $ pwd
Switch back to the root user account.
$ exit
and create a new directory replacement for the default user Home directory.
# mkdir /home/newowner
We will assign user homeowner directory ownership for the newowner.
# chown -R homeowner:homeowner /home/newowner
Now, to change the user’s default Home directory from /home/homeowner to the new directory /home/newowner, we will make use of the usermod -d
command in the following manner.
# usermod -d /home/newowner homeowner
The -d
flag sets the directory.
We can switch back to the homeowner account and confirm the changes.
# su - homeowner $ pwd
Alternatively, we could set the preferred user Home directory during user creation in the following manner.
# useradd -m -d /home/newowner homeowner
The -d
flag sets the directory.
While changing the default user Home directory, we might also need to migrate/move already existing user content to the new location. The usermod command to implement is:
# usermod -m -d /home/newowner homeowner
The -m
flag moves the old directory content to the new one.
We can now easily change the default Home directory in Linux. I hope this article guide was helpful. Feel free to leave a comment or feedback.