Python | os.geteuid () and seteuid () method

Python Methods and Functions

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.geteuid () in Python is used to get the effective user id of the current process, whereas os.seteuid () is used to set the effective user ID of the current process.

The effective user ID : This is usually the same as the real user ID, but it has been modified to allow an unprivileged user to access files that can only be accessed by the root user. An effective user ID is used for most access checks. It is also used as the owner of files created by the process.

Note: os.seteuid() and os.geteuid() are only available on UNIX platforms, and the functionality of the os. seteuid () is usually only accessible to the superuser, since only the superuser can change the user ID. 
Superuser — this is a root user or administrator who has full rights to launch or run any program on the operating system.

os.geteuid () method

Syntax: os.geteuid ()

Parameter: No parameter is required

Return Type: This method returns an integer value which represents the current process`s effective user id.

Code # 1: Using the os.geteuid () method

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

 
# import of the os module

import os

 
# Get effective user ID ale
# of the current process
# using the os.geteuid () method

euid = os.geteuid ()

 
# Print effective user ID
# of the current process

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

Exit:

 Effective user ID of the current process: 1000 

os.seteuid () method

Syntax: os.seteuid (euid)

Parameter:
euid : An integer value representing new effective user ID for the current process.

Return Type: This method does not return any value.

Code # 2: Using the os.seteuid () method

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

 
# import of the os module

import os

 
# Get the effective user ID
# of the current process
# using the os.geteuid () method

euid = os.geteuid ()

  
# Print effective user ID
# of the current process

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

 
# Change effective user ID
# of the current process
# using the os.seteuid () method

euid = 100

os.seteuid (euid)

print ( "Effective user ID changed" )

 
# Print effective identifier user
# of the current process

euid = os.geteuid ()

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

Exit :

 Effective user ID of the current process: 0 Effective user ID changed Effective user ID of the current process: 1000 





Get Solution for free from DataCamp guru