Python Glossary Русский

all Python 3Linux · macOSosfunction

os.setgid

os — user/group IDs

Set the process's real group id. Availability: Unix.

Syntax

os.setgid(...)

Call it before setuid: after the UID changes the process no longer has the right to change its group. A full drop also needs supplementary groups cleared via setgroups([]) or set via initgroups — otherwise the process keeps access through root's groups.

Examples

import os
print(os.getgid())  # → ? только Unix: реальный GID процесса, например 1000
os.setgid(os.getgid())  # → ? только Unix: установка того же GID проходит без ошибки
os.setgid(65534)  # → ? только Unix: root навсегда переходит в группу nogroup — вернуться уже нельзя
print(os.getgid(), os.getegid())  # → ? 65534 65534: у root setgid меняет и реальный, и эффективный GID
os.setgid(0)  # → PermissionError, если процесс запущен не от root

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy