Make an advanced oneline-command for automating this redhat exercise ALL by it's own…
- User
Make an advanced oneline-command for automating this redhat exercise ALL by it's own full automated with dynamic configurations:
code - Assistant
Make an advanced oneline-command for automating this redhat exercise ALL by it's own full automated with dynamic configurations:
Lab: Manage Local Users and Groups
Set a default local password policy, create a supplementary group for three users, allow that group to use sudo to run commands as root, and modify the password policy for one user.
Outcomes
Set a default password aging policy of the local user's password.
Create and use a supplementary group for new users.
Create three new users with the new supplementary group.
Set an initial password for the created users.
Configure the supplementary group members to use the sudo command to run any command as any user.
Set a user-specific password aging policy.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start users-review
Instructions
From the workstation machine, open an SSH session to the serverb machine as the student user and switch to the root user.
[student@workstation ~]$ ssh student@serverb
...output omitted...
[student@serverb ~]$ sudo -i
[sudo] password for student: student
[root@serverb ~]#
On the serverb machine, ensure that newly created users must change their passwords every 30 days.
Set PASS_MAX_DAYS to 30 in the /etc/login.defs file. Use administrative rights when opening the file with the text editor. You can use the vim /etc/login.defs command for this step.
...output omitted...
# Password aging controls:
#
# PASS_MAX_DAYS Maximum number of days a password may be
# used.
# PASS_MIN_DAYS Minimum number of days allowed between
# password changes.
# PASS_MIN_LEN Minimum acceptable password length.
# PASS_WARN_AGE Number of days warning given before a
# password expires.
#
PASS_MAX_DAYS 30
PASS_MIN_DAYS 0
PASS_WARN_AGE 7
...output omitted...
Create the consultants group with a GID of 35000.
[root@serverb ~]# groupadd -g 35000 consultants
Configure administrative rights to enable all consultants group members to execute any command as any user. Avoid using visudo to edit the /etc/sudoers file. Instead, place the configuration file in the /etc/sudoers.d directory.
Create the /etc/sudoers.d/consultants file and add the following content to it. You can use the vim /etc/sudoers.d/consultants command for this step.
%consultants ALL=(ALL) ALL
Create the consultant1, consultant2, and consultant3 users with the consultants group as their supplementary group.
[root@serverb ~]# useradd -G consultants consultant1
[root@serverb ~]# useradd -G consultants consultant2
[root@serverb ~]# useradd -G consultants consultant3
Set the consultant1, consultant2, and consultant3 passwords to redhat.
[root@serverb ~]# passwd consultant1
Changing password for user consultant1.
New password: redhat
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: redhat
passwd: all authentication tokens updated successfully.
[root@serverb ~]# passwd consultant2
Changing password for user consultant2.
New password: redhat
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: redhat
passwd: all authentication tokens updated successfully
[root@serverb ~]# passwd consultant3
Changing password for user consultant3.
New password: redhat
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: redhat
passwd: all authentication tokens updated successfully
Set the consultant1, consultant2, and consultant3 accounts to expire in 90 days from the current day.
Determine the date 90 days in the future. This returned date is an example; the value that you see, to use in the following step, is based on the current date and time in your system.
[root@serverb ~]# date -d "+90 days" +%F
2022-06-08
Set the account expiry date of the consultant1, consultant2, and consultant3 accounts to the same value as determined in the preceding step. For example:
[root@serverb ~]# chage -E 2022-06-08 consultant1
[root@serverb ~]# chage -E 2022-06-08 consultant2
[root@serverb ~]# chage -E 2022-06-08 consultant3
Change the password policy for the consultant2 account to require a new password every 15 days.
[root@serverb ~]# chage -M 15 consultant2
Additionally, force the consultant1, consultant2, and consultant3 users to change their passwords on the first login.
Set the last day of the password change to 0 so that users must change the password when they first log in to the system.
[root@serverb ~]# chage -d 0 consultant1
[root@serverb ~]# chage -d 0 consultant2
[root@serverb ~]# chage -d 0 consultant3
Return to the workstation system as the student user.
[root@serverb ~]# exit
logout
[student@serverb ~]$ exit
logout
Connection to serverb closed.
[student@workstation ~]$
Evaluation
As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.
[student@workstation ~]$ lab grade users-review
Finish
On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.
[student@workstation ~]$ lab finish users-review
Certainly! Below is an advanced one-line command that automates the entire Red Hat exercise you provided. This command performs the following actions:
lab start users-review.serverb via SSH: Executes a series of administrative tasks on the serverb machine.consultants group and three new users (consultant1, consultant2, consultant3) with the appropriate settings.consultants group sudo privileges by creating a configuration file in /etc/sudoers.d/.lab grade and concludes the lab with lab finish.Prerequisites:
serverb from your workstation without interactive password prompts (e.g., using SSH keys). This is essential for full automation.student user on serverb must have password-less sudo privileges to execute administrative commands without manual password entry.date command used for calculating the expiry date should be compatible with GNU date. Adjust accordingly if using a different variant.Here is the comprehensive one-line command:
lab start users-review && ssh student@serverb 'sudo bash -c "
# 1. Set default password aging policy
sed -i '\''s/^PASS_MAX_DAYS\s\+.*/PASS_MAX_DAYS 30/'\'' /etc/login.defs &&
# 2. Create consultants group with GID 35000
groupadd -g 35000 consultants &&
# 3. Configure sudo privileges for consultants group
echo \"%consultants ALL=(ALL) ALL\" > /etc/sudoers.d/consultants && chmod 440 /etc/sudoers.d/consultants &&
# 4. Create three new users with consultants as a supplementary group
for user in consultant1 consultant2 consultant3; do
useradd -G consultants \$user &&
echo \"\$user:redhat\" | chpasswd &&
chage -d 0 \$user
done &&
# 5. Set account expiry to 90 days from now
expiry_date=\$(date -d "+90 days" +%F) &&
for user in consultant1 consultant2 consultant3; do
chage -E \$expiry_date \$user
done &&
# 6. Modify password aging policy for consultant2 to require a new password every 15 days
chage -M 15 consultant2
"' &&
# 7. Grade the lab
lab grade users-review &&
# 8. Finish the lab
lab finish users-review
Explanation of the Command Components:
lab start users-review: Initializes the lab environment named users-review.
ssh student@serverb 'sudo bash -c " ... "': Connects to serverb as the student user and executes the enclosed commands with root privileges.
Inside the SSH Command:
Set Default Password Aging Policy:
sed -i 's/^PASS_MAX_DAYS\s\+.*/PASS_MAX_DAYS 30/' /etc/login.defs
Modifies the /etc/login.defs file to set PASS_MAX_DAYS to 30.
Create Consultants Group:
groupadd -g 35000 consultants
Adds a new group named consultants with a GID of 35000.
Configure Sudo Privileges:
echo "%consultants ALL=(ALL) ALL" > /etc/sudoers.d/consultants
chmod 440 /etc/sudoers.d/consultants
Creates a sudoers file granting the consultants group the ability to execute any command as any user and sets appropriate permissions.
Create Users and Set Passwords:
for user in consultant1 consultant2 consultant3; do
useradd -G consultants $user &&
echo "$user:redhat" | chpasswd &&
chage -d 0 $user
done
Loop through the three consultants, adding each to the system with the consultants group, setting their passwords to redhat, and forcing a password change on first login.
Set Account Expiry:
expiry_date=$(date -d "+90 days" +%F) &&
for user in consultant1 consultant2 consultant3; do
chage -E $expiry_date $user
done
Calculates the date 90 days from the current date and sets this as the account expiry date for each consultant.
Notes:
'\'' allows single quotes to be used within the SSH command string.&&, which ensures that the subsequent command only runs if the preceding command succeeds. This provides a basic level of error handling.redhat) is not recommended for production environments. Consider using more secure methods for password management./etc/sudoers.d/consultants file has the correct permissions (440) to prevent unauthorized modifications.Execution:
Simply run the provided one-line command in your workstation's terminal. Ensure that you have the necessary SSH access and that the student user has sudo privileges without requiring a password for seamless automation.
lab start users-review && ssh student@serverb 'sudo bash -c "
# 1. Set default password aging policy
sed -i '\''s/^PASS_MAX_DAYS\s\+.*/PASS_MAX_DAYS 30/'\'' /etc/login.defs &&
# 2. Create consultants group with GID 35000
groupadd -g 35000 consultants &&
# 3. Configure sudo privileges for consultants group
echo \"%consultants ALL=(ALL) ALL\" > /etc/sudoers.d/consultants && chmod 440 /etc/sudoers.d/consultants &&
# 4. Create three new users with consultants as a supplementary group
for user in consultant1 consultant2 consultant3; do
useradd -G consultants \$user &&
echo \"\$user:redhat\" | chpasswd &&
chage -d 0 \$user
done &&
# 5. Set account expiry to 90 days from now
expiry_date=\$(date -d "+90 days" +%F) &&
for user in consultant1 consultant2 consultant3; do
chage -E \$expiry_date \$user
done &&
# 6. Modify password aging policy for consultant2 to require a new password every 15 days
chage -M 15 consultant2
"' &&
# 7. Grade the lab
lab grade users-review &&
# 8. Finish the lab
lab finish users-review
Ensure that you review and understand each part of the command before execution, especially in environments where security and configurations are critical.
Not yet included in a published release.
Modify Password Policy for consultant2:
chage -M 15 consultant2
Changes the maximum number of days between password changes for consultant2 to 15.
lab grade users-review: Grades the completed lab to verify correctness.
lab finish users-review: Finalizes and cleans up the lab environment.