📜  Python| os.setreuid() 方法

📅  最后修改于: 2022-05-13 01:55:27.993000             🧑  作者: Mango

Python| os.setreuid() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。

os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError

Python中的os.setreuid()方法用于设置当前进程真实有效的用户id。

类 Unix 操作系统中的每个用户都由不同的整数标识,这个唯一的数字称为 UserID。 Real UserID表示进程所有者的帐户。它定义了该进程可以访问哪些文件。 Effective UserID通常与 Real UserID 相同,但有时会更改它以使非特权用户能够访问只能由 root 访问的文件。

注意: os.setreuid()方法仅在 UNIX 平台上可用,并且此方法的功能通常仅对超级用户可用。
超级用户是指具有在操作系统中运行或执行任何程序的所有权限的 root 用户或管理用户。

代码: os.setreuid() 方法的使用
# Python program to explain os.setreuid() method 
  
# importing os module 
import os
  
  
# Get the current process’s 
# real user id
# using os.getuid() method
ruid = os.getuid()
  
# Get the current process’s 
# effective user id.
# using os.geteuid() method
euid = os.geteuid()
  
  
# Print the current process’s
# real and effective user id.
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)
  
  
# Change the current process’s
# real and effective user ids
# using os.setreuid() method
ruid = 100
euid = 200
os.setreuid(ruid, euid)
print("\nReal and effective user ids changed\n")
  
  
# Get the current process’s 
# real and effective user ids
ruid = os.getuid()
euid = os.geteuid()
  
  
# Print the current process’s
# real and effective user id.
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)
输出:
Real user id of the current process: 0
Effective user id of the current process: 0

Real and effective user ids changed

Real user id of the current process: 100
Effective user id of the current process: 200

os.setreuid() 方法输出