So, you're looking to integrate your CentOS 7 system with a Windows Active Directory domain? Awesome! This is a common task in many enterprise environments, and it allows you to manage your Linux systems using the same centralized authentication and authorization mechanisms as your Windows machines. Don't worry, it might sound a bit intimidating, but I'm here to guide you through the whole process step by step. Let's dive in and get your CentOS 7 box playing nicely with your Windows domain!

    Prerequisites

    Before we get started, let's make sure we have all the necessary bits and pieces in place. Think of it like gathering your ingredients before you start cooking up a storm in the kitchen. Here's what you'll need:

    • A Running CentOS 7 System: Obviously, you'll need a CentOS 7 system that's up and running. This can be a physical machine or a virtual machine, whichever you prefer.
    • Network Connectivity: Your CentOS 7 system needs to be able to communicate with your Windows domain controllers. Make sure it's on the same network or has the necessary routing configured.
    • A Windows Active Directory Domain: You'll need a functioning Windows Active Directory domain. This includes at least one domain controller.
    • Administrative Privileges: You'll need administrative privileges on both your CentOS 7 system (root access) and your Windows domain (a domain administrator account).
    • Basic Linux Knowledge: A little familiarity with the Linux command line will go a long way. Don't worry, I'll walk you through the commands, but understanding what they do will be helpful.

    With these prerequisites in place, you're well on your way to successfully joining your CentOS 7 system to your Windows domain. Now, let's move on to the actual steps involved in the process.

    Step 1: Setting up the Hostname and DNS

    Alright, the first thing we need to do is make sure your CentOS 7 system has a properly configured hostname and DNS settings. This is crucial for the system to be able to identify itself on the network and resolve the names of your domain controllers. Think of it as giving your system a name and address so it can be found in the network neighborhood.

    Setting the Hostname

    Let's start by setting the hostname. The hostname should be a fully qualified domain name (FQDN) that includes the domain name. For example, if your domain is example.com, a good hostname for your CentOS 7 system would be centos7.example.com. To set the hostname, use the hostnamectl command:

    sudo hostnamectl set-hostname centos7.example.com
    

    Replace centos7.example.com with your desired hostname. After running this command, it's a good idea to reboot your system to ensure the hostname change is applied correctly. You can reboot using the reboot command:

    sudo reboot
    

    After the reboot, you can verify the hostname by running:

    hostnamectl
    

    This should display the hostname you set.

    Configuring DNS

    Next, we need to configure DNS so your CentOS 7 system can resolve the names of your domain controllers. To do this, you'll need to edit the /etc/resolv.conf file. However, this file is often managed by NetworkManager, so it's best to modify the network configuration file instead. First, identify the network interface you're using. You can use the ip addr command to list the network interfaces:

    ip addr
    

    Look for the interface that has an IP address assigned to it, such as eth0 or ens33. Once you've identified the interface, edit its configuration file. The configuration files are located in the /etc/sysconfig/network-scripts/ directory. The file name will be something like ifcfg-eth0 or ifcfg-ens33, depending on the interface name. Use your favorite text editor (like vi or nano) to edit the file:

    sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
    

    Add the following lines to the file, replacing your_domain_controller_ip with the IP address of your domain controller:

    DNS1=your_domain_controller_ip
    DNS2=8.8.8.8
    DOMAIN=example.com
    SEARCH=example.com
    

    The DNS1 line specifies the IP address of your primary DNS server (your domain controller). The DNS2 line specifies a secondary DNS server (Google's public DNS server in this example, but you can use any other DNS server you prefer). The DOMAIN and SEARCH lines specify the domain name. Save the file and exit the text editor.

    Next, restart the network service to apply the changes:

    sudo systemctl restart network
    

    Finally, verify that DNS resolution is working by using the ping command to ping your domain controller by its name:

    ping your_domain_controller_name.example.com
    

    If the ping is successful, it means your CentOS 7 system can resolve the name of your domain controller. If not, double-check your DNS settings and make sure everything is configured correctly.

    Step 2: Installing Required Packages

    Okay, now that we've got the hostname and DNS sorted out, it's time to install the necessary packages. These packages will provide the tools and libraries we need to communicate with the Windows domain. Think of it as gathering the right tools for the job – you wouldn't try to hammer a nail with a screwdriver, would you?

    We'll be using realmd, sssd, krb5-workstation, oddjob, and oddjob-mkhomedir. These packages are essential for joining the domain and managing user authentication. To install them, use the yum package manager:

    sudo yum install realmd sssd krb5-workstation oddjob oddjob-mkhomedir -y
    

    The -y option tells yum to automatically answer "yes" to any prompts, so the installation process will proceed without interruption. After the packages are installed, we'll need to configure them. But don't worry, realmd will handle most of the configuration for us.

    Step 3: Discovering and Joining the Domain

    Alright, with the packages installed, we're ready to discover and join the domain! This is where the magic happens. realmd makes this process relatively straightforward. First, let's discover the domain using the realm discover command:

    sudo realm discover example.com
    

    Replace example.com with your actual domain name. This command will query the DNS for information about your domain and display the domain's details, such as the domain name, realm name, and domain controllers. If the command fails to discover the domain, double-check your DNS settings and make sure your CentOS 7 system can resolve the name of your domain controller.

    Once you've successfully discovered the domain, you can join it using the realm join command:

    sudo realm join example.com -U administrator
    

    Replace example.com with your actual domain name and administrator with the username of a domain administrator account. You'll be prompted for the password of the administrator account. Enter the password, and realmd will attempt to join the domain. If the command is successful, you'll see a message indicating that the system has been joined to the domain.

    If you encounter any errors during the joining process, double-check your network settings, DNS configuration, and the credentials you're using. Make sure the domain administrator account has the necessary permissions to add computers to the domain.

    Step 4: Configuring SSSD

    Now that we've joined the domain, we need to configure SSSD (System Security Services Daemon) to handle user authentication and authorization. SSSD is a system service that provides access to identity and authentication resources. realmd usually configures SSSD automatically when you join the domain, but it's a good idea to verify the configuration and make any necessary adjustments.

    The main SSSD configuration file is located at /etc/sssd/sssd.conf. Open this file with your favorite text editor:

    sudo vi /etc/sssd/sssd.conf
    

    Make sure the file contains the following sections, replacing example.com with your actual domain name:

    [sssd]
    config_file_version = 2
    services = nss, pam
    domains = example.com
    
    [domain/example.com]
    krb5_realm = EXAMPLE.COM
    ad_domain = example.com
    use_fully_qualified_names = True
    fallback_homedir = /home/%d/%u
    access_provider = ad
    

    The krb5_realm parameter specifies the Kerberos realm name (which is usually the same as the domain name but in uppercase). The ad_domain parameter specifies the Active Directory domain name. The use_fully_qualified_names parameter tells SSSD to use fully qualified usernames (e.g., user@example.com). The fallback_homedir parameter specifies the default home directory for domain users. The access_provider parameter specifies that we're using Active Directory for access control.

    After making any changes to the sssd.conf file, restart the SSSD service:

    sudo systemctl restart sssd
    

    Step 5: Configuring PAM

    PAM (Pluggable Authentication Modules) is a system for plugging in different authentication methods into applications. We need to configure PAM to allow users from the Windows domain to log in to the CentOS 7 system. realmd usually configures PAM automatically, but it's a good idea to verify the configuration.

    The PAM configuration files are located in the /etc/pam.d/ directory. The files we're interested in are system-auth and password-auth. These files are usually symbolic links to a common configuration file. Open the system-auth file with your favorite text editor:

    sudo vi /etc/pam.d/system-auth
    

    Make sure the file contains the following lines:

    auth        sufficient    pam_sss.so use_first_pass
    account     required      pam_sss.so
    password    sufficient    pam_sss.so use_authtok
    session     required      pam_sss.so
    

    These lines tell PAM to use SSSD for authentication, account management, password management, and session management. If these lines are missing, add them to the file. Save the file and exit the text editor.

    Repeat the same process for the password-auth file:

    sudo vi /etc/pam.d/password-auth
    

    Make sure it contains the same lines as the system-auth file. Save the file and exit the text editor.

    Step 6: Testing the Configuration

    Alright, we've configured everything, and it's time to test the configuration! Let's try logging in as a domain user. You can use the su command to switch to a domain user:

    su - domain_user@example.com
    

    Replace domain_user with the username of a domain user and example.com with your domain name. You'll be prompted for the user's password. Enter the password, and if everything is configured correctly, you'll be logged in as the domain user. You can also try logging in via SSH using a domain user account.

    If you encounter any issues, double-check your SSSD and PAM configurations. Make sure the domain user account is enabled and has the necessary permissions to log in to the CentOS 7 system.

    Step 7: Enabling Automatic Home Directory Creation (Optional)

    By default, SSSD doesn't automatically create home directories for domain users when they log in for the first time. If you want to enable automatic home directory creation, you can use the oddjob-mkhomedir package we installed earlier. To enable automatic home directory creation, edit the /etc/pam.d/system-auth and /etc/pam.d/password-auth files and add the following line to both files:

    session     required      pam_mkhomedir.so skel=/etc/skel umask=0077
    

    This line should be placed after the session required pam_sss.so line. Save the files and exit the text editor. After making these changes, restart the SSSD service:

    sudo systemctl restart sssd
    

    Now, when a domain user logs in for the first time, a home directory will be automatically created for them.

    Conclusion

    And there you have it! You've successfully joined your CentOS 7 system to a Windows Active Directory domain. Now you can manage your Linux systems using the same centralized authentication and authorization mechanisms as your Windows machines. This simplifies user management, improves security, and makes your life as a system administrator a whole lot easier. If you encounter any issues along the way, don't hesitate to consult the documentation for realmd, sssd, and PAM. Good luck, and happy system administering!