+

Python | os.setreuid () method

All functions in the os module raise OSError for invalid or unreachable file names and paths or other arguments that are of the correct type but are not accepted by the operating system.

os.setreuid () in Python is used to set the real and effective user IDs of the current process.

Every user on Unix is such an operating system is identified by a different integer, this unique number is called the UserID.  Real UserID represents the account of the owner of the process. It determines which files this process has access to.  The effective user ID is usually the same as the real user ID, but sometimes it is changed to allow an unprivileged user to access files that can only be accessed by the root user.

Note : os.setreuid() is only available on UNIX platforms, and the functionality of this method is usually only available to the superuser. 
Superuser — it is a root user or administrator who has full rights to launch or run any program in the operating system.

Syntax: os.setreuid (ruid, euid)

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.

Return Type: This method does not return any value.

Code: using the os.setreuid () method

# Python program to explain the os.setreuid () method

 
# import of the os module

import os

 

  
# Get the current process
# real user ID
# using the os.getuid () method

ruid = os.getuid ()

 
# Get the current process
# effective user id.
# using the os. geteuid ()

euid = os. geteuid ()

 

 
# Print current process
# real and effective user ID.

print ( "Real user id of the current process:" , ruid)

print ( "Effective user id of the current process:" , euid)

 

  
# Change current process
# real and effective user IDs
# using the os.setreuid () method

ruid = 100

euid = 200

os.setreuid (ruid, euid)

print ( "Real and effective user ids changed " )

  

  
# Get the current process
# real and effective user IDs

ruid = os.getuid ()

euid = os.geteuid ()

 

 
# Print current process
# real and effective user ID.

print ( "Real user id of the current process:" , ruid)

print ( "Effective user id of the current process:" , euid)

Exit :

 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 

Get Solution for free from DataCamp guru