Changing user and group ID on Unix

Recently I’ve set up a file server for both Windows and Linux. When I went to mount the file system on Linux, things were broken – because I hadn’t paid attention to user ID and group ID when I created users on my different Unix machines.

So I needed to change the UID and GID of a user, then update the files. Luckily, someone had already done the work:

https://muffinresearch.co.uk/linux-changing-uids-and-gids-for-user/

In short:

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>

One wrinkle that I wasn’t expecting: you can’t change the user ID of a user who has a running process. So I had to create a second user with adduser, add that user as in /etc/groups for sudo, log in as that user, and then change the user ID of the original user.

When I ran this on Ubuntu and Raspbian, I saw about 4 errors in /proc which I ignored. There’s probably a faster way to do to this using xargs rather than running -exec each time, but I was a little worried I might exceed what I could pass in on a command line (I had hundreds of thousands of files) so I let it do its thing.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: