UbuntuJoinDomain.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. echo "Joining the domain..."
  35. sudo -S realm join --user="$ADMINUSER" --computer-ou="$OU" "$DOMAIN"
  36. #work in progress - --computer-desc="$COMPUTER_DESC"
  37. # Configure PAM to create home directories for domain users on first login
  38. sudo bash -c "cat >> /usr/share/pam-configs/mkhomedir" << EOL
  39. Name: Activate mkhomedir
  40. Default: yes
  41. Priority: 900
  42. Session-Type: Additional
  43. Session:
  44. required pam_mkhomedir.so umask=0077 skel=/etc/skel
  45. EOL
  46. # Enable the mkhomedir PAM module
  47. sudo pam-auth-update --enable mkhomedir
  48. # Set permissions for the home directories
  49. sudo chmod 0700 /home/*
  50. # Configure SSSD
  51. sudo sed -i 's/use_fully_qualified_names = True/use_fully_qualified_names = False/g' /etc/sssd/sssd.conf
  52. # Deny login to all users
  53. sudo realm deny --all
  54. # Allow login to domain groups
  55. sudo realm permit -g "Domain Admins"
  56. sudo realm permit -g "Access - Admin - All Servers"
  57. sudo realm permit -g "Access - Admin - $HOST"
  58. sudo realm permit -g "Access - Admin - All Linux Servers"
  59. # Set up sudoers file
  60. echo "%Domain\ Admins ALL=(ALL:ALL) ALL" | sudo tee /etc/sudoers.d/LocalAdmins > /dev/null
  61. echo "%Access\ -\ Admin\ -\ All\ Servers ALL=(ALL) ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  62. echo "%Access\ -\ Admin\ -\ All\ Linux\ Servers ALL=(ALL) ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  63. echo "%Access\ -\ Admin\ -\ $HOST ALL=(ALL) ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  64. echo "Ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers.d/LocalAdmins > /dev/null
  65. # Set Timezone to Eastern
  66. echo "Seting the Timezone..."
  67. sudo timedatectl set-timezone America/New_York
  68. # copy file from SCP server to local system
  69. # sudo scp -r anonymous@ptiwa001:ninja-agent.deb /home/coadmin/
  70. # curl -o ninja-agent.deb https://app.ninjarmm.com/agent/installer/e99519fc-b76e-4c50-b5ff-ef68db95b032/serverslinuxmainoffice-5.6.7925-installer-x86-64.deb
  71. echo "Installing Ninja..."
  72. # Step 1: Get OAuth token
  73. oauth_response=$(curl --request POST \
  74. --url https://app.ninjarmm.com/ws/oauth/token \
  75. --header 'Content-Type: application/x-www-form-urlencoded' \
  76. --data grant_type=client_credentials \
  77. --data client_id=Exx51oPGv_hZ8fgQh2IqtpX5VpA \
  78. --data client_secret=AAv3WeKnUjSOSRuW94plKdrihEw8AEZCJd38VK56aSGjAa4BAtm_zw \
  79. --data scope=management)
  80. # Extract access token from response
  81. access_token=$(echo "$oauth_response" | jq -r '.access_token')
  82. # Step 2: Generate installer URL
  83. installer_response=$(curl -X 'GET' \
  84. 'https://app.ninjarmm.com/v2/organization/26/location/45/installer/LINUX_RPM' \
  85. -H 'accept: application/json' \
  86. -H "Authorization: Bearer $access_token")
  87. # Extract installer URL from response
  88. installer_url=$(echo "$installer_response" | jq -r '.url')
  89. # Step 3: Download installer using generated URL
  90. curl -o ninja-agent.deb "$installer_url"
  91. # install Ninja
  92. sudo dpkg -i ninja-agent.deb
  93. # copy file from SCP server to local system
  94. #sudo scp -r anonymous@ptiwa001:falcon-sensor_6.46.0-14306.deb /home/coadmin/
  95. # Install CrowdStrike Falcon
  96. echo "Installing Falcon Sensor..."
  97. sudo export FALCON_CLIENT_ID="58f632f6b11c43f48864c9043ec8428d"
  98. sudo export FALCON_CLIENT_SECRET="8VrQZaLfAHgJNpED0627tyeu9oGbcwWP51mxYk34"
  99. sudo export FALCON_CID="D0511099B3FF494D8B87F48C4AB90201-56"
  100. sudo export FALCON_SENSOR_VERSION_DECREMENT="1"
  101. curl -L https://raw.githubusercontent.com/crowdstrike/falcon-linux-install-bash/main/falcon-linux-deploy.sh | sudo bash
  102. # install package using dpkg
  103. # sudo dpkg -i falcon-sensor_6.46.0-14306.deb
  104. # Run the additional command
  105. # sudo /opt/CrowdStrike/falconctl -s --cid=D0511099B3FF494D8B87F48C4AB90201-56
  106. # Remove packages that are not required
  107. echo "Removing old packages..."
  108. sudo apt autoremove -y
  109. # Configure syslog server
  110. echo "Configuring syslog server..."
  111. echo 'remote host is: dcpi-siem 10.200.24.51:514' | sudo tee -a /etc/rsyslog.conf
  112. echo '*.* @@10.200.24.51:514' | sudo tee -a /etc/rsyslog.conf
  113. # Start/Restart Services
  114. echo "Restarting system services..."
  115. sudo systemctl restart sssd
  116. sudo systemctl start ninjarmm-agent.service
  117. sudo systemctl start falcon-sensor
  118. sudo systemctl restart rsyslog
  119. # check the status of the services
  120. sudo systemctl | grep -E 'falcon-sensor|ninjarmm-agent|sssd.service'
  121. # Query user and print message
  122. id chagood && echo "Successfully queried Active Directory for user chagood"
  123. # Check if reboot is required
  124. if [ -f /var/run/reboot-required ]; then
  125. echo -e "\033[31mA reboot is required.\033[0m"
  126. else
  127. echo -e "\033[32mReboot not required.\033[0m"
  128. fi