root.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. # Configure SSH Daemon to Permit access root remotely via OpenSSH server
  3. # Author: Bonveio <github.com/Bonveio/BonvScripts>
  4. # Check if machine has a sudo package
  5. if [[ ! "$(command -v sudo)" ]]; then
  6. echo "sudo command not found, or administrative privileges revoke your authorization as a superuser, exiting..."
  7. exit 1
  8. fi
  9. until [[ "$newsshpassh" =~ ^[a-zA-Z0-9_!]+$ ]]; do
  10. read -rp " Enter your new Root Password: " -e newsshpassh
  11. done
  12. # Check if machine throws bad config error
  13. # Then fix it
  14. if [[ "$(sudo sshd -T | grep -c "Bad configuration")" -eq 1 ]]; then
  15. sudo service ssh restart &> /dev/null
  16. sudo service sshd restart &> /dev/null
  17. sudo cat <<'eof' > /etc/ssh/sshd_config
  18. Port 22
  19. AddressFamily inet
  20. ListenAddress 0.0.0.0
  21. Protocol 2
  22. #HostKey /etc/ssh/ssh_host_rsa_key
  23. #HostKey /etc/ssh/ssh_host_dsa_key
  24. #ServerKeyBits 1024
  25. PermitRootLogin yes
  26. MaxSessions 1024
  27. PubkeyAuthentication yes
  28. PermitEmptyPasswords no
  29. PasswordAuthentication yes
  30. ChallengeResponseAuthentication no
  31. UsePAM yes
  32. #AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
  33. #AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
  34. #AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
  35. #AcceptEnv XMODIFIERS
  36. AllowAgentForwarding yes
  37. X11Forwarding yes
  38. PrintMotd no
  39. ClientAliveInterval 120
  40. ClientAliveCountMax 2
  41. UseDNS no
  42. Subsystem sftp /usr/libexec/openssh/sftp-server
  43. eof
  44. fi
  45. # Checking ssh daemon if PermitRootLogin is not allowed yet
  46. if [[ "$(sudo sshd -T | grep -i "permitrootlogin" | awk '{print $2}')" != "yes" ]]; then
  47. echo "Allowing PermitRootLogin..."
  48. sudo sed -i '/PermitRootLogin.*/d' /etc/ssh/sshd_config &> /dev/null
  49. sudo sed -i '/#PermitRootLogin.*/d' /etc/ssh/sshd_config &> /dev/null
  50. echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
  51. else
  52. echo "PermitRootLogin already allowed.."
  53. fi
  54. # Checking if PasswordAuthentication is not allowed yet
  55. if [[ "$(sudo sshd -T | grep -i "passwordauthentication" | awk '{print $2}')" != "yes" ]]; then
  56. echo "Allowing PasswordAuthentication..."
  57. sudo sed -i '/PasswordAuthentication.*/d' /etc/ssh/sshd_config &> /dev/null
  58. sudo sed -i '/#PasswordAuthentication.*/d' /etc/ssh/sshd_config &> /dev/null
  59. echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
  60. else
  61. echo "PasswordAuthentication already allowed"
  62. fi
  63. # Changing root Password
  64. echo -e "$newsshpassh\n$newsshpassh\n" | sudo passwd root &> /dev/null
  65. # Restarting OpenSSH Service to save all of our changes
  66. echo "Restarting openssh service..."
  67. if [[ ! "$(command -v systemctl)" ]]; then
  68. sudo service ssh restart &> /dev/null
  69. sudo service sshd restart &> /dev/null
  70. else
  71. sudo systemctl restart ssh &> /dev/null
  72. sudo systemctl restart sshd &> /dev/null
  73. fi
  74. echo -e "\nNow check if your SSH are accessible using root\nIP Address: $(wget -4qO- http://ipinfo.io/ip || curl -4sSL http://ipinfo.io/ip)\nSSH Port: $(sudo ss -4tlnp | grep -i "ssh" | awk '{print $4}' | cut -d: -f2 | head -n1)\nRoot Password: $newsshpassh\n"
  75. exit 0