Creating a user via command line in Linux is a tedious task. Every time someone joins your organization and you need to type a long Linux command rather than doing this you can create a python script that can ask for you the username and password and create that user for you.
Examples:
Input : Enter Username : John Password: **** Output : User successfully created with given credentials
Below is the Python code –
# importing linrary import os import subprocess import sys import getpass # add user function def add_user(): # Ask for the input username = input("Enter Username ") # Asking for users password password = getpass.getpass() 4 try: # executing useradd command using subprocess module subprocess.run(['useradd', '-p', password, username ]) except: print(f"Failed to add user.") sys.exit(1) add_user() |
Output:

After successfully creating the user type, use this command to get details of new user –
cat /etc/passwd

Recommended Posts:
- Deleting a User in Linux using Python Script
- Python | Add Logging to a Python Script
- Run python script from anywhere in linux
- Python script to change MAC address of Linux machine
- Get Your System Information - Using Python Script
- Python script to open a Google Map location on clipboard
- Autorun a Python script on windows startup
- Python | Accepting Script Input
- Python Script to Shutdown Computer
- Python Script to Restart Computer
- Python Script to Logout Computer
- Convert Python Script to .exe File
- Create a Python Script Notifying to take a break
- Python script to get device vendor name from MAC Address
- Plot multiple separate graphs for same data from one Python script
- Python script to shows Laptop Battery Percentage
- Python Script to change name of a file to its timestamp
- Python Script to check PC last reboot time
- Send message to Telegram user using Python
- Setup API for GeeksforGeeks user data using WebScraping and Flask
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.

