UbuntuJoinDomain.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. # no longer used
  9. #echo "Marking linux-image-generic and linux-headers-generic as held back..."
  10. #sudo apt-mark hold linux-image-generic linux-headers-generic
  11. # Update and upgrade the system
  12. echo "Upgrading the system..."
  13. sudo DEBIAN_FRONTEND=noninteractive apt update
  14. sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y
  15. # Install required packages for Active Directory
  16. echo "Installing Active Directory Tools..."
  17. sudo DEBIAN_FRONTEND=noninteractive apt install -y realmd libnss-sss libpam-sss sssd sssd-tools adcli samba-common-bin oddjob oddjob-mkhomedir packagekit
  18. # Install any additional software
  19. # Required packages for NinjaRMM
  20. echo "Installing Ninja Tools..."
  21. sudo DEBIAN_FRONTEND=noninteractive apt install -y net-tools network-manager policycoreutils jq libjq1 libonig5
  22. # Load configuration file
  23. echo "Loading config file..."
  24. source config.sh
  25. # Set the username and password
  26. echo "Enter Active Directory Password..."
  27. ADMINUSER=$ADMIN_USER
  28. #ADMINPASS=$ADMIN_PASSWORD
  29. # Set the hostname
  30. echo "Setting System Hostname..."
  31. sudo hostnamectl set-hostname "$NEW_HOSTNAME"
  32. # Set /etc/host
  33. echo "127.0.0.1 localhost" | sudo tee /etc/hosts
  34. echo "$(hostname -I | cut -d' ' -f1) $HOST $NEW_HOSTNAME" | sudo tee -a /etc/hosts
  35. # Update and install required packages
  36. ##sudo apt update
  37. ##sudo apt install -y realmd libnss-sss libpam-sss sssd sssd-tools adcli samba-common-bin oddjob oddjob-mkhomedir packagekit
  38. # Join the domain
  39. echo "Joining the domain..."
  40. sudo -S realm join --user="$ADMINUSER" --computer-ou="$OU" "$DOMAIN"
  41. #work in progress - --computer-desc="$COMPUTER_DESC"
  42. # Configure PAM to create home directories for domain users on first login
  43. sudo bash -c "cat >> /usr/share/pam-configs/mkhomedir" << EOL
  44. Name: Activate mkhomedir
  45. Default: yes
  46. Priority: 900
  47. Session-Type: Additional
  48. Session:
  49. required pam_mkhomedir.so umask=0077 skel=/etc/skel
  50. EOL
  51. # Enable the mkhomedir PAM module
  52. sudo pam-auth-update --enable mkhomedir
  53. # Set permissions for the home directories
  54. sudo chmod 0700 /home/*
  55. # Configure SSSD
  56. sudo sed -i 's/use_fully_qualified_names = True/use_fully_qualified_names = False/g' /etc/sssd/sssd.conf
  57. # Deny login to all users
  58. echo "Setting Permissions..."
  59. sudo realm deny --all
  60. # Allow login to domain groups
  61. sudo realm permit -g "Domain Admins"
  62. sudo realm permit -g "Access - Admin - All Servers"
  63. sudo realm permit -g "Access - Admin - $HOST"
  64. sudo realm permit -g "Access - Admin - All Linux Servers"
  65. # Set up sudoers file
  66. echo "%Domain\ Admins ALL=(ALL:ALL) ALL" | sudo tee /etc/sudoers.d/LocalAdmins > /dev/null
  67. echo "%Access\ -\ Admin\ -\ All\ Servers ALL=(ALL) ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  68. echo "%Access\ -\ Admin\ -\ All\ Linux\ Servers ALL=(ALL) ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  69. echo "%Access\ -\ Admin\ -\ $HOST ALL=(ALL) ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  70. echo "Ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  71. # Set Timezone to Eastern
  72. echo "Seting the Timezone..."
  73. sudo timedatectl set-timezone America/New_York
  74. # copy file from SCP server to local system
  75. ## sudo scp -r anonymous@ptiwa001:ninja-agent.deb /home/coadmin/
  76. ## curl -o ninja-agent.deb https://app.ninjarmm.com/agent/installer/e99519fc-b76e-4c50-b5ff-ef68db95b032/serverslinuxmainoffice-5.6.7925-installer-x86-64.deb
  77. # Install Ninja RMM Agent
  78. echo "Installing Ninja..."
  79. # Step 1: Get OAuth token
  80. oauth_response=$(curl --request POST \
  81. --url https://app.ninjarmm.com/ws/oauth/token \
  82. --header 'Content-Type: application/x-www-form-urlencoded' \
  83. --data grant_type=client_credentials \
  84. --data client_id=Exx51oPGv_hZ8fgQh2IqtpX5VpA \
  85. --data client_secret=AAv3WeKnUjSOSRuW94plKdrihEw8AEZCJd38VK56aSGjAa4BAtm_zw \
  86. --data scope=management)
  87. # Extract access token from response
  88. access_token=$(echo "$oauth_response" | jq -r '.access_token')
  89. # Step 2: Generate installer URL
  90. installer_response=$(curl -X 'GET' \
  91. 'https://app.ninjarmm.com/v2/organization/26/location/45/installer/LINUX_DEB' \
  92. -H 'accept: application/json' \
  93. -H "Authorization: Bearer $access_token")
  94. # Extract installer URL from response
  95. installer_url=$(echo "$installer_response" | jq -r '.url')
  96. # Step 3: Download installer using generated URL
  97. curl -o ninja-agent.deb "$installer_url"
  98. # install Ninja
  99. sudo dpkg -i ninja-agent.deb
  100. # End Install Ninja RMM Agent
  101. # copy file from SCP server to local system
  102. #sudo scp -r anonymous@ptiwa001:falcon-sensor_6.46.0-14306.deb /home/coadmin/
  103. # Install CrowdStrike Falcon
  104. echo "Installing Falcon Sensor..."
  105. export FALCON_CLIENT_ID="58f632f6b11c43f48864c9043ec8428d"
  106. export FALCON_CLIENT_SECRET="8VrQZaLfAHgJNpED0627tyeu9oGbcwWP51mxYk34"
  107. export FALCON_CID="D0511099B3FF494D8B87F48C4AB90201-56"
  108. export FALCON_SENSOR_VERSION_DECREMENT="1"
  109. curl -L https://raw.githubusercontent.com/CrowdStrike/falcon-scripts/main/bash/install/falcon-linux-install.sh | bash
  110. # End Install CrowdStrike Falcon
  111. # install package using dpkg
  112. # sudo dpkg -i falcon-sensor_6.46.0-14306.deb
  113. # Run the additional command
  114. # sudo /opt/CrowdStrike/falconctl -s --cid=D0511099B3FF494D8B87F48C4AB90201-56
  115. # Remove packages that are not required
  116. echo "Removing old packages..."
  117. sudo apt autoremove -y
  118. # Configure syslog server
  119. echo "Configuring syslog server..."
  120. echo 'remote host is: dcpi-siem 10.200.24.51:514' | sudo tee -a /etc/rsyslog.conf
  121. echo '*.* @@10.200.24.51:514' | sudo tee -a /etc/rsyslog.conf
  122. # Start/Restart Services
  123. echo "Restarting system services..."
  124. sudo systemctl restart sssd
  125. sudo systemctl start ninjarmm-agent.service
  126. sudo systemctl start falcon-sensor
  127. sudo systemctl restart rsyslog
  128. # check the status of the services
  129. echo "Checking status of required services..."
  130. sudo systemctl | grep -E 'falcon-sensor|ninjarmm-agent|sssd.service|rsyslog.service'
  131. echo "$line"
  132. echo ""
  133. # Query user and print message
  134. echo "Checking Active Directory..."
  135. id ansible && echo "Successfully queried Active Directory for user peo\ansible"
  136. echo "$line"
  137. echo ""
  138. # Check if reboot is required
  139. echo "Is a reboot required?..."
  140. if [ -f /var/run/reboot-required ]; then
  141. echo -e "\033[31mA reboot is required.\033[0m"
  142. else
  143. echo -e "\033[32mReboot not required.\033[0m"
  144. fi