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 CHANGELOG
========= =========
Unreleased
----------
* Fix shell when creating new linux user account.
0.3.3 (2023-09-25) 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, def make_normal_user(c, username, full_name=None,
disabled_login=True, shell='/bin/bash',
password=None): password=None,
disabled_login=False):
""" """
Make a new "normal" user account. Make a new "normal" user account.
:param disabled_login: If true (the default), add the :param disabled_login: If true, will leave the account in a
``--disabled-login`` flag to the ``adduser`` command. The non-usable state, i.e. with invalid shell.
reason this is the default, is to avoid being prompted for a
password to give the new account.
""" """
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 return
if password: # nb. specify --disabled-login to avoid being prompted for password
disabled_login = True c.sudo("adduser --gecos '{}' --disabled-login {}".format(full_name or username,
disabled_login = '--disabled-login' if disabled_login else ''
c.sudo("adduser --gecos '{}' {} {}".format(full_name or username,
disabled_login,
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: if password:
c.sudo(f"bash -c 'echo {username}:{password} | chpasswd'", c.sudo(f"bash -c 'echo {username}:{password} | chpasswd'",
echo=False) echo=False)