18

How do I create a user in Linux using Python? I mean, I know about the subprocess module and thought about calling 'adduser' and passing all the parameters at once, but the 'adduser' command asks some questions like password, full name, phone and stuff. How would I answer this questions using subprocess? I've seen module called pexpect in this question: Can I use Python as a Bash replacement?. Is there any other standard module?

Community
  • 1
  • 1
Salsa
  • 917
  • 2
  • 13
  • 22

6 Answers6

24

Use useradd, it doesn't ask any questions but accepts many command line options.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
7

On Ubuntu, you could use the python-libuser package

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Is this a ubuntu-only package? If I need to run it on other distros, will I have to manually install this package? – Salsa Jul 07 '10 at 13:30
  • 1
    You are probably better off with `useradd` for cross-distribution compatibility, but note that even `useradd` is not guaranteed to be available on all distros. Puppy Linux I believe does not come with useradd by default, for example. – unutbu Jul 07 '10 at 14:05
  • `python-libuser` link is dead => https://packages.ubuntu.com/source/xenial/libuser – alper Apr 20 '20 at 11:47
5
import os
import crypt

password ="p@ssw0rd" 
encPass = crypt.crypt(password,"22")
os.system("useradd -p "+encPass+" johnsmith")
Jonathan Rioux
  • 1,067
  • 2
  • 14
  • 30
3

You could just use the built-in binaries so just call useradd or something through the subprocess module, However I don't know if there's any other modules that hook into Linux to provide such functionality.

Not Available
  • 3,095
  • 7
  • 27
  • 31
2
def createUser(name,username,password):
    encPass = crypt.crypt(password,"22")   
    return  os.system("useradd -p "+encPass+ " -s "+ "/bin/bash "+ "-d "+ "/home/" + username+ " -m "+ " -c \""+ name+"\" " + username)
Levon
  • 138,105
  • 33
  • 200
  • 191
mcolak
  • 609
  • 1
  • 7
  • 13
  • 1
    What's the number 22 for? Also, would subprocess be better than os.system? – answerSeeker Feb 25 '17 at 14:38
  • @answerSeeker it is salt. salt is a 2-character string which will be used to select one of 4096 variations of DES. (from pydoc) – yanpas Apr 28 '17 at 15:08
  • 4
    sorry for the necrobump, but crypt can generate a salt for you, no need to specify. salt = crypt.mksalt(crypt.METHOD_SHA512) salthash = crypt.crypt(password, salt) see [here](https://docs.python.org/3/library/crypt.html#crypt.mksalt) for more info. – brent saner Jan 29 '18 at 17:32
1

This is a solution where shell is false.

#!/bin/env/python

import subprocess
import traceback
import sys


def user_add(username, user_dir=None):
    if user_dir:
        cmd = ["sudo", "useradd", "-d", user_dir, "-m", username]
    else:
        cmd = ["sudo", "useradd", username]

    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = p.communicate()
    output = output.strip().decode("utf-8")
    error = error.decode("utf-8")
    if p.returncode != 0:
        print(f"E: {error}")
        raise
    return output


try:
    username = "user"
    output = user_add(username)
    print(F"Success. {username} is added")
except:
    traceback.print_exc()
    sys.exit(1)


alper
  • 2,919
  • 9
  • 53
  • 102