create users

password, groups and stuff

September 20, 2022



Add a new user and set the user password.

If you want to create a user and set a password for the user right away, it is best to:

  • first create the user using useradd:

    • Command details:

      • useradd: Is a low level utility for adding users.
      • -m, --no-create-home: Do no create the user's home directory.
      • -s, --shell: The name of the user's login shell.
  • then set a password using passwd:

    • Command details:

      • passwd: Changes passwords for user accounts.
# Create user 'test_user' without home directory.
$ sudo useradd --no-create-home --shell /bin/bash test_user
# Add a password.
$ sudo passwd test_user

useradd has the option -p, --password. However - according to man useradd - there is a good reason not to use this option:

# Taken from 'man useradd'.
...
The encrypted password, as returned by crypt(3). The default is to disable the password.

Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the
processes.
...

Note:

  • If you log in as test_user, e.g. like this: su [-/-l] test_user, the home directory will be created by the OS: /home/test_user/.

Add a user to a group

Assuming user and group already exist.

# Add the user to the group.
$ sudo usermod --append --groups docker main_user
  • Command details:

    • usermod: Modifies the system account files.
    • -a, --append: Add the user to the supplementary group(s). Use only with the -G option.
    • -G, --groups: A list of supplementary groups which the user is also a member of.

To make the change take effect, you need to log out and in once.

Another option is to open a new shell session, for example in bash:

# Open new 'bash' session.
$ bash

To add the current user to the group for the duration of the current session:

$ newgrp docker
  • Command details:

    • newgrp: Is used to change the current group ID during a login session.