How to Create a User in Linux Using Python

How to create a user in linux using python

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

How to add user to Linux via Python script?

For my application, what I needed was a way to run sudo commands over a python script.

This answer is basically what I was looking for.

Thanks again for taking the time to help.

Use of adduser --home linux by python

You can use useradd instead of adduser you can specify the password and more via in CLI (I guess you are or should use subprocess)

Please note in future as per SO guideline first search for similar questions before asking a new question. For example the answer to your question can be found here

How can I automate the user creation in Linux servers using Python

You are treating ssh as if it used execve(2) semantics, where you pass commands and arguments as individual parameters (the way sudo, xargs, and find -exec works).

ssh instead uses system(3) semantics, so it expects a single literal shell command (like su, eval, bash -c and Python's os.system).

One general purpose way of doing this for arbitrary commands is to first assign the shell command you want to run to another string:

remote_command="sudo useradd -p " + p + " -d "+ "/home/" + user+ " -m "+ " -c \""+ name+"\" " + user

And then use shlex.quote to escape it for ssh:

import shlex
os.system("ssh myuser@lab " + shlex.quote(remote_command))

Alternatively, skip the system() to avoid having to add another layer of escaping:

import subprocess
subprocess.run(["ssh", "myuser@lab", remote_command], shell=False)

How to create an user in linux using python libuser

See the creation of the jimbo user in https://pagure.io/libuser/blob/master/f/python/test-script

Having Trouble with creating user accounts in Linux with Python script

Fix the following line to use username and password:

os.system("sudo useradd -p {} -m {}".format(password, username))

Create a user-group in linux using python

I don't know of a python module to do it, but the /etc/group and /etc/gshadow format is pretty standard, so if you wanted you could just open the files, parse their current contents and then add the new group if necessary.

Before you go doing this, consider:

  • What happens if you try to add a group that already exists on the system
  • What happens when multiple instances of your program try to add a group at the same time
  • What happens to your code when an incompatible change is made to the group format a couple releases down the line
  • NIS, LDAP, Kerberos, ...

If you're not willing to deal with these kinds of problems, just use the subprocess module and run groupadd. It will be way less likely to break your customers machines.

Another thing you could do that would be less fragile than writing your own would be to wrap the code in groupadd.c (in the shadow package) in Python and do it that way. I don't see this buying you much versus just exec'ing it, though, and it would add more complexity and fragility to your build.

authenticate a user from local linux host using python script

You can use pexpect. Unless you are running your script as root you will need to supply a password to sudo (as you will have to for any answer that uses sudo). To make the script more portable also supply a sudo user name but you could hard code this if using root. This code was written for Ubuntu 21.10 and may need the strings updating for other distributions. I think the code is self-explanatory, you spawn a process, interact with it and expect certain responses during exection.

import pexpect

sudo_user = 'whatever your sudo user name is'
sudo_password = "whatever your sudo user password is"
user_name = "whatever local user name is"
password = "whatever local user password is"

child = pexpect.spawn(f'/usr/bin/sudo /usr/bin/login {user_name}', encoding='utf-8')
child.expect_exact(f'[sudo] password for {sudo_user}: ')
child.sendline(sudo_password)
return_code = child.expect(['Sorry, try again', 'Password: '])
if return_code == 0:
print('Can\'t sudo')
print(child.after) # debug
child.kill(0)
else:
child.sendline(password)
return_code = child.expect(['Login incorrect', '[#\\$] '])
if return_code == 0:
print('Can\'t login')
print(child.after) # debug
child.kill(0)
elif return_code == 1:
print('Login OK.')
print('Shell command prompt', child.after)

For more detail see the docs https://pexpect.readthedocs.io/en/stable/overview.html

Login via ssh using publickey and create a new user using Python?

I've tested your code by running different commands. So, please check if you are able to run simple commands like "sudo uptime". If it works fine, then follow the below instructions.

If you run "sudo adduser myuser", you will be prompted to enter more details as in below output:

sudo adduser myuser
Adding user `myuser' ...
Adding new group `myuser' (1002) ...
Adding new user `myuser' (1002) with group `myuser' ...
The home directory `/home/myuser' already exists. Not copying from `/etc/skel'.
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for myuser
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y

So, use the command "sudo useradd myuser" instead of "sudo adduser myuser" because "useradd" command will not prompt for any inputs.



Related Topics



Leave a reply



Submit