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 –Â
Python3
# importing linraryimport osimport subprocessimport sysimport getpassÂ
# add user functiondef add_user():Â
     # Ask for the input     username = input("Enter Username ")  Â
     # Asking for users password     password = getpass.getpass()             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
