Changing user and group ID system wide (including file system attributes)?

15,997

If I understand your question correctly, you can use find's -gid condition:

find /media/extdrive -gid 100 | sudo xargs chown myself:myself 

or if you prefer find's -exec:

sudo find /media/extdrive -gid 100 -exec chown myself:myself {} +

or, looking at chown's manpage, it has a --from option you may find useful:

sudo chown -R myself:myself --from=:100

See the appropriate manpages for more information.

Share:
15,997

Related videos on Youtube

sdaau
Author by

sdaau

Updated on September 18, 2022

Comments

  • sdaau
    sdaau almost 2 years

    (this question possibly related to Linux: simulating/masking user ownership upon mount of 'external' partitions? - Stack Overflow)

    I typically use Ubuntu (currently 11.04) as my desktop OS; let's say there, I'm the user myself with uid=1000, part of the group myself with gid=1000; so basically the home directory permissions look like this:

    > stat ~
      File: `/home/myself'
    ...
    Access: (0755/drwxr-xr-x)  Uid: ( 1000/   myself)   Gid: (  1000/   myself)
    

    Now, on the same machine I sometimes need to boot OpenSuse (currently 11.2) through a LiveUSB (Live-CD). All of that works fine for me, except that the default user in a Live-CD environment is linux with uid=999, part of the group users with gid=100; so there the home directory permissions look like this:

    > stat ~
      File: `/home/linux'
    ...
    Access: (0755/drwxr-xr-x)  Uid: ( 999/   linux)   Gid: (  100/   myself)
    

     

    So, basically, when I mount my Ubuntu drives (which are owned by 1000:1000) under the live OpenSUSE (whose default user is 999:100), I fail to access them as usual, i.e.:

    linux@linux:~> touch /media/extdrive/xxx
    touch: cannot touch `/media/extdrive/xxx': Permission denied
    

     

    So, I thought I should try to change the default uid/gud for linux:users on the OpenSUSE machine, so I tried this:

    > sudo /usr/sbin/usermod -u 1000 linux
    usermod: Account `linux' is currently in use.
    
    > id linux
    uid=999(linux) gid=100(users) groups=100(users),33(video)
    
    # so if that fails.... change directly from /etc/passwd? 
    > sudo nano /etc/passwd
    
    > id linux
    uid=1000(linux) gid=100(users) groups=100(users),33(video)
    

    ... and just having the uid changed, makes me access the Ubuntu drives fine - however, now I cannot access the OpenSUSE stuff previously owned by linux:users:

    > sudo nano /etc/fstab
    sudo: unknown uid: 999
    

    ... so, it turns out, bad idea to change /etc/passwd directly :) The solution here seems to have been to drop to root shell first, do the usermod command, reboot - and now I have on from the OpenSUSE environment:

    linux@linux:~> stat ~
    Access: (0755/drwxr-xr-x)  Uid: ( 1000/   linux)   Gid: (  100/   users)
    

    This makes me - for the most part - have access to the Ubuntu filesystems/drives as usual; while also having usual access to whatever is owned by the default user in OpenSUSE.

     

    However, to be on the safe side, I'd also like to change the group ID. What would be the recommended way of doing this ( and what would the recommended way be to change the uid, if I didn't do it right in the discussion above )?

    Many thanks in advance for any answers,
    Cheers!

  • sdaau
    sdaau over 12 years
    Many thanks @Kevin - that is exactly what I was looking for! Actually, I forgot to say: since I believe usermod somehow changed the uid's of files without traversing the file system, is there an alternative to the traversing done by find? E.g. is there something like groupmod -g 1000 users (where users originally has gid=100)? Cheers!
  • Kevin
    Kevin over 12 years
    Looking at the manpages, if you use usermod to change a user's uid or gid, it automatically changes the file ownership within the affected user's home directory only, and outside there and using groupmod, you have to change the ids manually.
  • sdaau
    sdaau over 12 years
    Cheers for that @Kevin, that settles it for me. Many thanks again!