2

Are there any Gnu/Linux installers that will look at an old /home partition, to set up the users based on the directories in the root of this partition?

So if I install a new Gnu/Linux, and keep my old /home, the /home partition will be full of files owned by various users and groups. I also need to set up a load of users, these users need to match the ones from the old system, so that the file in /home have the correct ownership. The root of the /home partition have a directory for each user, that is owned by that user, and their primary group.

Therefore it should be possible to scan this directory, and create a user/primary-group for each directory that is found.

  • Are there any tools that already do this?
  • Are they used in any Gnu/Linux installed?
  • Does Debian do this?

(I am about to install a new Debian.)

4
  • I'd suggest a script that creates the users and then uses chown -R to set the owner and group for each of the user directories in /home. That would resolve the issue of the uid and gid being different for the users on the new installation. Jul 5 '19 at 17:01
  • @NasirRiley that would break ownership of any files with unexpected ownership. However I done some thing similar in the past (changing the ownership based on the ownership). Jul 5 '19 at 17:37
  • What I'm suggesting is a starting point. If you don't have a standard of ownership for the directories in /home then one thing that you could try is to get the gid and uid of all of the users and groups of your current Debian distrubution and then create new users and groups on your new Debian distribution with the same uid and gid. You can set that when you create the users and groups with the -u switch for useradd and the -g switch for groupadd. That way, the users and groups would be created effectively owning the folders as the uid and gid would match. Jul 5 '19 at 18:29
  • You can also use adduser --uid and addtroup --gid. Jul 5 '19 at 18:31
3
+25

There's no script that can do this, but UNIX/Linux shells being what they are it's quite possible to string a series of tools together to manage at least part of what you want.

#!/bin/bash
for homedir in /home/*
do
    user="${homedir##*/}"
    uid="$(stat -c %u "$homedir")"
    gid="$(stat -c %g "$homedir")"

    groupadd --gid "$gid" "$user"
    useradd --gid "$gid" --uid "$uid" --shell /bin/bash --home-dir "$homedir" --no-create-home "$user"
done

There's no error checking, but mostly groupadd and useradd will fail safely. Prefix both commands with (for example) echo to see what would happen before running it in a live environment. I would also strongly recommend that you save copies of the files /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow beforehand, so that if everything goes pearshaped you can safely restore the user database to its original values and try again.

0
0

Are there any Gnu/Linux installers that will look at an old /home partition, and set up the users based on the directories in the root of this partition?

Yes and no. If /home is on a separate partition, then any distribution should be able to use it. However, you'll have to check the UID, and GID for each user so that you can be sure that they match up in /etc/passwd, and /etc/group.

So if I install a new Gnu/Linux, and keep my old /home, the /home partition will be full of files owned by various users and groups. I also need to set up a load of users, these users need to match the ones from the old system, so that the file in /home have the correct ownership. The root of the /home partition have a directory for each user, that is owned by that user, and their primary group.

Basically, yes. You can either edit the relevant config files in /etc, or you can re-create all the users and groups, and then use chown to fix the permissions on the files in your old /home directories.

Therefore it should be possible to scan this directory, and create a user/primary-group for each directory that is found.

Not sure of any tools that do this automatically, but here are a few commands that might help.

ls -n /old_home
total 8
drwxr-xr-x 32 1000 1000 4096 Jul  5 18:26 old_user

chown -R new_user:new_group /home/old_user

Are there any tools that already do this?

Not that I know of.

Are they used in any Gnu/Linux installed?

No.

Does Debian do this?

No.

(I am about to install a new Debian.)

Nice!

1
  • 1
    It's already beed stated in the comments above that doing this won't work because all of the files and subdirectories in each home directory don't have the same ownership. Jul 6 '19 at 2:05
-2

If you update, it should respect the existing users and groups. In case of doubt, copy /etc to a safe place, install and get the users out of the backup.

Be careful, different distributions may use different UID conventions, assignment of GIDs can vary.

Make sure you save whatever local configuration you did. Once overwritten it is gone. In particular, save the list of installed packages to replicate later as needed.

2
  • This does not address the question, it only gives some background on updating OS. Maybe the first sentence does touch on it, but I am not looking to do an update. I want to do a fresh install. I have not had much luck with updates (it does not always work), and I use configuration management to re-install packages). Jul 5 '19 at 16:24
  • @ctrl-alt-delor, upgrades normally work. Unless you mess up the system by installing stuff outside the package system, that is.
    – vonbrand
    Jul 5 '19 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.