Day 5 Advanced Linux Shell Scripting for DevOps Engineers with User management

Day 5 Advanced Linux Shell Scripting for DevOps Engineers with User management

Tasks:

  1. 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 -

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

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

  4. Read about User Management and Let me know on Linkedin if you're ready for Day 6.

  5. Create 2 users and just display their Usernames

1.) 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 -

Here is a shell script to print all days of the week using a loop:

This script first gets the current day of the week using the date command. Then, it sets the start and end days of the week to 1 (Monday) and 7 (Sunday), respectively. Finally, it loops through all days of the week, printing each one to the console.

Here is another shell script to print all days of the week using a command with start day and end day variables as arguments:

This script takes two arguments, the start day and the end day. It then loops through all days of the week, printing each one to the console.

To use either of these scripts, you would need to save them as a file with a .sh extension. Then, you could run them by typing the following command in the terminal:

This would print all days of the week to the console.

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

Here is a shell script to backup all your work done till now:

This script first gets the current date and time. Then, it gets the path to the directory containing your work. Next, it gets the path to the backup directory. If the backup directory does not exist, it creates it. Finally, it creates a backup of the work directory and prints a message to indicate that the backup was successful.

To use this script, you would need to save it as a file with a .sh extension. Then, you could run it by typing the following command in the terminal:

This would create a backup of your work directory in the /path/to/backup/directory directory.

Here are some things to keep in mind when using this script:

  • The work_dir variable should be set to the path to the directory containing your work.

  • The backup_dir variable should be set to the path to the backup directory.

  • The backup.sh script should be saved in the same directory as the work directory.

You can also modify this script to backup specific files or directories, or to create backups on a regular schedule.

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

Cron is a time-based job scheduler in Unix-like operating systems. It is used to schedule commands to run at regular intervals. Crontab is a file that contains the cron jobs for a specific user.

To automate the backup script using Cron, you would need to add the following line to your crontab file:

0 0 * * * bash /path/to/backup.sh

This line tells Cron to run the backup.sh script at 00:00 (midnight) every day.

You can also use Cron to run the backup script on a more specific schedule. For example, to run the script every Monday at 10:00 AM, you would use the following line:

0 10 * * 1 bash /path/to/backup.sh

For more information on Cron and Crontab, you can refer to the following resources:

4.) Read about User Management and Let me know on Linkedin if you're ready for Day 6.

A user management system in Linux is a set of processes and procedures for creating, managing, and deleting user accounts. It also includes the ability to assign permissions to users and groups and to control access to system resources.

The user management system in Linux is implemented using several files and directories, including:

  • /etc/passwd: This file contains a list of all user accounts on the system. Each line in the file contains the username, password, user ID, group ID, and other information about the user account.

  • /etc/group: This file contains a list of all groups on the system. Each line in the file contains the group name, group ID, and a list of members of the group.

  • /etc/shadow: This file contains the encrypted passwords for all user accounts. This file is only readable by root.

  • /etc/skel: This directory contains a set of files that are copied to the home directory of newly created user accounts.

The user management system in Linux can be managed using several commands, including:

  • useradd: This command is used to create a new user account.

  • usermod: This command is used to modify an existing user account.

  • userdel: This command is used to delete a user account.

  • passwd: This command is used to change the password for a user account.

  • groups: This command is used to list the groups that a user belongs to.

I am ready for Day 6! Let me know what you want me to do today.

5.) Create 2 users and just display their Usernames

To create 2 users and display their usernames, you can use the following command:

useradd -m -s /bin/bash user1 user2

This command will create two new user accounts, user1 and user2, with a home directory of /home/user1 and /home/user2 respectively, and a default shell of /bin/bash.

To display the usernames of the newly created users, you can use the following command:

cat /etc/passwd | grep user

This command will print all lines in the /etc/passwd file that contain the string user. The output of this command should be:

user1:x:1000:1000:user1:/home/user1:/bin/bash
user2:x:1001:1001:user2:/home/user2:/bin/bash

The x in the password field is a placeholder for the encrypted password.

Thank You

If you liked this blog then click on ❤

and do follow for more interesting and helpful blogs.

Did you find this article valuable?

Support GCP by becoming a sponsor. Any amount is appreciated!