Hello, I was wondering if you can help me?
- User
Hello, I was wondering if you can help me?
- Assistant
Of course! I'll do my best to assist you. What do you need help with?
- User
I need a simple BASH script for joining RHEL 8 systems to an Active Directory Domain. Can you provide an example please?
- Assistant
Sure! Here's a simple BASH script to join a RHEL 8 system to an Active Directory Domain:
#!/bin/bash # Set the realm and domain variables REALM="YOUR_REALM" DOMAIN="your-domain.com" # Prompt for the Active Directory admin username and password read -p "Admin username: " ADMIN_USER read -s -p "Admin password: " ADMIN_PASS echo # Install required packages yum install -y realmd samba-common-tools oddjob oddjob-mkhomedir sssd # Discover and join the Active Directory domain realm discover $DOMAIN realm join $DOMAIN -U $ADMIN_USER --verbose # Enable and start necessary services systemctl enable sssd systemctl start sssd # Display the Active Directory users and groups realm listBefore running the script, make sure to replace the
YOUR_REALMandyour-domain.complaceholders with your actual Active Directory realm and domain. Also, ensure you have appropriate administrative privileges to join machines to the domain.Once you've made these changes, save the script in a file, for example,
join-ad-domain.sh. Make it executable using the commandchmod +x join-ad-domain.sh.To run the script, open a terminal and navigate to the directory where you saved the file, then execute the command
./join-ad-domain.sh. Follow the prompts to enter the admin username and password, and the script will take care of the rest.Note: It's important to test this script in a non-production environment before attempting to join any critical systems to an Active Directory domain.
Let me know if you need further assistance!
- User
Interesting, can you include an option into the same BASH script for adding an Active Directory Group to the RHEL 8 admins as well?
- Assistant
Certainly! Here's an updated version of the BASH script that includes an option to add an Active Directory group to the RHEL 8 admins:
#!/bin/bash # Set the realm and domain variables REALM="YOUR_REALM" DOMAIN="your-domain.com" # Prompt for the Active Directory admin username and password read -p "Admin username: " ADMIN_USER read -s -p "Admin password: " ADMIN_PASS echo # Install required packages yum install -y realmd samba-common-tools oddjob oddjob-mkhomedir sssd # Discover and join the Active Directory domain realm discover $DOMAIN realm join $DOMAIN -U $ADMIN_USER --verbose # Enable and start necessary services systemctl enable sssd systemctl start sssd # Display the Active Directory users and groups realm list # Option to add an Active Directory group to the RHEL 8 admins read -p "Do you want to add an Active Directory group to the RHEL 8 admins? (y/n): " ADD_GROUP if [[ $ADD_GROUP == "y" || $ADD_GROUP == "Y" ]]; then read -p "Enter the Active Directory group name: " GROUP_NAME # Add the Active Directory group to sudoers echo "%${GROUP_NAME} ALL=(ALL) ALL" >> /etc/sudoers echo "Group '${GROUP_NAME}' has been added to the RHEL 8 admins." fiIn this updated script, after joining the RHEL 8 system to the Active Directory domain and starting the necessary services, it prompts the user if they want to add an Active Directory group to the RHEL 8 admins. If the user enters 'y' or 'Y' followed by pressing Enter, it prompts for the Active Directory group name and adds the group to the sudoers file, granting administrative privileges.
Remember to replace the
YOUR_REALMandyour-domain.complaceholders with the actual Active Directory realm and domain. Also, ensure you have appropriate administrative privileges to make changes to the sudoers file.Save the script, make it executable using the command
chmod +x join-ad-domain.sh, and run it with the command./join-ad-domain.sh. Follow the prompts to join the domain and optionally add an Active Directory group to the RHEL 8 admins.Let me know if there's anything else I can assist you with!
- User
Those commands don't look quite right for BASH and Active Directory. Can you provide an example of how the variable for {GROUP_NAME} should look?
- Assistant
Apologies for the confusion. When adding an Active Directory group to the
sudoersfile, the group name should be prefixed with the domain. Here's an example of how the{GROUP_NAME}variable should look:GROUP_NAME="YOUR_DOM\group-name"Replace
YOUR_DOMwith the appropriate domain (e.g.,ACME) andgroup-namewith the name of the Active Directory group you want to add to the RHEL 8 admins.So, for instance, if your Active Directory domain is
acme.comand you want to add the group namedadminsto the RHEL 8 admins, the line should be:GROUP_NAME="ACME\admins"Make sure to update the
GROUP_NAMEvariable in the script accordingly. - User
In the script you used as an example, it seems to just append names as text into the /etc/sudoers file. Shouldn't those same names also be added to the "wheel"
- Assistant
Apologies for the oversight. Yes, you are correct. In addition to adding the Active Directory group to the
sudoersfile, it is also advisable to add the group to thewheelgroup, which typically grants administrative privileges. Here's an updated version of the script that includes adding the Active Directory group to both thesudoersfile and thewheelgroup:#!/bin/bash # Set the realm and domain variables REALM="YOUR_REALM" DOMAIN="your-domain.com" # Prompt for the Active Directory admin username and password read -p "Admin username: " ADMIN_USER read -s -p "Admin password: " ADMIN_PASS echo # Install required packages yum install -y realmd samba-common-tools oddjob oddjob-mkhomedir sssd # Discover and join the Active Directory domain realm discover $DOMAIN realm join $DOMAIN -U $ADMIN_USER --verbose # Enable and start necessary services systemctl enable sssd systemctl start sssd # Display the Active Directory users and groups realm list # Option to add an Active Directory group to the RHEL 8 admins read -p "Do you want to add an Active Directory group to the RHEL 8 admins? (y/n): " ADD_GROUP if [[ $ADD_GROUP == "y" || $ADD_GROUP == "Y" ]]; then read -p "Enter the Active Directory group name: " GROUP_NAME # Add the Active Directory group to sudoers echo "%${GROUP_NAME} ALL=(ALL) ALL" >> /etc/sudoers # Add the Active Directory group to the wheel group usermod -aG wheel "${GROUP_NAME#YOUR_REALM\\}" echo "Group '${GROUP_NAME}' has been added to the RHEL 8 admins." fiIn this updated script, the line
usermod -aG wheel "${GROUP_NAME#YOUR_REALM\\}"adds the Active Directory group (without the domain prefix) to thewheelgroup using theusermodcommand.Make sure to replace the
YOUR_REALMplaceholder with the appropriate Active Directory realm in theusermodline.Again, my apologies for the earlier oversight. Please modify the script as necessary before running it. Let me know if you have any further questions!