- How can I retrospectively create a default home directory for an existing user in terminal?
- 5 Answers 5
- How to Change Default User Home Directory in Linux
- Benefits of Having a Linux Home Directory
- Changing Default User Home Directory in Linux
- Change Home Directory in Linux
- Change the home directory using usermod
- Change the home directory by editing /etc/passwd
- How to change the Home directory of the currently logged In user
- 5 Answers 5
How can I retrospectively create a default home directory for an existing user in terminal?
I created a user without a home directory and now I want to create a home directory for them. Not just a folder called /home/new-user , but a complete default home directory with all the normal folders and hidden files, etc. How can I do that?
When the user logs in, those subdirectories (Documents, Downloads, etc. ) will be automatically created. Although I’m searching for ways to «simulate» his login through the terminal.
@RaduRădeanu yeah I already tried that, also tried sudo -i -u new_user but it doesn’t work. I think we need to «simulate an X login». don’t know how to do that.
5 Answers 5
Use the following (as root, or with sudo if not root):
mkhomedir_helper username
For this to work, folder /home/username must not exist.
For X-related folders (Desktop, Downloads, etc), you will need to login in a graphics environment; they will be automatically generated the first time you login.
Simple and useful. This way you also can be sure that the newly created home dir is compliant with your system policies, without ever knowing them or any manual tinkering.
Don’t forget to check the directory’s permissions with ls -l /home . I found that the new directory was readable by all other users, so I removed those permissions with chmod go-r-x /home/username .
The subdirectories (Documents, Downloads, etc. ) are automatically created when the user first logs in through GNOME, provided that the home directory is created with the correct permissions. Here’s a demonstration:
alaa@aa-lu:~$ sudo useradd testinguser alaa@aa-lu:~$ sudo passwd testinguser Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully alaa@aa-lu:~$ sudo ls -l /home total 20 drwxr-xr-x 55 alaa alaa 4096 Aug 22 22:00 alaa drwx------ 2 root root 16384 Jun 5 09:46 lost+found alaa@aa-lu:~$ sudo mkdir /home/testinguser alaa@aa-lu:~$ sudo chown testinguser:testinguser /home/testinguser alaa@aa-lu:~$ ls -l /home total 24 drwxr-xr-x 55 alaa alaa 4096 Aug 22 22:00 alaa drwx------ 2 root root 16384 Jun 5 09:46 lost+found drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:03 testinguser alaa@aa-lu:~$ ls -l /home/testinguser/ total 0 alaa@aa-lu:~$
You can check that the user’s home directory is correctly set by checking the entry in /etc/passwd . You should, by default, see the home directory set to /home/testinguser :
alaa@aa-lu:~$ grep testinguser /etc/passwd testinguser:x:1001:1001::/home/testinguser:/bin/sh
If you don’t see the home directory /home/testinguser there, you’ll need to execute the command sudo usermod -d /home/testinguser testinguser to update it, although you should not need to use this command because it should be set by default (according to useradd ‘s manpages).
I then logged out of my account, and logged back in with testinguser , and here are the subdirectories automatically created:
alaa@aa-lu:~$ ls -l /home/testinguser/ total 36 drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Desktop drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Documents drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Downloads drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Music drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:07 Pictures drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Public drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Templates drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Videos
I didn’t need to copy the contents of /etc/skel .
If possible, can you please try following these steps, creating another new user? Once you’re done, you can remove this new user by sudo deluser testinguser && sudo rm -r /home/testinguser .
If all of this did not work with you, then I’m guessing it’s a bug.
How to Change Default User Home Directory in Linux
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:
For instance, if a Linux system has a user called dnyce, then the absolute path to this user’s Home directory will be:
We can even list the permission settings associated with the user in the following manner:
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.
Switch back to the root user account.
and create a new directory replacement for the default user Home directory.
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.
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.
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/
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 :
> sudo usermod -d /var/www/html myuserName
usermod: user myUserName is currently logged in
That helps, But i would like to change my home directory permanently, not for just the current session.
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 :
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 .
editing /etc/passwd for an account that is logged in, and then start a new session with that same account, and it obeys the new home dir. For the already logged in account, that session still has the old home location in the Environment.
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.