Is it possible to chown a file, based on numeric UID, to a user that does not yet exist?

60,209

Solution 1

Yes, you can chown to a numerical UID that does not have a corresponding user.

Solution 2

chown UID:GID fileName can be done either with numbers or username or groupname

ex: chown 1000:1000 dirname is valid

you may have to reset the directory permission with chmod 755 for example after doing it to get access on it

Hints

  • You can check user id with id someUsername
  • You can check group id with gid someUsername
  • You can change permissions only on directories with find someLocation -type d -exec chown 1000:1000 {} \;
Share:
60,209

Related videos on Youtube

glarry
Author by

glarry

Updated on September 18, 2022

Comments

  • glarry
    glarry almost 2 years

    For example, for managing a disk partition for another system where the user exists. I know I can simply create a user temporarily but I find this question interesting.

  • glarry
    glarry over 6 years
    I tested before I asked: chown \#1005 file returns chown: invalid user: ‘#1005’.
  • Pankaj Goyal
    Pankaj Goyal over 6 years
    Do not use an octothorpe; it is not a number. Just use the number, e. g. sudo chown 1005 /path/to/file.
  • glarry
    glarry over 6 years
    According to this logic, sudo thinks it's a number. Furthermore, it thinks groups of digits that don't start with a number sign are not numbers. :)
  • glarry
    glarry over 6 years
    I first tried chown 1005 file, by the way. It didn't work, for an unrelated reason, but I blamed it on the missing number sign. You have to at least use ./file, apparently for chown to be able to tell which of the two is the user. Just so you (reader) know.
  • Hauke Laging
    Hauke Laging over 6 years
    @glarry I do not have to use ./. Is the file name really file?
  • glarry
    glarry over 6 years
    @HaukeLaging No, and it was a very well hidden typo, if you ask me. The fact that I used tab completion after ./ made the difference.
  • Stephane
    Stephane over 5 years
    Using variables chown -R $HOST_USER_ID:$HOST_GROUP_ID /usr/bin/mariadb/install/data gives me an error chown: invalid spec: '1000:' sous ` Lubuntu 16/04
  • Stephane
    Stephane over 5 years
    I could work around the issue by doing two distinct commands chown -R $HOST_USER_ID /usr/bin/mariadb/install/data; chgrp -R $HOST_GROUP_ID /usr/bin/mariadb/install/data;
  • Philippe Gachoud
    Philippe Gachoud over 5 years
    @Stephane your UID and GID must be the number of the group/id you want to change, and is setted into /etc/group and /etc/passwd or either by other system like ldap, you can refer to commands like gentent to have more infos about that.