Restoring user accounts
Q I used to have Ubuntu 8.10 installed in /dev/sda1 and /home on /dev/sda2 with four user accounts. Then I decided to download Ubuntu 9.04 and replace 8.10 on /dev/sda1. After the installation had finished, I wanted to restore the other three user accounts, but Ubuntu told me that these accounts were there already (they're home folders), so I cannot add them again. Can you please tell me how I can reinstate these three user accounts?
A This appears to be a limitation of Ubuntu's user administration tool, which refuses to create a user if the given home directory already exists. You could rename the old user directory, recreate the user, switch over the user and group IDs (UID and GID), delete the new user directory and then rename the old user directory - but that's a hell of a lot of work to go through just to use the 'easy' GUI option, and you'd have to repeat this arduous process for each user.
It is much easier to do this with the command line-based useradd tool, which doesn't create a user directory by default. First, you'll need to find the UID and GID previously used by your users, which you can do with ls. Try running the following:
sudo ls -l /home
in order to produce some results that look roughly like this:
drwxr-xr-x 2 1001 1001 4096 2009-04-16 11:07 fred
drwxr-xr-x 2 1002 1002 4096 2009-04-16 15:23 jim
drwxr-xr-x 30 nelz nelz 4096 2009-04-16 11:02 nelz
The third and fourth entries contain the UID and GID. You'll see that for the user called nelz the output shows the user and group names, but the other two - the users that haven't yet been set up - have numeric values assigned to them. Now you can recreate these users with the useradd command line tool like this:
sudo useradd --home /home/fred --uid 1001 --user-group fred
sudo passwd fred
This creates the user fred, who has a home directory of /home/fred and a UID of 1001, then asks you for a password for the user. The --user-group option creates an individual group for this user, which is the way Ubuntu handles users. If you want all users to be members of the same group (say, users), use this command instead:
sudo useradd --home /home/fred --uid 1001 --gid users fred
Repeat this for each user, then check with ls -l that each home directory is owned by the correct group and user. If this isn't the case, you can correct them with:
chown -R fred: ~fred
Although this process should have solved your immediate problem, there's no guarantee that you won't run into similar issues next time you upgrade. But rather than shy away from updating your distro, it's worth making a copy of /etc/passwd. This means that the next time you update, you can copy the relevant lines from this directory to the same file in the new installation, although you'll still need to set passwords for the users.
An alternative to the command line for this task is Webmin. It isn't in the standard Ubuntu repositories, but you can download and install a Deb package from www.webmin.com/download.html. Then point your browser at https://localhost:10000, tell it to accept the security certificate (it's your own program running locally, so a self-signed certificate is fine) and finally go to System > Users and Groups to set your users up.
Follow us on Identi.ca or Twitter