FineShot.AI
Vibe to Live · Ep 3.1
•3 min read

Vibe to Live Ep 3.1: SSH Into AWS Lightsail, Step by Step

  1. Ep 1Intro
  2. Ep 2Domain + SSL
  3. Ep 3Server
  4. Ep 3.1SSH · you are here
  5. Ep 4nginx
  6. Ep 5Node + pm2
  7. Ep 6Proxy
  8. Ep 7Git · soon

Watch on YouTube•7:42 video

SSH (Secure Shell) is how you log in to your server and type commands on it. In this episode we log in to our Ubuntu server on AWS Lightsail from the browser and from our own computer, fix the most common SSH error, create our own key, and set up a safer user than ubuntu.

By the end you will have

  • SSH access from the browser and from your own computer
  • A fix for “WARNING: UNPROTECTED PRIVATE KEY FILE”
  • Your own SSH key pair, made with ssh-keygen
  • A separate deploy user that logs in with that key

SSH into Lightsail from the browser

The quickest way in: on the instance page in Lightsail, click Connect using SSH. A terminal opens in your browser, already logged in. Handy, but you’ll want to connect from your own computer for real work.

SSH from your own computer with the .pem key

Use the key pair you downloaded when you created the instance:

ssh -i /path/to/key.pem ubuntu@YOUR_SERVER_IP

-i tells SSH which key file to use, ubuntu is the default user on Lightsail’s Ubuntu image, and the IP is your server’s static IP.

The “UNPROTECTED PRIVATE KEY FILE” error

The first attempt often fails like this:

WARNING: UNPROTECTED PRIVATE KEY FILE!
Permissions 0644 for '/path/to/key.pem' are too open.
...
Permission denied (publickey).

Your downloaded key is readable by other users on your computer (permissions 644), so SSH refuses to use it. That is a safety feature, not a bug.

Fix it with chmod 400

chmod 400 /path/to/key.pem

400 means only you, the owner, can read the file, and nobody can write to it. Run the ssh command again and it logs in.

Create your own SSH key with ssh-keygen

Rather than sharing one Lightsail key, make your own key pair on your computer:

ssh-keygen -t rsa

You get two files in ~/.ssh/:

  • id_rsa: the private key. Keep it secret, never share it.
  • id_rsa.pub: the public key. This is the one you put on the server.

Already have an id_rsa? Pressing Enter at the file prompt overwrites it, so back it up or choose a different name. A passphrase adds extra safety; leave it empty only if you understand the trade-off.

Create a safe user: deploy

Doing everything as the main ubuntu user is risky. On the server, create a new user for deployments and give it sudo rights:

sudo useradd -m -s /bin/bash deploy
sudo usermod -aG sudo deploy

-m creates the home folder and -s /bin/bash sets its shell. If deploy should use sudo, also give it a password with sudo passwd deploy, since sudo asks for one.

Add your public key to authorized_keys

Switch to the new user (for example with sudo su - deploy) and create its SSH folder:

mkdir ~/.ssh && chmod 700 ~/.ssh
vim ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Into authorized_keys, paste the contents of id_rsa.pub from your own computer: one line starting with ssh-rsa. The permissions matter: SSH ignores the file if the folder or the file can be read by others.

Log in with your own SSH key

ssh -i ~/.ssh/id_rsa deploy@YOUR_SERVER_IP

You’re in as deploy, with your own key, and no .pem file needed.

Tip: always test a new login in a second terminal before closing the first one. If something is wrong, you still have a working session to fix it, so you never lock yourself out.

If it doesn’t work

Still “Permission denied (publickey)”. Check you’re using the right user name with the right key: ubuntu with the .pem, deploy with your id_rsa. On the server, ~/.ssh must be 700 and authorized_keys must be 600, both owned by that user.

The connection just times out. Port 22 must be allowed in the Lightsail firewall, and the IP must be the instance’s current public (static) IP.

Related

Next

Logged in and safe. In episode 4 we install nginx and put our website on this server.