Unix UID 有多大(以位为单位)?

Jos*_*sef 23 linux unix uid

我知道 unix 用户 ID (UID) 通常是 16 位或 32 位无符号整数,但是我怎样才能找到任何给定系统(在 shell 中)?

小智 14

您需要查看<limits.h>(或它包含的文件之一,例如sys/syslimits.h在 OS X 上)#defineUID_MAX.

最新的操作系统(Solaris 2.x、OS X、BSD、Linux、HP-UX 11i、AIX 6)最多可以处理 20 亿 ( 2^31-2) '不。

  • `login.defs` 的 [manpage](http://linux.die.net/man/5/login.defs) 表明在这种情况下,`UID_MAX` 只控制将自动分配给新用户的最高 uid使用 `useradd` 创建的用户。 (6认同)
  • 大多数系统使用 /etc/login.defs ,它确实将 UID_MAX 设置为最高可用 UID 值,在我检查过的任何系统上为 60000。 (5认同)
  • 不幸的是,没有“UID_MAX”这样的东西。例如,“shadow-utils”中的工具使用“(uid_t)-1”来找出 UID 的最大值。 (2认同)
  • 它可能是 2^32(40 亿而不是 2)。在 RHEL _UID 上,4,294,967,295 (2^32-1) 通常是为“无效值”UID 保留的,而 4,294,967,294 (2^32-2) 在某些操作系统中是为 nfsnobody 用户保留的。因此最大非保留值为 4,294,967,293 (2^32-3)_ (2认同)

kir*_*gin 6

glibc 为所有这些系统类型提供了定义。

您可以检查/usr/include/bits/typesizes.h

% grep UID_T /usr/include/bits/typesizes.h
#define __UID_T_TYPE            __U32_TYPE
Run Code Online (Sandbox Code Playgroud)

接下来你看看/usr/include/bits/types.h

% grep '#define __U32_TYPE' /usr/include/bits/types.h
#define __U32_TYPE              unsigned int
Run Code Online (Sandbox Code Playgroud)

这可以让您找出 C 类型。由于您需要以字节为单位的大小,因此您最好的选择是根据以下规范解析 typedef 名称types.h

We define __S<SIZE>_TYPE and __U<SIZE>_TYPE for the signed and unsigned
variants of each of the following integer types on this machine.

 16      -- "natural" 16-bit type (always short)
 32      -- "natural" 32-bit type (always int)
 64      -- "natural" 64-bit type (long or long long)
 LONG32      -- 32-bit type, traditionally long
 QUAD        -- 64-bit type, always long long
 WORD        -- natural type of __WORDSIZE bits (int or long)
 LONGWORD    -- type of __WORDSIZE bits, traditionally long
Run Code Online (Sandbox Code Playgroud)

所以,这是一个单行:

% grep '#define __UID_T_TYPE' /usr/include/bits/typesizes.h | cut -f 3 | sed -r 's/__([US])([^_]*)_.*/\1 \2/'
U 32
Run Code Online (Sandbox Code Playgroud)

这里的U意思是unsigned(这也可以S用于signed)和32大小(在上面的列表中查找;我认为,大多数时候你可以假设它已经以字节为单位的大小,但如果你希望你的脚本完全可移植它case打开这个值可能会更好)。