Advanced Linux Shell Scripting for DevOps Engineers with User management

Advanced Linux Shell Scripting for DevOps Engineers with User management

Day 5 of 90daysofdevops

Task 1: Creating Multiple Directory using Shell Scripting

  • Write a bash script "create_dir.sh" that when the script is executed with three given arguments (one is the directory name and second is the start number of directories and third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

    Example 1: When the script is executed as

    "./create_dir.sh day 1 20"

    then it creates 90 directories as day1 day2 .... day20

      #!/bin/bash
      dir_name=$1
      day_start=$2
      day_end=$3
    
      for (( i=day_start; i<=day_end; i++))
      do mkdir "$dir_name$i"
    
      done
      echo "File creation successful."
    

Task 2: Taking Backup using Shell Scripting

  • Create a Script to back up all your work done till now.
#!/bin/bash

src_dir=/home/abhisekmoharana773/90daysdevops/days
tgt_dir=/home/abhisekmoharana773/90daysdevops/backup

curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S") backup_file=$tgt_dir/$curr_timestamp.tgz

echo "Taking backup on $curr_timestamp"

tar -cvzf $backup_file $src_dir

echo "Backup complete"

Task 3: Cron, Crontab, Automate Backup Script

Define Cron and Crontab, to automate the backup Script.

  • Cron is a time-based job scheduler that allows users to schedule commands or scripts to run automatically at specific intervals or times. These jobs can be scheduled to run once or repeatedly at regular intervals, such as daily, weekly, monthly, or at specific times.

  • Crontab, on the other hand, is a file used to configure and manage the scheduling of cron jobs. It contains a list of commands and scripts that are scheduled to run automatically at specified times or intervals.

  • ** Each user on a Linux system can have their own crontab file, and they can configure their own set of scheduled jobs that only they have access to modify.

    Users can edit their crontab file using the crontab command, which allows them to add, modify, or remove scheduled jobs.

# To set cron jobs
crontab -e

#For showing cron jobs of current user
crontab -l

# To remove cron jobs
crontab -r
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).

# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

#  Crontab backup tests
30 5 * * * /home/abhisekmoharana773/90daysdevops/backup/backup.sh

Task 4: User Management

Define User Management

  • User management in Linux refers to the process of creating, modifying, and deleting user accounts on a Linux system. It involves managing user access, permissions, and security, as well as configuring user settings and preferences.

  • User management is an important aspect of Linux system administration, as it helps ensure the security and stability of the system. By creating separate user accounts for each user, administrators can control access to system resources and limit the potential for unauthorized access or data breaches.

  • Some of the key tasks involved in Linux user management include creating new user accounts, modifying user account settings, assigning permissions and privileges, setting user preferences, and disabling or deleting user accounts when necessary.

  • Here are the basic commands and tools used in user management:

  1. useradd - This command is used to create a new user account. By default, a home directory is created for the user in the /home directory.

  2. usermod - This command is used to modify user accounts, such as changing the user's home directory or adding the user to a new group.

  3. userdel - This command is used to delete a user account.

  4. passwd -This command is used to set or change a user's password.

  5. /etc/passwd - This file contains information about each user account, such as the username, user ID, home directory, and shell.

  6. /etc/shadow - This file contains encrypted password information for each user account.

  7. /etc/group - This file contains information about each group, such as the group name, group ID, and the list of users who belong to the group.

  8. chown - This command is used to change the owner of a file or directory.

  9. chmod - This command is used to change the permissions of a file or directory, such as granting read, write, or execute permissions to the user, group, or others.

Task 5: Create 2 users and just display their Usernames

grep 'abhi\|somya' /etc/passwd

Thank You,

Abhisek Moharana