How to make user home folder after account creation? [duplicate]
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?
26 Answers
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.
If you forgot to use -d -m, the best and quick option is to run
sudo mkhomedir_helper usernamewith creates the home directory as if you would have provided the missing options.
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 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.
To change the default value of the new user's home directory, you can give
sudo useradd -D --base-dir /home/new_usercommand. See useradd -D [options] from
man useradd 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