How to Add User Accounts in Linux

...

What should every user who utilizes your Linux machine have?
Every user who utilizes your Linux machine should have a separate user account.

What does having a separate user account allow each user to do?
Having a separate user account allows a user to have separate files in a safe space and the ability to customize their home directory, path, environment variables, and more.

How do you list the available user accounts on Linux?
To list the available user accounts on Linux, run:

cut -d: -f1 /etc/passwd

Which program do you use to add a new user account to the system?
The program you use to add a new user account to the system is useradd.

What are some of the flags for the useradd command?
Some of the flags for the useradd command include:

  • -c - Adds a description or comment.
  • -d - Sets the home directory path.
  • -g - Sets the primary group.
  • -G - Adds the user to multiple groups.
  • -o - Uses the UID of an existing user.
  • -p - Adds an encrypted password.
  • -s - Sets the default shell.

How do you add or change the password of an existing account on Linux?
To add or change the password of an existing account on Linux, run:

passwd <username>
Example of adding a new user for someone named James Adem, who would be primarily part of the tech group as well as the apple and linux groups, and whose default shell is zsh
useradd -g tech -G apple,linux -s /bin/zsh -c "James Adem" adem

Modify Default User Settings

What three configuration files does useradd read from? (1)
The three configuration files that useradd reads from are:

  1. /etc/login.defs.
  2. /etc/useradd.
  3. /etc/default/useradd.

How do you list the default values for useradd?
To list the default values for useradd, run:

useradd -D

How do you change the default values for useradd with useradd?
To change the default values for useradd with useradd, run:

useradd -D <flag> <value>

What flags are available to use with useradd -D and what values do they change?
The flags available to use with useradd -D and the values they change include:

  • -b or --base-dir - The path prefix for the new home directory.
  • -e or --expiredate - The date when the user account will be disabled.
  • -f or --inactive - The maximum number of days after the password exceeded its maximum age where the user is expected to change their password.
  • -g or --gid - The default primary group for newly created users, accepting group names or a numerical group ID.
  • -s or --shell - The default login shell for new users.'
Example of changing the default shell to /bin/sh and the home directory to /home/new with useradd
useradd -D -b /home/new -s /bin/sh

Modify User Groups on Linux

Which program is used to modify an existing user on the system?
The program you use to modify an existing user on the system is usermod.

Example of changing the default shell of adem to /bin/bash
usermod -s /bin/bash adem

How do you add a user to a group?
To add a user to a group, run:

usermod -aG <group> <username>

What happens if you use just the -G flag with usermod?
If you use just the -G flag with usermod, the user will be removed from any other supplementary group.

Example of adding a user named adem to the sales group
usermod -aG sales adem

How to Delete User Accounts on Linux

Which program do you use to delete a user on the system?
The program you use to delete a user on the system is userdel.

How do you remove a user from the system using userdel?
To remove a user from the system using userdel, run:

userdel <username>

How do you remove a user and their home directory using userdel?
To remove a user and their home directory using userdel, run:

userdel -r <username>

What should you do as a precaution when removing users?
As a precaution, before removing users you should find all the files owned by the user, assigned to a deleted user's UID, or without an owner and reassign them to any other existing user account.

How do you find all of the files owned by a user by their username?
To find all of the files owned by a user by their username, run:

find / -user <username>-ls

How do you find all of the files owned by a user by their UID?
To find all of the files owned by a user by their username, run:

find / -uid <uid>-ls

How do you find all of the files not owned by a user?
To find all of the files not owned by a user, run:

find / -nouser -ls