Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

  • You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments -

#!/bin/bash

?

# Check if the script was provided with the correct number of arguments

if [ "$#" -ne 3 ]; then

? echo "Usage: $0 <directory_name> <start_number> <end_number>"

? exit 1

fi

?

directory_name="$1"

start_number="$2"

end_number="$3"

?

# Ensure the start and end numbers are valid integers

if ! [[ "$start_number" =~ ^[0-9]+$ ]] || ! [[ "$end_number" =~ ^[0-9]+$ ]]; then

? echo "Invalid start or end number. Please provide valid integers."

? exit 1

fi

?

# Create directories using a for loop

for ((i = start_number; i <= end_number; i++)); do

? dir="${directory_name}${i}"

? mkdir -p "$dir"

? echo "Created directory: $dir"

done

Terminal:

Make sure the script is executable using the chmod command:

chmod +x createDirectories.sh

You can then execute the script with the desired arguments, as in your example:

./createDirectories.sh day 1 90

This will create 90 directories named day1, day2, day3, and so on up to day90.

  • Create a Script to backup all your work done till now.

Creating a backup of your work is essential for data safety. You can create a simple Bash script to backup your work to a specific backup directory. Here's a basic script for creating a backup:

#!/bin/bash

?

# Define the source directory (the directory you want to back up)

source_dir="/path/to/devops"

?

# Define the backup directory (where you want to store the backup)

backup_dir="/path/to/backup_devops"

?

# Create a timestamp for the backup

timestamp=$(date “+%Y-%m-%d-%H-%M-%S”)

?

# Create a backup directory with the current timestamp

backup_folder="${backup_dir}/backup_${timestamp}.tgz"

?

# Check if the source directory exists

if [ ! -d "$source_dir" ]; then

? echo "Source directory does not exist."

? exit 1

fi

?

# Create the backup directory

mkdir -p "$backup_folder"

?

# Copy the contents of the source directory to the backup directory

Tar czf ?$source_dir -C $backup_folder .

?

# Check if the Backup was successful

if [ $? -eq 0 ]; then

? echo "Backup completed successfully. Files are stored in: $backup_folder"

else

? echo "Backup failed."

fi

?

  • Read About Cron and Crontab, to automate the backup Script

Cron and Crontab are essential tools for automating tasks, including running scripts like the backup script you've created at scheduled times. Here's an overview of both:

Cron: Cron is a time-based job scheduler on Unix-like operating systems. It allows you to schedule repetitive tasks to run at specific times or intervals. Cron is responsible for managing these scheduled tasks.

Crontab: Crontab is a simple text file that specifies the commands or scripts to be executed by Cron and their scheduling details. Each user on a system can have their own crontab file, where they define the tasks they want to run automatically.

To automate your backup script using Cron and Crontab, you need to add an entry to your crontab file. Here's how you can do it:

Open your crontab file using the crontab -e command. This will open the crontab file in the default text editor.

Add an entry to schedule the backup script. The format for a crontab entry is as follows:

For example, if you want to run your backup script every day at 2:00 AM, you would add the following entry to your crontab file:

0 2 * /path/to/backup_script.sh

In this example, "0" represents the minute (2:00 AM), "2" represents the hour, and the asterisks indicate that the script should run every day.

Save and exit the text editor. The new crontab entry will be automatically scheduled to run your backup script at the specified time.

  • Read about User Management?in Linux

A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users and hence the ids for local user begins from 1000 onwards.

User management in Linux involves creating, modifying, and managing user accounts on a Linux system. It also includes configuring user privileges and access control. Here are the key aspects of user management in Linux:

1.???? User Accounts:

·??????? User accounts are individual user identities on a Linux system.

·??????? Usernames are case-sensitive and can consist of letters, numbers, and some special characters.

·??????? User accounts are associated with unique user IDs (UIDs) and group IDs (GIDs).

·??????? User account information, including home directory and default shell, is stored in the /etc/passwd file.

2.???? User Groups:

·??????? User groups are collections of user accounts that share common privileges and access to files and directories.

·??????? Group information is stored in the /etc/group file.

·??????? Users can belong to multiple groups, but one group is their primary group.

3.???? User Management Commands:

·??????? Key user management commands include useradd, userdel, and usermod to add, delete, and modify user accounts, respectively.

·??????? The passwd command is used to change a user's password.

·??????? The groups command shows the groups a user belongs to.

4.???? Group Management Commands:

·??????? Group management commands include groupadd, groupdel, and groupmod to add, delete, and modify groups, respectively.

5.???? Root User:

·??????? The root user (superuser) has administrative privileges and can perform any system operation.

·??????? Root access is required to make changes to user accounts and groups.

6.???? User Privileges:

·??????? Users can be assigned various privileges through user groups, such as sudo access for administrative tasks.

·??????? The /etc/sudoers file controls which users or groups are allowed to execute commands as superusers.

7.???? User Home Directories:

·??????? Each user has a home directory, typically located in the /home directory.

·??????? The home directory stores the user's personal files and configuration settings.

8.???? Login Shells:

·??????? Each user is associated with a login shell, which determines the command-line interface and environment when they log in.

·??????? Common login shells include bash, zsh, and sh.

9.???? File Permissions:

·??????? Linux uses file permissions to control access to files and directories.

·??????? User accounts and groups are associated with files and directories, and permissions are set using commands like chmod and chown.

10.? Account Locking and Expiration:

·??????? You can lock a user account to prevent login by setting the account's password to an invalid value.

·??????? Account expiration can be set using chage to limit the validity of a user account.

11.? Password Policies:

·??????? Password policies, such as password complexity and aging, can be defined using the Pluggable Authentication Module (PAM) configuration in /etc/security/.

12.? Account Monitoring:

·??????? Account activities can be monitored and logged using utilities like last, who, and auditd.

?

  • Create 2 users and just display their Usernames

To create two users and display their usernames, you can use the useradd command to create the users and the awk command to extract and display their usernames from the /etc/passwd file. Here's how you can do it:

Create the first user:

sudo useradd Kamran

Create the second user

sudo useradd Ali

Display the usernames of the created users:

awk -F: '{print $1}' /etc/passwd | grep -E 'Kamran|Ali'

要查看或添加评论,请登录

Muhamad Kamran的更多文章

社区洞察

其他会员也浏览了