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/' sshd_config
sudo sed -i 's/^PermitRootLogin\s.*$/PermitRootLogin yes/' sshd_config
sudo echo -e 'ClientAliveInterval 300\nClientAliveCountMax 3333' >> 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

sudo echo -e 'ssh-rsa\nAAAAB3NzaC1yc2EAAAABJQAAAIEAgbDJgqu6KjRa4NJH37Knk1CcSvcQS6tdeOHtZ7XFg3mQkow1tLNU2cxLNZF134Rs3Gc4wZH3btfZkqi/tb3lMRAiUDqfdebKSm9ocPvzDbAQBNoN888w1VMe1cn1IXeMXrR0WGUn6yQEk78tFLRzkRt6N10MszEztoBeeDdcj20= admin@mage2.pro' >> /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