Skip to content

Instantly share code, notes, and snippets.

@amanjuman
Created August 13, 2021 13:48
Show Gist options
  • Save amanjuman/7a7548f80fb4dc5fb11a89fb389e4ea9 to your computer and use it in GitHub Desktop.
Save amanjuman/7a7548f80fb4dc5fb11a89fb389e4ea9 to your computer and use it in GitHub Desktop.
SSH Hardening
## Remove Existing Host Files
rm /etc/ssh/ssh_host_*
## Regenerate Host Files
ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ""
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
## Enable Host Files
sed -i 's/^HostKey \/etc\/ssh\/ssh_host_\(dsa\|ecdsa\)_key$/\#HostKey \/etc\/ssh\/ssh_host_\1_key/g' /etc/ssh/sshd_config
## Module Update
awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe
mv /etc/ssh/moduli.safe /etc/ssh/moduli
## Key Exchange Update
echo -e "\n# Restrict key exchange, cipher, and MAC algorithms. \n# hardening guide.\nKexAlgorithms curve25519-sha256,[email protected],diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256\nCiphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr\nMACs [email protected],[email protected],[email protected]\nHostKeyAlgorithms ssh-ed25519,[email protected]" >> /etc/ssh/sshd_config
## Restart SSH Service
service ssh restart
Additional Config:
PermitRootLogin no
ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no
AuthenticationMethods publickey
PubkeyAuthentication yes
PermitEmptyPasswords no
ClientAliveInterval 300
ClientAliveCountMax 0
IgnoreRhosts yes
HostbasedAuthentication no
X11Forwarding yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment