day5 of #90DaysOfDevops

Advanced Linux Shell Scripting for DevOps Engineers with User management

·

4 min read

1. Write a bash script to create directories. so that when the script is executed with three given arguments (one is the directory name and second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

image

image

image

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

image

NOTE: The cp command is used to copy files and directories, but by default it will only copy files. To copy a directory, you must use the -r option to enable recursive copying, which will copy the directory and all of its contents, including any subdirectories and files.

image

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

Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit or delete entries to cron. A crontab file is a user file that holds the scheduling information.

  • Cron is a time-based scheduling daemon that runs on Unix and Linux systems. It is used to schedule commands or scripts to run automatically at a certain time or date.

Crontab is a configuration file that specifies shell commands to run periodically. It is named after the command used to manage the configuration file: crontab. The configuration file is stored in a simple text file, and you can use the crontab command to edit it.

To use crontab to schedule a script or command, you would do the following:

  1. Open the crontab configuration file by running crontab -e

  2. Add a new line to the file with the schedule and command you want to run

  3. Save and exit the file Cron will automatically run the command or script at the specified time according to the configuration in the crontab file.

You can also use the crontab -l command to list the current crontab configuration, and crontab -r to remove the current crontab configuration.

Detailed explanation about cronjobs

4. Read about User Management

A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users hence the ids for local user begins from 1000 onwards.

  • Get a List of All Users using the /etc/passwd File

  • Each line in the file has seven fields delimited by colons that contain the following information:

User name.

Encrypted password (x means that the password is stored in the /etc/shadow file).

User ID number (UID).

User’s group ID number (GID).

Full name of the user (GECOS).

User home directory.

Login shell (defaults to /bin/bash)

  • If you want to display only the username you can use either awk or cut commands to print only the first field containing the username:

  • The getent command displays entries from databases configured in /etc/nsswitch.conf file, including the password database, which can be used to query a list of all users

  • to check whether a user exists in our Linux box we, can simply filter the users’ list by piping the list to the grep command.

For example, to find out if a user with the name charan exists in our Linux system we can use the following command:

image

  • If you want to find out how many users accounts you have on your system, pipe the getent passwd output to the wc command:

  • Each user has a numeric user ID called UID. If not specified when creating a new user with the useradd command, the UID will be automatically selected from the /etc/login. defs file depending on the UID_MIN and UID_MAX values.

  • To check the UID_MIN and UID_MAX values on your system, you can use the following command:

5. Create 2 users and just display their Usernames

image

image

That's it for Today. Thanks for your time.

Please share your valuable feedback by liking 👍, sharing🤝 and commenting.

See you tomorrow, with another blog.

#day5 #90DaysOfDevops #Linux #DevOps

Did you find this article valuable?

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