Muffinresearch Labs by Stuart Colville

Linux: Changing UIDs and GIDs for a user | Comments (2)

Posted in Code, Linux/Unix on 24th September 2008, 12:41 am by Stuart

Just had to do a quick switch over of UID and GIDs for a couple of users.

I’m using Puppet to manage server configurations and it’s got some nice features for automating the set-up of users. Unfortunately when I initially set-up the puppet directives, I overlooked setting the UID for each user which means that the UIDs assigned have been randomly created. As I want to maintain UIDs across all the boxes I’m using, it means there’s a need to migrate users’s uid’s and gids that are different.

WARNING: Messing with UIDs and GIDs can be hazardous to your sanity if it all goes pear-shaped. Using any of the scripts that follow is done entirely at your own risk.

A bunch of commands to change UIDS and GIDS

Here’s the commands to run as root to change the UID and GID for a user. Simply change the variables in angled brackets to match your settings:

usermod -u <NEWUID> <LOGIN>
groupmod -g <NEWGID> <GROUP>
find / -user <OLDUID> -exec chown -h <NEWUID> {} \;
find / -group <OLDGID> -exec chgrp -h <NEWGID> {} \;
usermod -g <NEWGID> <LOGIN>

usermod and groupmod simply change the UID and GID for their respective named counterpart usermod also changes the UID for the files in the homedir but naturally we can’t assume the only place files have been created is in the user’s homedir.

The find command recurses the filesystem from / and changes everything with uid of OLDUID to be owned by NEWUID and them changes the group for the files owned by the OLDGROUP

The final usermod command changes the login group for the user

As Jared pointed out in the comments it’s a good idea to specify -h for the chgrp and chown commands so that symlinks aren’t followed. If anything that the symlink linked to is still owned by one of the users affected this will be sorted with the find commands anyway.

Swapping Users UIDs

In my case user bar had a UID of 1001 and user foo had a UID of 1002 and I wanted to swap them over. To do that you have to run the changes with an intermediate step or everything will get in a right pickle. So much so it would be the point in the film where the “real you” enters the room and tries to strangle you.

  1. Change foo’s UID and GID from 1001 -> 1012
  2. Change bar’s UID and GID from 1002 -> 1011
  3. Change foo’s UID and GID from 1012 -> 1002
  4. Change bar’s UID and GID from 1011 -> 1001

These steps assume that 1012 and 1011 are not taken naturally.

There you have it. User foo now has a UID of 1002 and the foo group now has a GID of 1002 and user bar has a UID of 1001 and the group bar’s GID is now 1001 too. Phew!

Post Tools

Comments: Add yours

1. On February 5th, 2009 at 2:26 am Jared said:

But, note that chown and chgrp follow symbolic links, altering the file pointed to rather than the link itself, unless you used the -h switch. If a user creates a link to a system file, they get ownership of it when you run your commands. You may not have known if this had happened unless you also used the -v switch to report all operations or ran find again after making the changes. :(

2. On February 5th, 2009 at 10:03 am Stuart Colville said:

@Jared: Very good point well made. I’ve updated the post to reflect the need to use -h.







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Using Loggerhead with mod_wsgi|(0)

Here’s a post I wrote over on the Project Fondue Blog about our use of Loggerhead with mod_wsgi under Apache. Loggerhead is the rather nice branch viewer for bazaar branches as used on Launchpad.net.

If you’re not already subscribed to the Project Fondue blog feed then I can recommend it, as there should be some interesting posts coming out of there in the coming months (yes I’m unashamedly biased!).

Ubuntu: Turn off changing workspace with mouse wheel|(1)

I found the changing with the workspace with the mouse wheel really annoying. To disable it go to System => Preferences => CompizConfig (available if the compizconfig-settings-manager package is installed) and uncheck “Viewport Switcher” which is under the “Desktop” heading.

Photos on Flickr

© Copyright 2004-10 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.576s.