Creating and Managing Users and Groups from the Command Line

Creating and Managing Users and Groups from the Command Line

The ability to create, modify, and remove user accounts and groups is fundamental to Linux system administration. This guide covers the essentials of user and group management via the command line, exploring configuration files, powerful commands, best practices, and advanced techniques.

Table of Contents

  • 1. Core Configuration Files
  • 2. Creating Users
  • 3. Modifying and Locking Accounts
  • 4. Deleting Users
  • 5. Managing Groups
  • 6. Advanced Topics
  • 7. Best Practices and Security Tips

1. Core Configuration Files

Linux relies on several flat files to store account information. Understanding these files is crucial before running any command.

/etc/passwd

Defines user account data. Each line:

username:x:UID:GID:comment:home_directory:shell
  • username: login name
  • x: placeholder for encrypted password (in /etc/shadow)
  • UID: User ID
  • GID: Primary Group ID
  • comment: GECOS field, typically full name
  • home_directory: path to user’s home
  • shell: login shell

/etc/shadow

Holds encrypted passwords and aging info. Only root has read access.

/etc/group

Lists groups and their memberships:

groupname:x:GID:user1,user2,…

2. Creating Users

Two main utilities: useradd (low-level) and adduser (friendly script on Debian/Ubuntu).

useradd Syntax

useradd [options] username

Common options:

Option Description
-m Create home directory
-d /path Specify home directory
-s /bin/bash Set login shell
-g group Primary group
-G grp1,grp2 Supplementary groups
-c comment GECOS field
-u UID Specify UID

Example: Create a New User

sudo useradd -m -c Alice Johnson -s /bin/bash alice

Then set a password:

sudo passwd alice

adduser (Debian/Ubuntu)

A wrapper that prompts you interactively:

sudo adduser bob

You will be asked for the full name, password, and default shell.

3. Modifying and Locking Accounts

usermod

usermod [options] username

Key options:

  • -l newname: Change login name
  • -d /new/home -m: Move home directory
  • -s /bin/zsh: Change login shell
  • -g group: New primary group
  • -G grp1,grp2: Supplementary groups

Locking and Unlocking

  • Lock an account:
    sudo passwd -l username
  • Unlock:
    sudo passwd -u username

4. Deleting Users

Use userdel. Options:

  • -r: Remove home directory and mail spool
  • -f: Force deletion even if user is logged in

Example:

sudo userdel -r alice

5. Managing Groups

Creating Groups

groupadd syntax:

sudo groupadd [options] groupname

Optionally specify GID:

sudo groupadd -g 1050 developers

Modifying Groups

sudo groupmod -n newname oldname

Or change GID:

sudo groupmod -g 2000 staff

Adding Users to Groups

  • Add a single group:
    sudo usermod -aG group user
  • View group membership:
    groups username

Deleting Groups

sudo groupdel groupname

6. Advanced Topics

Default Settings: /etc/login.defs

Controls defaults for UID_MIN, UID_MAX, password aging, and more. Tune for organizational policies.

Skeleton Directory: /etc/skel

Files and directories here are copied into each new user’s home when -m is used. Customize bashrc, profile, etc.

Implementing LDAP or NIS

Large environments often delegate authentication to directory services. Tools like NIS or LDAP can centralize users and groups.

7. Best Practices and Security Tips

  • Use UID and GID ranges to separate system and human accounts. Adjust UID_MIN accordingly.
  • Enforce strong password policies via /etc/login.defs and PAM modules (pam_pwquality.so).
  • Regularly audit /etc/passwd and /etc/shadow for orphaned or expired accounts.
  • Lock default accounts (e.g., guest, ftp) if not in use: passwd -l.
  • Provide least privilege: assign users only to necessary groups.
  • Use sudo instead of sharing root credentials. Configure /etc/sudoers with visudo.

Conclusion

Mastering user and group management from the command line is essential for secure and efficient system administration. From basic useradd invocations to advanced directory integration, these tools grant full control over permissions, resources, and compliance with organizational policies.

Download TXT




Leave a Reply

Your email address will not be published. Required fields are marked *