Every Linux user is assigned a default home directory when it is created. Whenever a user logs into Linux via SSH, they are logged into their default home directory. In most cases, a Linux user does not have permission to access other folders. But sometimes you may need to change default home directory of user in Linux. In this article, we will look at how to change default home directory of user in Linux.
How to Change Default Home Directory of User in Linux
Here are the steps to change default home directory of user in Linux. There are two ways to change user directory. We will look at each case separately. You will need to log in as user with root or sudo privileges to be able to run the following commands. You can run these commands on almost all Linux distributions since they are available by default in most Linux systems.
1. Change User’s Home Directory
If you only need to change user ubunut’s home directory from /home/ubuntu to /home/data then open terminal and run the following command.
# usermod -d /home/data ubuntu
In the above command, we need to use usermod command with -d option to change user’s home directory. After the -d option, we need to specify the absolute path to new home folder, followed by the username whose home directory you need to change.
2. Change Home Directory + Move Content of Current Directory
If you also want to move content of current directory, along with changing home directory, then use -m option with usermod command.
# usermod -m -d /home/data ubuntu
In the above command, -m option will move all contents from your current folder to the new home folder /home/data.
3. Change default directory during user creation
The above two commands are to be used if you already have an existing user whose directory you want to change. However, if you want to set the default home folder of new user when you create it, then you need to use useradd command. Here is an example to change user ubuntu’s default folder to /home/data instead of /home/ubuntu.
$ sudo useradd -m -d /home/data ubuntu
That’s it. In this article, we have learnt how to easily change default home folder of user in Linux.