ITGeeksHub

User Accounts and Group Management in Linux

User Accounts and Group Management in Linux

User Accounts and Group Management in Linux

1. Creating User Accounts

Use the useradd command to create a new user. The ‘-m’ flag creates a home directory for the user.

sudo useradd -m username

2. Setting Password for User Accounts

To set a password for the user, use the passwd command.

sudo passwd username

3. Password Expiration

Password expiration can be configured using the chage command. You can set a maximum age for passwords.

sudo chage -M 90 username
        sudo chage -m 10 username

This sets the maximum password age to 90 days and the minimum to 10 days.

4. Enabling and Disabling User Accounts

To disable a user account, you can use the usermod command with the ‘-L’ (lock) option:

sudo usermod -L username

To enable the account, unlock it using the ‘-U’ option:

sudo usermod -U username

5. Creating User from a CSV File

Below is a sample Bash script that creates users from a CSV file. The CSV file format should be username,password.

#!/bin/bash
        # Script to create users from a CSV file

        # Check if the CSV file is provided
        if [ -z "$1" ]; then
            echo "Usage: $0 users.csv"
            exit 1
        fi

        # Read the CSV file line by line
        while IFS=',' read -r username password; do
            # Create the user
            sudo useradd -m "$username"
            # Set the password for the user
            echo "$username:$password" | sudo chpasswd
            echo "User $username created"
        done < "$1"
        

6. Example: Creating a CSV File

user1,password1
user2,password2
user3,password3

Summary

This guide provides an overview of user account management in Linux, covering user creation, password management, and account status management. Modify and use the provided script to efficiently create users from CSV files.

Leave a Reply