Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active November 26, 2022 15:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/ed6c8b33f85631cd6148b8db8bb18ac1 to your computer and use it in GitHub Desktop.
Save miguelmota/ed6c8b33f85631cd6148b8db8bb18ac1 to your computer and use it in GitHub Desktop.
Ubuntu EC2 secure SSH server

Create new user

sudo useradd -m -s /bin/bash alice
sudo passwd alice
sudo usermod -aG sudo alice

Copy authorized users to new user

su - alice
sudo cp -r /home/ubuntu/.ssh .ssh
sudo chown -R alice:alice .ssh

Delete default user

sudo deluser --remove-home ubuntu

Disable ssh root login

sudo vim /etc/ssh/sshd_config
sudo sshd -t
sudo service ssh reload
PasswordAuthentication no
ChallengeResponseAuthentication no
PermitRootLogin no
PermitEmptyPasswords no

Disable root account

sudo passwd -l root

Install fail2ban

sudo apt update
sudo apt install fail2ban -y
sudo vim /etc/fail2ban/jail.conf
sudo service fail2ban restart
sudo service fail2ban status
ignoreip = 127.0.0.1/8 ::1

[sshd]
enabled = true

Add 2fa

https://gist.github.com/miguelmota/9b12335f508481a6cdd642444b3b9b1c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment