2

What is the maximum number of characters that a user name or group name may be on Linux?

I need to allocate a buffer and would like to know how much space I need to allocate to guarantee it is large enough for whatever group or user name my application might encounter.

ekad
  • 14,436
  • 26
  • 44
  • 46
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
  • 2
    This might very well be programming related, e.g. if the OP wants to know how much buffer space he must, at most, allocate for storing user/group names. E.g. consider getpwnam_r() and sysconf(_SC_GETPW_R_SIZE_MAX). – janneb May 15 '11 at 16:21
  • 1
    Or to answer the question partially, see the LOGIN_NAME_MAX macro which must be at least 9 but might be bigger; On Linux it seems to be 256. For group name limits, I don't know. – janneb May 15 '11 at 16:31
  • I've edited the question to clarify that this is indeed a programming related question since the string-length tag was too subtle. – WilliamKF May 15 '11 at 16:33
  • Why was this closed AFTER the programming nature was clarified? – Chris Stratton May 15 '11 at 17:16

1 Answers1

4

(Putting my comment into an answer now that the question has been reopened)

POSIX specifies that LOGIN_NAME_MAX must be >= _POSIX_LOGIN_NAME_MAX. _POSIX_LOGIN_NAME_MAX, in turn, is defined to 9. On Linux it seems LOGIN_NAME_MAX is 256.

For groups, I don't think there is anything similar. Some kind of upper bound can be guesstimated via the getgrnam_r() and getgrgid_r() functions, which take a user supplied buffer for the char* entries in struct group. The maximum needed size for this buffer can be retrieved via sysconf(_SC_GETGR_R_SIZE_MAX) or the macro NSS_BUFLEN_GROUP. On Linux, NSS_BUFLEN_GROUP seems to be 1024.

janneb
  • 36,249
  • 2
  • 81
  • 97