Advanced Linux Shell Scripting for DevOps Engineers with User Management

Advanced Linux Shell Scripting for DevOps Engineers with User Management

Creating n number of directories using a script

  • Two ways of creating a directory

    1. One is a normal method which is described as below

       #!/bin/bash
       for i in {1..90}
       do
           mkdir days$i;
       done
      

    2. Using Iteration

#!/bin/bash
create_directory=$1
start_number=$2
end_number=$3
for ((i=$start_number;i<=$end_number;i++))
do
    mkdir $create_directory${i}
done

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

#!/bin/bash
src_dir=/home  #source File
tgt_dir=/mnt/c/Users/Dwarika/Downloads/script/dir #destination file 

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

tar czf $backup_file $src_dir
echo "backup completed"

Cron and Crontab, to automate the backup Script.

The word cron has been taken from the Greek word Chronos meaning time in Linux systems that allows users to schedule commands or scripts to run automatically at specified intervals.

Cron uses a table called a "cron table" or "crontab" to specify the schedule of the commands.

Crontab fields

*    *    *   *    *  Command_to_execute
|    |    |    |   |       
|    |    |    |    Day of the Week ( 0 - 6 ) ( Sunday = 0 )
|    |    |    |
|    |    |    Month ( 1 - 12 )
|    |    |
|    |    Day of Month ( 1 - 31 )
|    |
|    Hour ( 0 - 23 )
|
Min ( 0 - 59 )

  • It will take backup every day at 01:00 pm

User Management

User Administration is the process of managing different user accounts and their respective permissions in an operating system.

In Linux or Unix-based operating systems, we can create different user accounts, sort them into groups, change their set of permissions, or delete them

User accounts

In Linux, a single-user account generally belongs to a single user. The permissions for this user account determine the level of access the user will have while using the system.

User accounts are discussed in detail below.

Types of user accounts

  1. Super User: This account is also known as the root account. It has all the permissions and can run all the commands without any restriction. The keyword sudo is often used to run a command as the root user.

  2. Normal User: The general users of the system are assigned this account. This account has restricted access to the commands and files of the system. The root user can create such an account, modify its permissions or delete it.

  3. System User: This is the type of account that is created only for a specific purpose or software. For example, a mail account.

Groups

In Linux or Unix-based operating systems, we can form groups of user accounts. Groups are used to manage the user accounts collectively. We can manage the access permissions for the entire group.

A single user can be a part of multiple groups, and a group can have multiple users.

Create 2 users and just display their Usernames

  1. to add user

    • useradd <username>
  2. display Usernames

    • cat /etc/passwd |egrep -i '<username>|<username>'