How to Manage Users and Groups in Unix

Managing Users and Groups in Unix

Unix provides powerful tools for managing users and groups, allowing you to control access to system resources and organize your system more effectively. Understanding how to add, modify, and remove users and groups is essential for system administrators.

In this blog, we’ll explore how to manage users and groups using Unix commands, with practical examples.


1. Understanding Users and Groups

A user is an individual account on a Unix system, each identified by a unique User ID (UID). A group is a collection of users who share common permissions. Each user is a member of at least one group (the primary group).

There are also secondary groups, which provide additional access rights to users.


2. Adding and Modifying Users

Add a New User with useradd

The useradd command is used to create new user accounts.

Basic Syntax
sudo useradd [options] username
Example:

Create a new user named johndoe:

$ sudo useradd johndoe

This command creates a new user but does not set a password. To set a password for the user:

$ sudo passwd johndoe

Add a User with Specific Options

You can add a user with specific settings such as a home directory or shell.

Example: Create a user alice with a home directory /home/alice and /bin/bash as the shell:

$ sudo useradd -m -d /home/alice -s /bin/bash alice
  • -m: Creates a home directory.
  • -d: Specifies the home directory.
  • -s: Specifies the shell.

Modify User Information with usermod

You can modify user details using usermod.

Example:

Change the login shell for alice:

$ sudo usermod -s /bin/zsh alice

To change the user’s home directory:

$ sudo usermod -d /home/newalice -m alice

3. Removing Users

Remove a User with userdel

The userdel command deletes a user account from the system.

Basic Syntax
sudo userdel [options] username
Example:

Delete the user johndoe:

$ sudo userdel johndoe

To remove the user’s home directory along with the user:

$ sudo userdel -r johndoe

4. Adding and Modifying Groups

Add a New Group with groupadd

The groupadd command is used to create new groups.

Basic Syntax
sudo groupadd groupname
Example:

Create a new group developers:

$ sudo groupadd developers

Add a User to a Group with usermod

You can add a user to an existing group using the usermod command.

Example:

Add the user alice to the developers group:

$ sudo usermod -aG developers alice
  • -aG: Appends the user to the specified group without removing them from other groups.

Modify Group Information with groupmod

You can change group properties with the groupmod command.

Example:

Change the name of the developers group to devs:

$ sudo groupmod -n devs developers

5. Removing Groups with groupdel

The groupdel command is used to remove groups from the system.

Example:

Delete the devs group:

$ sudo groupdel devs

6. Viewing Users and Groups

List All Users

To list all users on the system, view the /etc/passwd file:

$ cat /etc/passwd

This file contains user details, including usernames, user IDs, home directories, and login shells.

List All Groups

To list all groups, view the /etc/group file:

$ cat /etc/group

This file contains group information, including group names, group IDs, and members.

View Group Membership with groups

To view which groups a user belongs to:

$ groups alice

7. Managing User Permissions

Unix assigns read, write, and execute permissions for files to the user, group, and others. The chmod, chown, and chgrp commands allow you to modify file permissions and ownership.

Change File Permissions with chmod

The chmod command is used to change file permissions.

Example:

$ chmod 755 myfile.txt

This gives the owner full permissions (read, write, execute), and gives the group and others read and execute permissions.

Change File Owner with chown

The chown command changes the ownership of a file.

Example:

$ sudo chown alice:developers myfile.txt

This changes the owner of myfile.txt to alice and the group to developers.

Change Group Ownership with chgrp

The chgrp command changes the group ownership of a file.

Example:

$ sudo chgrp developers myfile.txt

Practice Time!

  1. Create a new user named bob and set a password for him:
   $ sudo useradd bob
   $ sudo passwd bob
  1. Add bob to the group admins:
   $ sudo usermod -aG admins bob
  1. Change the shell of bob to /bin/zsh:
   $ sudo usermod -s /bin/zsh bob
  1. Remove the user bob along with their home directory:
   $ sudo userdel -r bob

Summary

  • Use useradd, usermod, and userdel to manage users.
  • Use groupadd, usermod, groupdel, and groupmod to manage groups.
  • View users and groups with cat /etc/passwd and cat /etc/group.
  • Change file ownership and permissions with chmod, chown, and chgrp.

Leave a Reply

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