Advanced Linux Shell Scripting for DevOps Engineers with User management

Advanced Linux Shell Scripting for DevOps Engineers with User management

In IT Compay your manager tell you create 90 directories. So what did you think, how did I create 90 directories? Manually one by one or using a script, or a command?

All 90 directories within seconds using a simple command.

Tasks:

Task1: 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 -

Example 1: When the script is executed as

./createDirectories.shday 1 90

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

This is the code using (for loop) to create 90 directories. After that we have save to file this file and run the file when we will run the file the give you error that is permission denied. so we write some command and provide permission.

After that we will run the code.

After that they will create one Directories that is

After that we enter this Directories and check all the directories that is day(1-90) is created. that is real output we want.

Example 2: When the script is executed as

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

It is same process to Example 1

This is the code using (for loop) to create 90 directories. After that we have save to file this file and run the file when we will run the file the give you error that is permission denied. so we write some command and provide permission.

After that we will run the code.

After that they will create one Directories that is

After that we enter this Directories and check all the directories that is day(1-90) is created. that is real output we want.

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

Backups are an important part of DevOps Engineer's day to Day activities. So we will right the code.

After that we run the code and record the backup to for backup Directories. The output is:

Task3: Cron and Crontab, to automate the backup Script.

cron and crontab are components in Linux that allow users to automate tasks, including backup scripts, at scheduled intervals.

cron: is a time-based job scheduler in Unix-like operating systems. It runs in the background and executes commands or scripts at predefined times without user intervention.

To access the cron configuration, you can use the following command:

This opens the crontab file for editing. Each line in the crontab file represents a scheduled task. The syntax for a cron job is as follows:

The five asterisks represent the time and date information for scheduling:

  • Minute (0 - 59)

  • Hour (0 - 23)

  • Day of month (1 - 31)

  • Month (1 - 12)

  • Day of week (0 - 6, where 0 is Sunday)

For example, the following cron job runs a backup script every 2 Minutes:

crontab is a command-line utility that allows users to create, edit, and manage their cron jobs. The crontab -e command opens the crontab file for editing, and crontab -l displays the current cron jobs.

Common crontab Commands:

  • `crontab -e`: Edit the crontab file.

  • crontab -l`: Display the current crontab.

  • crontab -r`: Remove the current crontab.

Example:

Suppose you have a backup script named backfile.sh located in /path/to/backfile.sh, and you want to schedule it to run every 2 Minutes. You can achieve this by adding the following line in your crontab:

After saving the crontab file, this backup script will automatically execute at the specified time.

Remember to provide the correct path to your script and ensure that it has the necessary permissions to run. Regularly check the logs and output of your cron jobs to ensure they are running as expected.

This automation approach helps streamline tasks like backup processes, ensuring they are executed at specific intervals without manual intervention.

The output is

you can check now we have 2 tar file that is create it 1st tar file we created task 2. and second tar file created automatically every 2 min that is user crontab.

Task4: What is User Management in Linux:

User management in Linux involves the administration and control of user accounts on a Linux system. This encompasses creating, modifying, and deleting user accounts, as well as managing user privileges and access.

Here are key aspects of user management in Linux:

  • User Accounts:

Creating Users: The useradd command is used to create new user accounts. For example: UserName: Ram

Setting Passwords: The passwd command is used to set or change passwords for user accounts.

Deleting Users: The userdel command removes user accounts. It is often used with the -r option to remove the user's home directory.

  • User Groups:

    Creating Groups: The groupadd command is used to create new groups.add groupname

Adding Users to Groups: The usermod command can be used to add a user to a group.

  • User Privileges:

    Root Access: Users can be granted root (administrator) access using the sudo command. Users must be added to the sudo group.

  • User Information:

    Viewing User Information: The id commands provide information about a user.

  • Login Control:

    Account Locking/Unlocking: The passwd command can be used to lock or unlock user accounts.

  • File Permissions:

    Ownership and Permissions: Users have ownership and permissions on files and directories. The chown and chmod commands are used for modifying ownership and permissions.

  • User Environment:

    Shell Configuration: The chsh command allows users to change their default shell.

  • Account Expiry:

    Setting Account Expiry: The chage command can be used to set expiration dates for user accounts.

  • Logging and Auditing:

    User Login Logs: User login information is recorded in log files like /var/log/auth.log or /var/log/secure.

User management is a crucial aspect of Linux system administration, ensuring proper control, security, and organization of user accounts on a system. Regularly reviewing and updating user permissions and access is important for maintaining a secure and well-organized environment.

Task5: How to add user using function.

Conclusion:

This blog all about Advance Shell Scripting. Also we have completed some tasks in this blog that is used Daily to Daily life in IT Company.

I hope you enjoyed this blog on learning advance shell scripting for DevOps engineers. I have completed advance shell script in this blog. For Linux blog we will continue next blog.