What is os.geteuid() in Unix using Python?

The os.geteuid() method is used to extract the effective user ID of the current process. The inverse method of this function is seteuid(), which is used to set the effective user ID. These methods are commonly found on UNIXA multi-user operating system based systems.

Note: The OS module in Python is used to deal with operating system commands.

What is meant by an “effective user ID”?

Question

Effective user ID

Show Answer

Syntax


os.geteuid()

Parameters

N/A: It does not take any argument value(s).

Return value

This method returns an effective user ID as an integer type.

Code example

Now, let’s see an example of this method.

# Program to show working of geteuid() method
# importing mobules
import os
import random
# generating random number between 10-100
euid = random.randint(10,100)
# Assigning effective user id to current thread
os.seteuid(euid)
# Extracting id to test this method
euid= os.geteuid()
# printing euid on console
print(euid)

Code explanation

  • Line 6: We generate a random number between 10 and 100, to use an an effective user ID.
  • Line 8: We assign an effective user ID to the current process.
  • Line 10: We get the effective user ID of that process, by using the os.geteuid() method.
  • Line 12: We print the euidEffective user ID to the console.

Free Resources