BLOG POSTS
    MangoHost Blog / How to Add or Modify User in Linux – Managing user accounts with useradd and usermod
How to Add or Modify User in Linux – Managing user accounts with useradd and usermod

How to Add or Modify User in Linux – Managing user accounts with useradd and usermod

The useradd and usermod commands are part of the Linux operating system and are used for managing user accounts. These commands allow you to add new users, modify existing user data, and perform various administrative tasks related to user accounts.

What is useradd and usermod?

useradd is a command-line utility that is used to create a new user account on a Linux system. It is typically used by system administrators to add new users to the system. The useradd command creates a new entry in the system’s user database, sets up the user’s home directory, and assigns the user a unique user ID (UID) and group ID (GID).

usermod is another command-line utility that is used to modify existing user accounts. It allows you to change various attributes of a user account, such as the user’s home directory, login shell, password, and group membership.

Where is useradd and usermod used?

The useradd and usermod commands are commonly used on Linux systems by system administrators and users with administrative privileges. These commands are typically used in scenarios where there is a need to create new user accounts, modify existing user accounts, or perform administrative tasks related to user accounts.

Programming Languages

The useradd and usermod commands are written in C programming language.

Installation

The useradd and usermod commands are part of the core Linux operating system and are usually pre-installed on most Linux distributions. However, if for some reason they are not installed, you can install them using the package manager specific to your Linux distribution.

Installing on Ubuntu/Debian

To install the useradd and usermod commands on Ubuntu or Debian, you can use the following command:

sudo apt-get install passwd

Installing on CentOS/RHEL

To install the useradd and usermod commands on CentOS or RHEL, you can use the following command:

sudo yum install passwd

Examples of useradd and usermod commands

Creating a new user

To create a new user account using the useradd command, you can use the following syntax:

sudo useradd username

This command will create a new user account with the specified username. By default, the user’s home directory will be created in the /home directory and the user will be assigned a unique UID and GID.

Modifying an existing user

To modify an existing user account using the usermod command, you can use the following syntax:

sudo usermod options username

This command allows you to modify various attributes of the user account. For example, you can change the user’s home directory, login shell, password, and group membership.

Similar commands/packages

There are several other commands and packages available on Linux that serve similar purposes as useradd and usermod. Some of these include:

  • adduser: This is another command-line utility for creating new user accounts. It is a higher-level tool that provides a more user-friendly interface compared to useradd.
  • userdel: This command is used to delete user accounts from the system.
  • chsh: This command is used to change the login shell for a user account.
  • passwd: This command is used to change the password for a user account.

Scripts as examples

Script 1: Create User

This script demonstrates how to use the useradd command to create a new user account.

#!/bin/bash

# Prompt the user for the username
read -p "Enter the username: " username

# Create the user account
sudo useradd $username

# Set the initial password for the user
sudo passwd $username

Script 2: Modify User

This script demonstrates how to use the usermod command to modify an existing user account.

#!/bin/bash

# Prompt the user for the username
read -p "Enter the username: " username

# Prompt the user for the new home directory
read -p "Enter the new home directory: " home_directory

# Modify the user account
sudo usermod -d $home_directory $username

Script 3: Delete User

This script demonstrates how to use the userdel command to delete a user account.

#!/bin/bash

# Prompt the user for the username
read -p "Enter the username: " username

# Delete the user account
sudo userdel $username

List of possible functions or constants

Command Description
useradd Create a new user account
usermod Modify an existing user account
userdel Delete a user account
chsh Change the login shell for a user account
passwd Change the password for a user account

Conclusion

The useradd and usermod commands are essential tools for managing user accounts on Linux systems. They are commonly used by system administrators and users with administrative privileges to create new user accounts, modify existing user accounts, and perform various administrative tasks related to user accounts. These commands provide flexibility and control over user management, allowing administrators to customize user accounts to meet their specific requirements. Overall, the useradd and usermod commands play a crucial role in maintaining the security and functionality of Linux systems.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

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