4

My situation

I inherited a collection of four Linux boxes where no care was taken to keep UIDs unique across systems. There are ~40 users and over 7TB of data owned collectively by those users.

In order to have some sanity when NFS mounting /data volumes between these four boxes, I need to get the UIDs matching. I also want to minimize the amount of time users are asked to stay logged out while I perform this change.

Because of the number of users and amount of data, I have written a program to chown (in one pass of the ~7TB) all the files from current state UIDs to fixed state UIDs for all 40 users. At the end of this, I'll need to change /etc/passwd so that the usernames match the new fixed state UIDs.

This would be simpler if I only had one user to fix and/or if there were less data to crawl through and chown.

I had thought that after running my program to change file permissions I would simply run

usermod -u fixedUID jsmith -o

For each of the 40 users. However, the manual for usermod suggests it is going to do it's own chown of the files in /home/jsmith (thus defeting my attempt at a simple pass fix). I can't figure out how to disable this and I'm worried the final usermod step is going to turn my 1-pass fix into a 41-pass fix.

My question

How would you change the users UIDs after chowning everything on the file system? Is there a better way than "vipw" ?

1 Answer 1

3

Try changing the user's home directory temporarily (e.g. to /tmp/home/username), changing the uid, and then changing the homedir back to what it was.

For example:

# mkdir -p /tmp/home/jsmith  ## uncomment if required.
usermod -d /tmp/home/jsmith jsmith
usermod -u fixedUID jsmith -o
usermod -d /home/jsmith jsmith

If the user's home directory isn't /home/username, you can extract the real home directory with getent:

# mkdir -p /tmp/home/jsmith  ## uncomment if required.
homedir=$(getent passwd jsmith | awk -F: '{print $6}')
usermod -d /tmp/home/jsmith jsmith
usermod -u fixedUID jsmith -o
usermod -d "$homedir" jsmith
2
  • 1
    you could get the shell to do the getent work for you: homedir=~jsmith.
    – meuh
    Mar 6, 2016 at 8:34
  • true, but I like getent and think it's under-utilised and not as widely known as it should be. Also, using it in an answer is a good way of highlighting the fact that it's better to use getent than to, e.g., just run awk or grep or whatever on /etc/passwd (depending on nsswitch or pam etc, /etc/passwd may not be the only source of login details).
    – cas
    Mar 6, 2016 at 11:16

You must log in to answer this question.

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