89

I created a new user with useradd name and forgot to use -d -m to create their home directory. I tried making one, copying the contents of /etc/skel, and chowning everything to the new user.

Aliases don't work, such as ll, and I just have a $ at the command prompt, instead of name@server ~$. Also, using the scroll wheel dumps garbage on the command line :(

How do I fix this, or is it easier to delete the user and start over?

2
  • 1
    I haven't figured out what else was needed from that point, but I fixed it by deleting the user and directory and starting over with adduser instead of useradd.
    – mortona42
    Jun 18, 2012 at 19:50
  • 1
    I think that would result in a different userid and groupid which may or may not be a problem for you.
    – grantbow
    Aug 26, 2014 at 9:43

6 Answers 6

82

You have $ at the command prompt because you are using the sh shell.

The shell with name@server is based on the bash shell.

You have to change the default shell for the newly created user via : usermod -s /bin/bash .

Using usermod again to add the user home directory if it wasn't present. usermod -d /home/username

If the user has no home directory specified in /etc/passwd :

Run mkhomedir_helper <username> to create the home directory. mkhomedir_helper will create the user home directory and copy the stuff in /etc/skel as well.

If the user already has a home directory specified in /etc/passwd :

Such as via usermod -d /some/directory , mkhomedir_helper will not work. The only way is to manually create the home directory for the affected user.

6
  • My output from that last command was 'usermod: no changes' even though there's no directory in the home folder
    – AlxVallejo
    May 8, 2014 at 12:45
  • @AlxVallejo , thanks for the comment. I did some testing and edited my answer accordingly. Hopefully it helps answer your question as well.
    – iridescent
    Jun 20, 2014 at 14:54
  • @iridescent Thanks for your testing. The hint about the shell is helpful. I think your description of the two cases is almost complete but unintentionally misleads a bit by describing "the only way" at the end. Indeed, the home directory must be specified in /etc/passwd. This is independent of /home having a <username> directory on disk, preventing mkhomedir_helper from completing. A quick rm -rf /home/<username> should allow mkhomedir_helper <username> to complete the tasks manually performed by the @Zenexer script below.
    – grantbow
    Aug 26, 2014 at 10:19
  • 9
    sudo mkhomedir_helper <username> worked for me. mkhomedir_helper fails silently without sudo.
    – mv288
    Dec 8, 2014 at 10:10
  • I had to use mkhomedir_helper my_user 0077 in order to match other home directories (700 or drwx------.)
    – Boop
    May 5, 2017 at 9:00
34

If you forgot to use -d -m, the best and quick option is to run

sudo mkhomedir_helper username

with creates the home directory as if you would have provided the missing options.

6

Here's a quick bash script. Run as root with sudo. It takes any number of arguments, each being a username in need of a home directory. This makes a few assumptions: that your home directories are in /home, and that your skeleton directory is /etc/skel. These are the defaults on Ubuntu. You can download or wget/curl this script from gist.

#!/bin/bash
if [ $# -lt 1 ]; then
    echo "Syntax: $_ USER[ USER[ ...]]" >&2
    exit 1
fi

exit_code=0

for user in "$@"; do
    home="/home/$user"
    cp -R /etc/skel "$home" && echo $'\e[32m'"Copied skeleton to: $home"$'\e[m' || ( exit_code=$?; echo $'\e[31m'"Failed to create: $home"$'\e[m' ) >&2
    chown -R "$user:$user" "$home" && echo $'\e[32m'"Set owner on: $home"$'\e[m' || ( exit_code=$?; echo $'\e[31m'"Failed to set owner on: $home"$'\e[m' ) >&2
done

exit $exit_code
6

Take a look at the xdg-user-dirs-update command. It will create the default X windows directories: Desktop, Download, etc...

A user without the the the default directories can run xdg-user-dirs-update --force to create the directories. I had to do this for one of my user accounts.

I recommend reading the man page before running the xdg-user-dirs-update command. The man page for xdg-user-dir adds a few more details.

The "XDG Base Directory Specification" is part of the freedesktop.org specifications.

4

To change the default value of the new user's home directory, you can give

sudo useradd -D --base-dir /home/new_user

command. See useradd -D [options] from

man useradd 
4
  • When I did echo $HOME after just using useradd, it gave me /home/new_user, so I don't think that was the problem, unless that wasn't a good indicator.
    – mortona42
    Jun 18, 2012 at 21:04
  • 3
    This answer is irrelevant to the question. I only say this because this command is for changing the default values on your system, and can cause damage if you use it expecting something else.
    – Zenexer
    Jun 20, 2013 at 7:33
  • It seems that I wrongly understand what --base-dir option does. According to the useradd man page, --base-dir option does "The default base directory for the system if -d HOME_DIR is not specified. BASE_DIR is concatenated with the account name to define the home directory.". So I thought that this option will move old user's home to new user's home.
    – numand
    Jul 26, 2015 at 14:58
  • 1
    This is NOT correct. In fact, this causes all future user homes to be created under /home/new_user/new_user instead of just /home/new_user. To correct this, do sudo useradd -D --base-dir /home Sep 5, 2023 at 17:45
3

I just ran into this (Ubuntu 12.04) and I solved it by creating a temp user, copying over the user directory, chowning it, and finally deleting the temp user.

sudo adduser temp
sudo cp -r /home/temp /home/name
sudo chown -R name.name /home/name
sudo deluser temp
1
  • How do I change the $HOME for "name"? Dec 2, 2013 at 3:16

Not the answer you're looking for? Browse other questions tagged .