Linux Quicky. after effects of changing a uid or gid, on a user or group.

whytewolf's picture

okay, lets say you have a basic understanding of linux. and you find out you need to have the same uid and gid between a couple of servers for say NFS sharing. At this point i would point out using some shared service, like LDAP, or NIS, for user control would be idle. but it is to late. you have established systems. and you need to change these users to match between all the systems. AND you need to update files after the change.

Well, first, take note of the gid or uid you are changing from. this is important. just so you know where you are coming from. so lets say we have a user with a username of bdenver. now bdenver has a uid of 130 on HostA, but 20032 on HostB. now, HostB is the one you need to change. and things looks pretty good. you run the command to change the uid

usermod -u 130 bdenver

after of coarse making sure that no other user was using that uid right? once you have changed the uid for the user, usermod will change the files in the home directory only to match the new uid. however bdenver here had a few files in the web directory. and thats the file system we are most worried about with this. so we need to get all the files everyone to update to the new uid. well we have a single command that will search the entire system, find ALL files with the old uid and update them to the new uid. that command is

find / -uid 20032 -print0 | xargs -0 chown -h bdenver

now, some things about this command. the 20032 and bdenver are of coarse the old uid and the username change thos to match your user change. the -h for chown means it will change the ownership of links, but not the original file the link points to. also by using -print0 and -0 in find and xargs you are forcing both programs to use null ended strings instead of white space strings. allowing for spaces in names and all sorts of interesting things that normally one liners like this will blow up on. 

for group changing, just change the -uid to -gid, and the chown to chgrp [also might want to change the number to the gid of the group, and bdenver to the group name. 

Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <p>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.