"Today, let's dive into advanced Linux Shell Scripting with a focus on User Management. Shell Scripting is an essential skill for DevOps engineers enabling automation and auto-scaling. So, let's get started and dive in!"
1. Creating a Dynamic Directories:
Let's start by creating a bash script named as createdirectories.sh
that generates a specified number of directories with a dynamic directory name.
Specify three arguments: <directory-name>, <start-number>, and <end-number>.
This setup facilitates the creation of a dynamic directory structure with the given parameters.
For instance, when provided with "directory" as the directory name, "01" as the start number, and "20" as the end number, the script will generate directory01, directory02, and subsequent directories accordingly.
2.Create a Script to backup all your work done till now:
Below scripts create a backup of your work:
/home/ubuntu/scripts
is the path to the directory you want to backup, and /home/ubuntu/backups
is the path to the directory where you want to store the backups.
Make sure to give execute permissions to the script:
You can then run the script to create a backup of your work:
This will create a backup directory with a specified timestamp.
3.What is cron and crontab:
The cron is a software utility, offered by a Linux operating system that automates the scheduled task at a predetermined time. It enables the users to execute the scheduled task on a regular basis like doing the backup every day at midnight, scheduling updates on a weekly basis, synchronizing the files at some regular interval. Cron checks for the scheduled job recurrently and when the scheduled time fields match the current time fields, the scheduled commands are executed. It is started automatically from /etc/init.d on entering multi-user run levels.
Crontab is the command used to interact with the cron daemon. It allows users to create, view, edit, and delete cron jobs. Each user on a Unix system can have their own crontab file, which contains the schedule of commands to be executed by cron.
Here's a brief overview of how to use crontab to automate the backup script:
Open your terminal and run the following command to open the crontab editor:
crontab -e
Some useful commands crontab commands to use in the terminal :
crontab -e
: Edit the crontab file or create one if it doesn’t already exist.crontab -l
: Display crontab file contents.crontab -r
: Remove your current crontab file.crontab -i
: Remove your current crontab file with a prompt before removal.If this is your first time editing the crontab, you'll be prompted to choose an editor (e.g., nano, vim). Select your preferred editor.
Once the crontab editor opens, you can add a new line at the end of the file to schedule your backup script. The syntax for a cron job is as follows:
* * * * * command_to_execute - - - - - | | | | | | | | | +----- Day of the week (0 - 7) (Sunday is 0 or 7) | | | +------- Month (1 - 12) | | +--------- Day of the month (1 - 31) | +----------- Hour (0 - 23) +------------- Minute (0 - 59)
For example, if you want to run your backup script every day at 5:00 AM, you will add the following line:
0 5 * * * /home/ubuntu/scripts/backups.sh
Replace /home/ubuntu/scripts/backups.sh
with the actual path to your backup script.
After adding the line, save and exit the crontab editor. In nano, you can do this by pressing Ctrl + X
, then Y
to confirm, and finally Enter
to save.
Your backup script will now run automatically according to the schedule specified in the crontab file.
Explanation:
0
- Minute field, specifying that the task should run when the minute is 0 (on the hour).5
- Hour field, specifying that the task should run at 5 AM.*
- The day of the month.*
- month fields.*
- Day of the week field, specifying that the task should run on Sunday./home/ubuntu/scripts/backups.sh
- The command or script to be executed.
4. User management in Linux:
Linux is a fantastic platform that allows multi-user access options. Different users can access the same Linux OS for working simultaneously. A user account allows multiple people to access directories, terminals, the Internet, etc. There are three types of user accounts:
User Account: This account is for general use with limited permissions.
System Account: This account is used for operating specific programs such as daemon, MySQL, mail, etc.
Root Account: This account is for administrators. It has unlimited access to change the system.
Creating Users: Creating users is one of the most common tasks in Linux. To create a new user, you can use the
adduser
oruseradd
command. For example:sudo adduser username
Give password to the Users: If you want to assign a password to the user, execute the
passwd
command in the terminal.sudo passwd username
Modifying Users: The
usermod
command is used to modify user account properties such as username, home directory, shell, etc.sudo usermod -d /new/home/directory username
Deleting Users: To delete a user account, you can use the
deluser
oruserdel
command.sudo userdel username
User Information: To display information about a user, you can use the
id
command.id username
Display Detailed Information of a User's: You can use the lslogins command to check the comprehensive information of all users. This command provides the complete overview of all users and groups, and here is the example:
$ lslogins
List the Users
You can either use compgen command or /etc/passwd to list down all system users. Output from compgen command is simpler than compared to the /etc/passwd. These are the commands you can execute.
$ compgen -u
$ compgen -u
$ cat /etc/passwd
$ cat /etc/passwd
Managing User Groups: Users can belong to one or more groups. To manage group membership, you can use commands like
groups
,usermod
, andgroupmod
.Sudo Access: Users can be granted sudo (superuser) access to perform administrative tasks. This is configured in the
/etc/sudoers
file.User Home Directories: Each user has a home directory where they store their files by default. The home directory path typically follows the format
/home/username
.User IDs (UIDs): Each user is assigned a unique User ID (UID) in Linux. The root user has a UID of 0, while regular users typically have UIDs starting from 1000 onwards.
Group IDs (GIDs): Each group is assigned a unique Group ID (GID) in Linux. Groups are used to manage file permissions and group membership.
5.Create 2 users and just display their Usernames
To create two users and display their usernames, you can use the following commands:
sudo adduser user1
sudo adduser user2
These commands will 2 create users. Once the users are created, you can simply display their usernames using the id
command:
id -nG user1
id -nG user2
The -n
option in the id
command is used to display only the usernames, and the -G
option specifies the primary group for each user. Alternatively, you can also use the awk -F':' '{print $1}' /etc/passwd
command to display user information:
awk -F':' '{print $1}' /etc/passwd
This will display the usernames of the users user1
and user2
.
This is the #Day05 of the #90DaysofDevOps challenge! Hope you found this article informative and useful so please share it with others who might benefit from it.
Thanks for reading this article.
Keep Learning...