UbuntuJoinDomain.sh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. set -e
  3. # run dpkg-reconfigure unattended-upgrades and answer no unattended
  4. echo "Running dpkg-reconfigure unattended-upgrades..."
  5. echo "unattended-upgrades unattended-upgrades/enable_auto_updates boolean false" | sudo debconf-set-selections
  6. sudo dpkg-reconfigure -f noninteractive unattended-upgrades
  7. # mark linux-image-generic and linux-headers-generic as held back
  8. # echo "Marking linux-image-generic and linux-headers-generic as held back..."
  9. # sudo apt-mark hold linux-image-generic linux-headers-generic
  10. # upgrade the system
  11. echo "Upgrading the system..."
  12. sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y
  13. # Update and install required packages for Active Directory
  14. sudo DEBIAN_FRONTEND=noninteractive apt update
  15. sudo DEBIAN_FRONTEND=noninteractive apt install -y realmd libnss-sss libpam-sss sssd sssd-tools adcli samba-common-bin oddjob oddjob-mkhomedir packagekit
  16. # Load configuration file
  17. source config.sh
  18. # Set the username and password
  19. ADMINUSER=$ADMIN_USER
  20. #ADMINPASS=$ADMIN_PASSWORD
  21. # Set the hostname
  22. sudo hostnamectl set-hostname "$NEW_HOSTNAME"
  23. # Set /etc/host
  24. echo "127.0.0.1 localhost" | sudo tee /etc/hosts
  25. echo "$(hostname -I | cut -d' ' -f1) $HOST $NEW_HOSTNAME" | sudo tee -a /etc/hosts
  26. # Join the domain
  27. sudo -S realm join --user="$ADMINUSER" --computer-ou="$OU" "$DOMAIN"
  28. #work in progress - --computer-desc="$COMPUTER_DESC"
  29. # Configure PAM to create home directories for domain users on first login
  30. sudo bash -c "cat >> /usr/share/pam-configs/mkhomedir" << EOL
  31. Name: Activate mkhomedir
  32. Default: yes
  33. Priority: 900
  34. Session-Type: Additional
  35. Session:
  36. required pam_mkhomedir.so umask=0077 skel=/etc/skel
  37. EOL
  38. # Enable the mkhomedir PAM module
  39. sudo pam-auth-update --enable mkhomedir
  40. # Set permissions for the home directories
  41. sudo chmod 0700 /home/*
  42. # Configure SSSD
  43. sudo sed -i 's/use_fully_qualified_names = True/use_fully_qualified_names = False/g' /etc/sssd/sssd.conf
  44. # Deny login to all users
  45. sudo realm deny --all
  46. # Allow login to domain groups
  47. sudo realm permit -g "Domain Admins"
  48. sudo realm permit -g "AnsibleAdmins"
  49. # Set up sudoers file
  50. echo "%Domain\ Admins ALL=(ALL:ALL) ALL" | sudo tee /etc/sudoers.d/LocalAdmins > /dev/null
  51. echo "%AnisbleAdmins ALL=(ALL) ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  52. echo "ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  53. # Remove packages that are not required
  54. sudo apt autoremove -y
  55. # Start Services
  56. sudo systemctl restart sssd
  57. # check the status of the services
  58. sudo systemctl | grep -E 'sssd.service'
  59. # Query user and print message
  60. id chagood && echo "Successfully queried Active Directory for user chagood"
  61. # Check if reboot is required
  62. if [ -f /var/run/reboot-required ]; then
  63. echo -e "\033[31mA reboot is required.\033[0m"
  64. else
  65. echo -e "\033[32mReboot not required.\033[0m"
  66. fi