Skip to content
Related Articles

Related Articles

Improve Article
Add a User in Linux using Python Script
  • Last Updated : 04 May, 2020

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.


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
         # executing useradd command using subprocess module['useradd', '-p', password, username ])      
         print(f"Failed to add user.")                     

user add output

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

cat /etc/passwd


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :