UbuntuJoinDomain.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. # Install any additional software
  17. # Required packages for Ninja
  18. # sudo DEBIAN_FRONTEND=noninteractive apt install -y net-tools network-manager policycoreutils
  19. # Load configuration file
  20. source config.sh
  21. # Set the username and password
  22. ADMINUSER=$ADMIN_USER
  23. #ADMINPASS=$ADMIN_PASSWORD
  24. # Set the hostname
  25. sudo hostnamectl set-hostname "$NEW_HOSTNAME"
  26. # Set /etc/host
  27. echo "127.0.0.1 localhost" | sudo tee /etc/hosts
  28. echo "$(hostname -I | cut -d' ' -f1) $HOST $NEW_HOSTNAME" | sudo tee -a /etc/hosts
  29. # Update and install required packages
  30. #sudo apt update
  31. #sudo apt install -y realmd libnss-sss libpam-sss sssd sssd-tools adcli samba-common-bin oddjob oddjob-mkhomedir packagekit
  32. # Join the domain
  33. #echo "$ADMINPASS" | sudo -S realm join --user="$ADMINUSER" --computer-ou="$OU" "$DOMAIN"
  34. sudo -S realm join --user="$ADMINUSER" --computer-ou="$OU" "$DOMAIN"
  35. #work in progress - --computer-desc="$COMPUTER_DESC"
  36. # Configure PAM to create home directories for domain users on first login
  37. sudo bash -c "cat >> /usr/share/pam-configs/mkhomedir" << EOL
  38. Name: Activate mkhomedir
  39. Default: yes
  40. Priority: 900
  41. Session-Type: Additional
  42. Session:
  43. required pam_mkhomedir.so umask=0077 skel=/etc/skel
  44. EOL
  45. # Enable the mkhomedir PAM module
  46. sudo pam-auth-update --enable mkhomedir
  47. # Set permissions for the home directories
  48. sudo chmod 0700 /home/*
  49. # Configure SSSD
  50. sudo sed -i 's/use_fully_qualified_names = True/use_fully_qualified_names = False/g' /etc/sssd/sssd.conf
  51. # Deny login to all users
  52. sudo realm deny --all
  53. # Allow login to domain groups
  54. sudo realm permit -g "Domain Admins"
  55. # sudo realm permit -g "Access - Admin - All Servers"
  56. # sudo realm permit -g "Access - Admin - $HOST"
  57. # Set up sudoers file
  58. echo "%Domain\ Admins ALL=(ALL:ALL) ALL" | sudo tee /etc/sudoers.d/LocalAdmins > /dev/null
  59. # echo "%Access\ -\ Admin\ -\ All\ Servers ALL=(ALL) ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  60. # echo "%Access\ -\ Admin\ -\ $HOST ALL=(ALL) ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  61. # copy file from SCP server to local system
  62. # sudo scp -r anonymous@ptiwa001:ninja-agent.deb ~/
  63. # install package using dpkg
  64. # sudo dpkg -i ninja-agent.deb
  65. # copy file from SCP server to local system
  66. # sudo scp -r anonymous@ptiwa001:falcon-sensor_6.46.0-14306.deb ~/
  67. # install package using dpkg
  68. # sudo dpkg -i falcon-sensor_6.46.0-14306.deb
  69. # Run the additional command
  70. # sudo /opt/CrowdStrike/falconctl -s --cid=D0511099B3FF494D8B87F48C4AB90201-56
  71. # Remove packages that are not required
  72. sudo apt autoremove -y
  73. # Start Services
  74. sudo systemctl restart sssd
  75. # sudo systemctl start ninjarmm-agent.service
  76. # sudo systemctl start falcon-sensor
  77. # check the status of the services
  78. # sudo systemctl | grep -E 'falcon-sensor|ninjarmm-agent|sssd.service'
  79. # Query user and print message
  80. id chagood && echo "Successfully queried Active Directory for user chagood"
  81. # Check if reboot is required
  82. if [ -f /var/run/reboot-required ]; then
  83. echo -e "\033[31mA reboot is required.\033[0m"
  84. else
  85. echo -e "\033[32mReboot not required.\033[0m"
  86. fi