Open In App

Python | os.setreuid() method

Last Updated : 15 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

All functions in the os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.setreuid() method in Python is used to set the current process’s real and effective user IDs. In this article, we will what is setreuid in Python OS module.

Every user in a Unix-like operating system is identified by a different integer number, this unique number is called UserID. Real UserID represents an account of the owner of the process. It defines which files that this process has access to. Effective UserID is normally the 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.

Note: os.setreuid() method is only available on UNIX platforms and functionality of this method is typically available only to the superuser. A superuser means a root user or an administrative user who has all the permissions to run or execute any program in the operating system.

Python OS setreuid Method Syntax

Syntax: os.setreuid(ruid, euid)

Parameters:

  1. ruid: An integer value representing new user id for the current process.
  2. euid: An integer value representing new effective user id for the current process.

Return Type: This method does not return any value.

Set Real and Effective User IDs using os.setreuid() Method

In this example, the script fetches and displays the current real and effective user IDs. Subsequently, it modifies both IDs and showcases the updated values.

Python3




import os
 
ruid = os.getuid()
euid = os.geteuid()
 
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)
 
ruid = 100
euid = 200
os.setreuid(ruid, euid)
print("\nReal and effective user ids changed\n")
 
ruid = os.getuid()
euid = os.geteuid()
 
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)


Output

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() method output


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads