Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management
Tasks:
- So Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates 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
syntax:
Example 2: When the script is executed as
./
createDirectories.sh
Movie 20 50
then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50
Create a Script to backup all your work done till now.
Backups are an important part of DevOps Engineers day to Day activities The video in References will help you to understand How a DevOps Engineer takes backups.
syntax:
On executing above script we will get below output:
Read About Cron and Crontab, to automate the Script.
Cron is a time-based job scheduler in Unix-like operating systems. It enables users to schedule commands or scripts to run automatically at specified intervals. The tasks scheduled with cron are called cron jobs. These jobs can be executed at regular intervals such as daily, weekly, monthly, or at specific times.
Crontab is a file that contains the commands or scripts to be executed by the cron daemon. It is a simple text file that contains a list of commands along with the time and frequency of their execution. Each user has their own crontab file that can be edited using the crontab command.
To create a cron job, you need to add an entry to your crontab file. Each entry consists of six fields separated by spaces. These fields specify the minute, hour, day of the month, month, day of the week, and the command or script to be executed. The syntax for creating a cron job is as follows:
- command to be executed
- — — — -
| | | | |
| | | | + — — — day of the week (0–6) (Sunday = 0)
| | | + — — — — month (1–12)
| | + — — — — — day of the month (1–31)
| + — — — — — — hour (0–23)
+ — — — — — — — min (0–59)
- command to be executed
For example, the following entry runs a script every day at midnight:
You can use the command crontab -e
to edit your crontab file and crontab -l
to list the current crontab file.
You can also use crontab -r
to remove your current crontab file, crontab -d
to delete a specific user crontab and cron -l -u <username>
to list a specific user crontab.
This cron job runs the script located at /path/to/script.sh
at minute 0 and hour 0 every day of the month, every month, and every day of the week.
Cron is a powerful tool that can automate many tasks, but it requires some knowledge and care to use it effectively. It is important to test and debug your cron jobs before deploying them to production to avoid unexpected behaviors.
How to Add/Edit Crontab
Crontab files are typically stored in the /etc/cron.d/ directory on Linux systems. The crontab command can be used to edit the crontab file.
crontab -e
By default, it will edit the crontab entries of the currently logged-in user. To edit other user crontab use the command below:
crontab -u username -e
How to List Crontab
To view the crontab entries of current users use the following command.
crontab -l
Use -u followed by the username to view the crontab entries of the specified user.
crontab -u username -l
Automating the backup script through crontab.
4.Read about User Management
In Linux, user management refers to the process of creating, modifying, and deleting user accounts and their associated permissions and privileges. Linux is a multi-user operating system, which means that it can support multiple users simultaneously. Each user has a unique username and password that allows them to log in to the system and perform various tasks.
Here are some of the basic commands and concepts related to user management in Linux:
Adding a user: The “useradd” command is used to add a new user account to the system. For example, to create a new user named “john,” you would type “sudo useradd john” in the terminal.
Setting a password: Once you have added a new user, you can set a password for their account using the “passwd” command. For example, to set a password for the user “john,” you would type “sudo passwd john” and then enter and confirm the new password.
Modifying user properties: The “usermod” command is used to modify user properties such as the user’s home directory, default shell, or group membership. For example, to change the default shell for the user “john” to bash, you would type “sudo usermod -s /bin/bash john.”
Deleting a user: The “userdel” command is used to delete a user account from the system. For example, to delete the user “john,” you would type “sudo userdel john.”
Switching user: The “su” command allows you to switch to another user account. For example, to switch to the user “john,” you would type “su john” and enter the password for that account.
Managing groups: In Linux, users can be members of one or more groups, which determine the permissions and access rights that they have on the system. The “groupadd” command is used to create a new group, while “usermod” can be used to add or remove users from a group.
Checking user information: The “id” command can be used to display information about a user, including their username, user ID, and group memberships.
Overall, user management is an important aspect of Linux administration and is essential for ensuring the security and stability of a system. By properly managing user accounts and permissions, administrators can prevent unauthorized access and maintain control over system resources.
Create two users and just display their Usernames
root - It is the superuser that can perform all the actions in a server. This privilege is given at the time of server spin-up.
With useradd commands, you can add a user.
Syntax:
useradd -c "comment" username
To delete a user account userdel command is used.
Syntax:
userdel -r <userName>
The command usermod is used to modify the properties of an existing user.
Syntax:
usermod -c <'newName'> <oldName>
/etc/passwd - This file contains the detailed information of all the users created in a server.
Add two users as below:
Display two users by using below command:
syntax: cat /etc/passwd