- How can I retrospectively create a default home directory for an existing user in terminal?
- 5 Answers 5
- 👥 Как создать домашний каталог для существующего пользователя Linux
- Создание домашнего каталога по умолчанию для существующего пользователя
- Заключение
- 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
- How to Create Home Directory for Existing User in Linux
- Create default home directory for existing user
- Conclusion
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.
👥 Как создать домашний каталог для существующего пользователя Linux
По умолчанию, когда вы создаете пользователя в Linux, домашний каталог создается в каталоге /home.
Возможно вы заметили, что в производных дистрибутивах Ubuntu и Debian команда useradd по умолчанию не будет создавать домашний каталог.
Давайте представим ситуацию, что вы уже создали пользователя, но домашний каталог отсутствует.
В этом руководстве я покажу вам, как создать домашний каталог по умолчанию для существующего пользователя в системах Linux.
Создание домашнего каталога по умолчанию для существующего пользователя
В примере я использую Ubuntu 20.04 и собираюсь создать пользователя с именем ‘bob’ с помощью команды useradd:
$ grep bob /etc/passwd bob:x:1003:1003::/home/bob:/bin/sh $
Если я попытаюсь войти в систему от этого пользователя, используя su -, система показывает logging in with Home=/
Это означает, что домашний каталог пользователя не создан.
$ su - bob Password: No directory, logging in with HOME=/ $
В Linux домашним каталогом пользователя по умолчанию является /home.
Чтобы создать домашний каталог по умолчанию, используйте команду mkhomedir_helper.
Обязательно запустите команду mkhomedir_helper от имени пользователя root или пользователя с доступом sudo.
$ sudo mkhomedir_helper bob
$ ls -al /home/bob total 20 drwxr-xr-x 2 bob bob 4096 Jun 1 02:26 . drwxr-xr-x 5 root root 4096 Jun 1 02:26 .. -rw-r--r-- 1 bob bob 220 Jun 1 02:26 .bash_logout -rw-r--r-- 1 bob bob 3771 Jun 1 02:26 .bashrc -rw-r--r-- 1 bob bob 807 Jun 1 02:26 .profile
В графической среде (например, GNOME или XFCE), если вам не хватает подкаталогов в домашнем каталоге, пользователь должен выйти из системы и войти обратно.
При первом входе пользователя в систему все подкаталоги, такие как папки Pictures, Documents, Videos, и Downloads , могут быть созданы в домашнем каталоге.
Другой метод – удалить пользователя и создать нового пользователя с помощью параметра -m или –create-home.
Следующая команда создает домашнюю папку (-m) и задаст указанный домашний каталог (-d) в качестве значения для входа нового пользователя:
$ sudo useradd -m -d /home/bob01 bob01
Заключение
В заключение, если вы являетесь поклонником Ubuntu, вы должны теперь использовать команду adduser.
Если у вас уже есть пользователь, теперь вы сможете добавить каталог по умолчанию.
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.
How to Create Home Directory for Existing User in Linux
By default when you create a user in Linux, users default home directory is created under /home. If you noticed on Ubuntu and Debian derivated distribution useradd command won’t create a home directory by default.
Let’s think of a situation where you have already created a user but the home directory is missing. In this tutorial, I will show you how to create a default home directory for an existing user in Linux.
Create default home directory for existing user
Here I am using Ubuntu 22.04 and going to create a user named ‘ bob’ using useradd command:
Useradd command has added an entry home directory in /etc/passwd file
$ grep bob /etc/passwd bob:x:1003:1003::/home/bob:/bin/sh
If I try to login as the user using su — , it shows that it’s logging in with Home=/ . This means the user home directory is not created.
$ su - bob Password: No directory, logging in with HOME=/
In Linux, a user’s default home directory is /home. To create a default home directory use mkhomedir_helper command.
Make sure to run mkhomedir_helper command as root or user with sudo access.
$ sudo mkhomedir_helper bob
The previous command creates a home directory named «/home/bob» and user settings files.
$ ls -al /home/bob total 20 drwxr-xr-x 2 bob bob 4096 Jun 1 02:26 . drwxr-xr-x 5 root root 4096 Jun 1 02:26 .. -rw-r--r-- 1 bob bob 220 Jun 1 02:26 .bash_logout -rw-r--r-- 1 bob bob 3771 Jun 1 02:26 .bashrc -rw-r--r-- 1 bob bob 807 Jun 1 02:26 .profile
For a graphical environment (such as GNOME or XFCE ), if you are missing subdirectories in the home directory, the user needs to log out and log in back.
When the user login the first time all subdirectories such as Pictures, Documents, Videos, and Downloads folders can be created in the home directory.
Another method is to delete the user and create a new user using -m or —create-home option.
The following command creates a home folder (-m) and set the specified home directory (-d) as the value for the new user’s login:
$ sudo useradd -m -d /home/bob01 bob01
Conclusion
To conclude, If you are a Ubuntu fan you should be now using adduser command, it’s recommended by Debian. If you have an existing user, now you should be able to add default directory.
Thanks for reading and please drop your suggestions on the below comment section.
If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks