Python| os.getresuid() 和 os.setresuid() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError 。
Python中的os.getresuid()
方法用于获取当前进程真实、有效、保存的用户id, os.setresuid()
方法用于设置当前进程的真实、有效、保存的用户id。
类 Unix 操作系统中的每个用户都由不同的整数标识,这个唯一的数字称为 UserID。为一个进程定义了三种UserID,可以根据任务的权限动态改变。
Real UserID : It is account of owner of this process. It defines which files that this process has access to.
Effective UserID : It is normally same as Real UserID, but sometimes it is changed to enable a non-privileged user to access files that can only be accessed by root.
Saved UserID: It is used when a process is running with elevated privileges (generally root) needs to do some under-privileged work, this can be achieved by temporarily switching to non-privileged account.
While performing under-privileged work, the effective UID is changed to some lower privilege value, and the euid is saved to saved userID(suid), so that it can be used for switching back to privileged account when task is completed.
注意: os.setresuid()
和os.getresuid()
方法仅在 UNIX 平台上可用,并且os.setresuid()
方法的功能通常仅对超级用户可用,因为只有超级用户可以更改进程的 ID。
超级用户是指具有在操作系统中运行或执行任何程序的所有权限的 root 用户或管理用户。
os.getresuid() 方法——
Syntax: os.getresuid()
Parameter: No parameter is required
Return Type: This method returns a tuple whose attributes represents current process’s real, effective, and saved user ids.
# Python program to explain os.getresuid() method
# importing os module
import os
# Get the current process’s
# real, effective, and saved user ids.
# using os.getresuid() method
ruid, euid, suid = os.getresuid()
# Print the current process’s
# real, effective, and saved user ids.
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)
print("Saved user id of the current process:", suid)
输出:
os.setresuid() 方法——
Syntax: os.setresuid(ruid, euid, suid)
Parameters:
ruid: An integer value representing new user id for the current process.
euid: An integer value representing new effective user id for the current process.
suid: An integer value representing new saved user id for the current process.
Return Type: This method does not return any value.
# Python program to explain os.setresuid() method
# importing os module
import os
# Get the current process’s
# real, effective, and saved user ids.
# using os.getresuid() method
ruid, euid, suid = os.getresuid()
# Print the current process’s
# real, effective, and saved user ids.
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)
print("Saved user id of the current process:", suid)
# Change the current process’s
# real, effective, and saved user ids
# using os.setresuid() method
ruid = 100
euid = 200
suid = 300
os.setresuid(ruid, euid, suid)
print("\nReal, effective, and saved user ids changed\n")
# Get the current process’s
# real, effective, and saved user ids.
# using os.getresuid() method
ruid, euid, suid = os.getresuid()
# Print the current process’s
# real, effective, and saved user ids.
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)
print("Saved user id of the current process:", suid)
输出: