Unix UID 有多大(以位元為單位)?

Unix UID 有多大(以位元為單位)?

據我了解,unix 使用者 ID (UID) 通常是 16 或 32 位元無符號整數,但我如何找到任何給定係統(在 shell 中)?

答案1

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

最新的作業系統(Solaris 2.x、OS X、BSD、Linux、HP-UX 11i、AIX 6)可以處理多達 20 億個 ( 2^31-2),因此我會假設這一點,並為那些不支持的更模糊的系統提供解決方法't。

答案2

glibc 提供了所有這些系統類型的定義。

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

% grep UID_T /usr/include/bits/typesizes.h
#define __UID_T_TYPE            __U32_TYPE

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

% grep '#define __U32_TYPE' /usr/include/bits/types.h
#define __U32_TYPE              unsigned int

這可以讓您找出 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

所以,這裡有一句話:

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

這裡的U意思是unsigned(這也可以是Sfor signed)並且32是大小(在上面的列表中查找它;我認為,大多數時候你可以假設這已經是字節大小了,但是如果你希望你的腳本完全可移植)可能會更好地打開case這個值)。

答案3

這個連結提出問題,響應者使用試誤法來確定有問題的系統使用有符號長整型,留下 31 位元來儲存值,最大值為 2,147,483,647。

# groupadd -g 42949672950 testgrp
# more /etc/group
testgrp:*:2147483647:

答案4

這是一個有趣的問題。如果有一個標準的、可移植的方法來確定這一點,我會感到驚訝。

我手邊沒有 Linux 機器,但idFreeBSD 8.0 上的指令會回零:

# id 4294967296
uid=0(root) gid=0(wheel) groups=0(wheel),5(operator)

我確信這是未定義的行為,但我敢打賭,大多數版本id要么會換行為零65'536(如果是 16 位元 UID),4'294'967'296要么會在超出系統限制時出錯。

相關內容