Python | os.getuid() and os.setuid() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

All functions in 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.getuid() method in Python is used to get the current process’s real user id while os.setuid() method is used to set the current process’s real user id.



User ID: In Unix-like operating systems, a user is identified by a unique value called user ID. User ID is used to determine which system resource a user is authorized to access.

Note: os.setuid() and os.getuid() methods are available only on UNIX platforms and functionality of os.setuid() method is typically available only to the superuser as only superuser can change user id.
Superuser means a root user or an administrative user who has all the permissions to run or execute any program in the operating system.

os.getuid() method

Syntax: os.getuid()

Parameter: No parameter is required

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

Code #1: Use of os.getuid() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.getuid() method 
  
# importing os module 
import os
  
# Get the real user ID
# of the current process
# using os.getuid() method
uid = os.getuid()
  
# Print the real user ID
# of the current process
print("Real user ID of the current process:", uid)

chevron_right


Output:

Real user ID of the current process: 1000

Getuid Method output

os.setuid() method


Syntax: os.setuid(uid)

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

Return Type: This method does not return any value.

Code #2: Use of os.setuid() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.setuid() method 
  
# importing os module 
import os
  
# Get the real user ID
# of the current process
# using os.getuid() method
uid = os.getuid()
  
# Print the real user ID
# of the current process
print("Real user ID of the current process:", uid)
  
# Set real user ID
# of the current process
# using os.setuid() method
uid = 1500
os.setuid(uid)
print("Real user ID changed")
  
  
# Print the real user ID
# of the current process
print("Real user ID of the current process:", os.getuid())

chevron_right


Output:

Real user ID of the current process: 0
Real user ID changed
Real user ID of the current process: 1500

Setuid-terminal-output



My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.