Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Hello, I was wondering if you can help me?

  1. User

    Hello, I was wondering if you can help me?

  2. Assistant

    Of course! I'll do my best to assist you. What do you need help with?

  3. User

    I need a simple BASH script for joining RHEL 8 systems to an Active Directory Domain. Can you provide an example please?

  4. 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 list
    

    Before running the script, make sure to replace the YOUR_REALM and your-domain.com placeholders 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 command chmod +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!

  5. 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?

  6. 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."
    fi
    

    In 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_REALM and your-domain.com placeholders 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!

  7. 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?

  8. Assistant

    Apologies for the confusion. When adding an Active Directory group to the sudoers file, 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_DOM with the appropriate domain (e.g., ACME) and group-name with 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.com and you want to add the group named admins to the RHEL 8 admins, the line should be:

    GROUP_NAME="ACME\admins"
    

    Make sure to update the GROUP_NAME variable in the script accordingly.

  9. 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"

  10. Assistant

    Apologies for the oversight. Yes, you are correct. In addition to adding the Active Directory group to the sudoers file, it is also advisable to add the group to the wheel group, which typically grants administrative privileges. Here's an updated version of the script that includes adding the Active Directory group to both the sudoers file and the wheel group:

    #!/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."
    fi
    

    In this updated script, the line usermod -aG wheel "${GROUP_NAME#YOUR_REALM\\}" adds the Active Directory group (without the domain prefix) to the wheel group using the usermod command.

    Make sure to replace the YOUR_REALM placeholder with the appropriate Active Directory realm in the usermod line.

    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!