Familiarization of basic Linux Commands-useradd, userdel , groupadd, groupdel, passwd
useradd- adding new users
The useradd
command in Linux is used to create new user accounts. It is a low-level utility that provides options for configuring user properties such as home directories, shell access, user groups, and more.
Syntax
useradd [OPTIONS] USERNAME
USERNAME
: The name of the new user to be created.OPTIONS
: Parameters to customize the user account.
Features of useradd
Creates a New User
It adds the user information to the system's user account files, such as/etc/passwd
,/etc/shadow
, and/etc/group
.Configures User Environment
By default, it sets up a home directory, user ID (UID), group ID (GID), and a default shell.
Options
Option | Description |
---|---|
-c "COMMENT" | Sets a description or comment for the user, typically the full name. |
-d HOME_DIR | Specifies the home directory for the user. If not provided, it defaults to /home/USERNAME . |
-e EXPIRE_DATE | Sets an expiration date for the user account (in YYYY-MM-DD format). |
-f INACTIVE_DAYS | Defines the number of days after the password expires before the account is disabled. |
-g GROUP | Assigns a primary group for the user. |
-G GROUP1,GROUP2 | Adds the user to additional (supplementary) groups. |
-m | Creates the user's home directory if it doesn’t already exist. |
-M | Prevents the creation of the home directory. |
-p PASSWORD | Sets an encrypted password for the user. (Not recommended for security reasons—use passwd instead.) |
-s SHELL | Specifies the login shell for the user. Default is /bin/bash (depending on the system configuration). |
-u UID | Sets a specific user ID (UID) for the account. |
-r | Creates a system account instead of a regular user account. |
This creates:
- A user
alice
. - A home directory
/home/alice
. - A unique UID.
- A default shell (e.g.,
/bin/bash
).
/etc/passwd
.alice
to the sudo
and developers
groups.alice
is set to /bin/zsh
.passwd -User Password Setup
The useradd
command itself does not automatically set a password for the user. To set a password, use the passwd
command:
sudo passwd alice
alice
.Files Modified by useradd
/etc/passwd
Stores user account details such as username, UID, GID, home directory, and shell./etc/shadow
Contains encrypted passwords and password-related settings./etc/group
Defines group memberships.
Default Configuration for New Users
The default settings for new users are stored in /etc/default/useradd
and /etc/skel/
:
/etc/default/useradd
Configures default values such as shell, home directory, and account expiration./etc/skel/
Contains default files copied to the user's home directory (e.g.,.bashrc
,.profile
).
Viewing User Details
You can verify the details of a created user:
grep alice /etc/passwd
Best Practices
Set Secure Passwords
Usepasswd
to set user passwords securely.Use
-m
Option
Ensure home directories are created by default using-m
.Avoid Plaintext Passwords
Avoid using-p
for passwords; instead, set them interactively.Assign Groups Wisely
Carefully assign users to administrative groups likesudo
.
Summary
The useradd
command is a powerful utility for adding users in Linux. With various options, it provides flexibility for customizing user accounts. While simple to use, understanding its options and default behavior ensures secure and efficient user management.
userdel - user deletion
The userdel command in Linux is used to delete a user account and its associated files from the system. It is an essential command for user management, typically used by system administrators.userdel [OPTIONS] USERNAME
USERNAME
: The name of the user account to be deleted.OPTIONS
: Parameters that control how the deletion is performed.userdel
Removes User Account
Deletes the user entry from system files like/etc/passwd
,/etc/shadow
, and/etc/group
.Manages User Data
Can optionally delete the user’s home directory and mail spool.Handles Logged-In Users
Will not delete accounts of users who are currently logged in without force.
Options
Option | Description |
---|---|
-f | Force deletion of the user account, even if the user is currently logged in. |
-r | Removes the user's home directory and mail spool along with the account. |
How to Use userdel
1. Delete a User Account
sudo userdel alicealice
user but leaves her home directory and files intact.This removes the bob
user and deletes:
- The user’s home directory (
/home/bob
). - The user’s mail spool (if it exists).
charlie
account, even if the user is logged in.Best Practices
Check User’s Activity Ensure the user is not running any critical processes before deletion:
-f
)
Use force deletion sparingly, as it can disrupt active sessions The userdel command is a powerful tool for managing and removing user accounts in Linux. Its options allow for flexible deletion, including managing user data. By following best practices and verifying actions before execution, system administrators can ensure a smooth and error-free user removal process.
groupadd - adding new groups
The groupadd
command in Linux is used to create new groups. Groups are a way to organize and manage user permissions by associating users with specific roles or resource access.
groupadd [OPTIONS] GROUPNAME
GROUPNAME
: The name of the new group to be created.OPTIONS
: Parameters to customize the group.Features of groupadd
Creates New Groups
It adds a group entry to the system files such as/etc/group
and/etc/gshadow
.Configures Group Properties
You can set custom Group IDs (GID) or configure it as a system group.
Options
Option | Description |
---|---|
-f | Does not return an error if the group already exists. |
-g GID | Specifies a Group ID (GID) for the new group. |
-K KEY=VALUE | Overrides /etc/login.defs defaults for this group creation. |
-o | Allows the creation of a group with a duplicate GID (use cautiously). |
-r | Creates a system group (with a GID typically below 1000). |
This creates a group named developers
with:
- A unique GID automatically assigned.
- Entries in
/etc/group
and/etc/gshadow
.
admins
with GID 2000
.3. Create a System Group
System groups are typically used for system-level services and processes.
sudo groupadd -r sysadmins
Comments
Post a Comment