Day-05 Advanced Linux Shell Scripting for DevOps Engineers with User management

Irfan S MominIrfan S Momin
4 min read

Tasks

  1. So Write a bash script create directories.sh that when the script is executed with three given arguments (one is the directory name, 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

    ./createDirectories.sh day 1 90

    then it creates 90 directories as day1 day2 day3 .... day90

    Example 2: When the script is executed as

    ./createDirectories.sh Movie 20 50 then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50

  2. Create a Script to back up all your work done till now.

    Backup script as a function

  3. Read About Cron and Crontab, to automate the backup Script

    Cron & Crontab:

    Cron is an effective and popular command-line utility used to schedule a broad range of tasks at a specified time without user interaction. The scheduled tasks are known as cron jobs while the crontab is the list of the file containing the cron jobs.

    Cron can be used to automate tasks in the following scenarios:

    • Running schedule backups,

    • Checking disk space at a specific interval,

    • Running automatic maintenance,

    • Periodically removing unwanted files,

    • Running network diagnostic

Syntax:

It is easier to work with cron, once you understand its syntax. The general syntax to use cron is:

View crontab:

To view the crontab file, use the following command:

$ crontab –l

To view the crontab file of a particular user, use the following command:

$ sudo crontab –u user -l

Edit crontab:

To edit the crontab file of the current user:

$ crontab –e

To edit the crontab file of a particular user:

$ sudo crontab -u user

If you are editing a crontab file for the first time, you will be asked to choose an editor.

Once you select the editor, the following window will appear.

Example: Cron job to take my script backup at every minute

  1. Read about User Management

    One essential component of keeping a system secure is user management. A common result of poor user and privilege management is the compromising of numerous systems. As a result, you must comprehend how to safeguard your server using straightforward user account management strategies.

    There are three types of accounts on a Unix system −

    • Root account: This is also called a superuser and would have complete and unfettered control of the system. A superuser can run any command without any restriction. This user should be assumed as a system administrator.

    • System accounts: System accounts are those needed for the operation of system-specific components for example mail accounts and sshd accounts. These accounts are usually needed for some specific function on your system, and any modifications to them could adversely affect the system.

    • User accounts: User accounts provide interactive access to the system for users and groups of users. General users are typically assigned to these accounts and usually have limited access to critical system files and directories.

      Unix supports a concept of Group Account which logically groups several accounts. Every account would be a part of another group account. A Unix group plays an important role in handling file permissions and process management.

      Managing Users and Groups

      There are four main user administration files −

      /etc/passwd − Keeps the user account and password information. This file holds the majority of information about accounts on the Unix system. /etc/shadow − Holds the encrypted password of the corresponding account. Not all the systems support this file.

      /etc/group − This file contains the group information for each account. /etc/gshadow − This file contains secure group account information.

The following table lists out commands that are available on the majority of Unix systems to create and manage accounts and groups −

  • useradd -Adds accounts to the system

  • usermod -Modifies account attributes

  • userdel -Deletes accounts from the system

  • groupadd -Adds groups to the system

  • groupmod -Modifies group attributes

  • groupdel -Removes groups from the system

  1. Create 2 users and just display their Usernames.

    User Account Properties

1
Subscribe to my newsletter

Read articles from Irfan S Momin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Irfan S Momin
Irfan S Momin

A Foodie Software Engineer's Journey to Eating Healthy and Learning Something New Every Day