Day 5 DevOps challenge
Advanced Linux Shell Scripting for DevOps Engineers with User management
Table of contents
✔️Shell script to create multiple directories at once :
#!/bin/bash
echo "creating multiple directories at once "
for (( i = $2; i < $3; i++ ))
do
mkdir "$1$i"
done
here the script working as :
At first, it prints "creating multiple directories at once "
then,
$1 is directory_name(day)
$2 is start_directory(1)
$3 is end_directory(90)
and After executing the script, we get the output as a list of directories as ...
✔️Script to backup all work done till now :
Create a backup script as below :
#!/bin/bash
back_up_ file="backup$(date +%y-%m-%d-%H-%M-%S).tar.gz"
dir="/home/ubuntu/"
tar -czf $back_up_file $dir
echo "backup completed: $back_up_file"
I have saved the script in
/home/ubuntu
path
you can save the script in your desired directory.Assign execute permission for the script using
chmod u+x <filename>.sh
List all the files using
ls
and execute the script as./back_up.sh
Now list the contents again using
ls
Now we can see the back_up file named
backup23-06-27-07-10-11.tar.gz
Use
ls -l backup23-06-27-07-10-11.tar.gz
Check for a back_up file in your directory.
Here is the workflow you can see
✔️Cron & crontab to automate the backup script:
💡what is cron ?
Cron is a time-based job scheduling system commonly used in Unix-like operating systems. It enables users to automate the execution of commands or scripts at specific intervals or on specific dates. Cron jobs are often employed for tasks like backups, system maintenance, and repetitive processes. The system relies on a daemon called cron, which runs continuously, and a configuration file called crontab, where users define the schedule and commands for their jobs. Cron ensures that scheduled tasks are executed reliably and efficiently, providing an essential tool for automating routine tasks in a Unix environment.
💡What is crontab?
Crontab is a Unix command used to create and manage scheduled tasks, known as cron jobs. It allows users to define specific commands and the desired timing for their execution. Each user has their own crontab file, which is edited using the crontab command. Cron jobs are essential for automating repetitive tasks in Unix-like operating systems
Format of cron job
✔️let's see how to create & use crontabs:
create a file
paste the below code there
#!/bin/bash a=$(date +%y-%m-%d-%H-%M-%S) touch /home/ubuntu/$a.deb
Then,
to list existing cron jobs type
crontab -l
select an editor and write your desired scheduling task ,type :
crontab -e
/home/ubuntu/<filename>.sh
To check the cron job status type :
sudo service cron status
To start the cronjob type :
sudo service cron start
the task will be running now to view type
ls
To stop the cronjob type
crontab -r
✔️User management:
what is user management in Linux?
User management in Linux refers to the process of creating, modifying, and removing user accounts within the operating system. It involves several key tasks:
User Creation: Administrators can create new user accounts with specific usernames and passwords, assigning unique user identifiers (UIDs) and group identifiers (GIDs) to each account.
User Authentication: Linux provides various methods for authenticating user credentials, such as password-based authentication, public key authentication, or integration with external authentication systems like LDAP.
User Permissions: Users can be assigned specific permissions and access levels to files, directories, and system resources, ensuring appropriate security and segregation of duties.
User Groups: Users can be organized into groups, simplifying the assignment of permissions and managing user privileges collectively.
User Modification: Administrators can modify user account attributes, such as the user's full name, home directory, default shell, password expiration, and account expiration.
User Removal: When a user account is no longer needed, it can be removed from the system, ensuring proper cleanup of resources associated with that user.
Effective user management is crucial for maintaining system security, controlling access, and ensuring accountability within a Linux environment.
✔️Create 2 users & just display their usernames
Type for user 1
sudo useradd user1 -m
type for add user 2
sudo useradd user2 -m
💡here is an idea to ensure users are added or not , by typing sudo cat /etc/passwd
this command:
you can see at the last of the terminal there are 2 users added sucessfully
set a password for the user1:
Type sudo passwd user1
Then, give a new password and retype the new password.
Then, your password was updated for the user1 successfully
similarly, you can set a password for user2 also.
Thank you so much for reading :)
Please let me know if you suggest some edits and improvements for this blog.
Follow me on LinkedIn to see interesting posts like this : )