Fix shell when creating new linux user account

This commit is contained in:
Lance Edgar 2024-05-07 14:13:11 -05:00
parent 6a15b5ae5e
commit 305e4c40c7
2 changed files with 23 additions and 13 deletions

View file

@ -2,6 +2,12 @@
CHANGELOG
=========
Unreleased
----------
* Fix shell when creating new linux user account.
0.3.3 (2023-09-25)
------------------

View file

@ -129,25 +129,29 @@ def mkdir(c, paths, owner=None, mode=None,
def make_normal_user(c, username, full_name=None,
disabled_login=True,
password=None):
shell='/bin/bash',
password=None,
disabled_login=False):
"""
Make a new "normal" user account.
:param disabled_login: If true (the default), add the
``--disabled-login`` flag to the ``adduser`` command. The
reason this is the default, is to avoid being prompted for a
password to give the new account.
:param disabled_login: If true, will leave the account in a
non-usable state, i.e. with invalid shell.
"""
if not c.run('getent passwd {}'.format(username), warn=True).failed:
# do not bother if user exists
missing = c.run(f'getent passwd {username}', warn=True).failed
if not missing:
return
if password:
disabled_login = True
disabled_login = '--disabled-login' if disabled_login else ''
c.sudo("adduser --gecos '{}' {} {}".format(full_name or username,
disabled_login,
# nb. specify --disabled-login to avoid being prompted for password
c.sudo("adduser --gecos '{}' --disabled-login {}".format(full_name or username,
username))
# then fix the shell unless we shouldn't
if not disabled_login:
c.sudo(f'usermod -s {shell} {username}')
# and maybe set password
if password:
c.sudo(f"bash -c 'echo {username}:{password} | chpasswd'",
echo=False)