I've created a new NodeJS instance on Amazon Lightsail, and wish to connect to it from my Mac's command line. Not sure how to include the required SSH key in the connection command when it says Permission denied (publickey)
.
How do I connect to a new Amazon Lightsail instance from my Mac?
13.3k views Asked by Rohit Falor AtThere are 4 answers
I spent hours figuring out how to add an additional key to login to my Wordpress/Bitnami Lightsail Instance.
I though that by adding new key pairs in [https://lightsail.aws.amazon.com/ls/webapp/account/keys][1], I could get direct access to my instances, but that was not the case. I always got "Permission denied (publickey)" when trying to connect via SSH/SFTP.
How to solve it?
You should add your public keys directly to your Lightsail instance:
Connect first to your instance via your Lightsail console. The link should looks like this: https://lightsail.aws.amazon.com/ls/remote/yourzone/instances/instancename/terminal?protocol=ssh
nano ~/.ssh/authorized_keys
It should looks something like:
ssh-rsa AFGGS#%NzaC1yc2EFDSGgpCvpVhFyRSpfsdfjhgasdDSduD$
This means that only one key par is allowed to connect via SSH/SFTP to your Lightsail instance (voilĂ !).
You should add to that file the new public key bellow:
(YOUR PREVIOUS KEY)ssh-rsa AFGGS#%NzaC1yc2EFDSGgpCvpVhFyRSpfsdfjhgasdDSduD$
(ADD NEW PUBLIC KEY) ssh-rsa ASJKAKKFS#%ASDFbsdjfhJHGJvpVhFyRSpfsdfjhgasdDSduD$
Restart your server and login from your local terminal:
ssh bitnami@yourpublicip -i /Users/youruser/.ssh/yourkeyfile
With this, I could also login via SFTP in Filezilla (Mac), adding the private key in the Site Manager.
To use your existing ~/.ssh/id_rsa.pub
- Login to the server using the browser client
- Do
nano ~/.ssh/authorized_keys
- Get your local public key with
xclip -sel clip < ~/.ssh/id_rsa.pub
- Append the
~/.ssh/authorized_keys
with the copied key - Restart the instance
- Login locally using
ssh ubuntu@[instance_public_ip]
To be able to connect to your amazon lightsail instance you need to download the key first.
Go to Accounts > SSH Keys ( https://lightsail.aws.amazon.com/ls/webapp/account/keys ) > Download (Make sure that you download the key for the same region where your instance is installed.)
Save it in a folder in your local machine. For Example- "Desktop > keys"
Open the terminal and navigate to the directory where the key is stored(
cd desktop/keys
)Enter this command in terminal:
chmod 600 keyfilename.pem
(replace keyfile name with your actual key name e.gchmod 600 LightsailDefaultKey-eu-west-2.pem
)
It is required that your private key files are NOT accessible by others. This is why we have to change the file permissions
- Run this command
ssh -i keyfilename.pem username@ip
replace keyfilename.pem with your actual file name, username with your username(e.g bitnami or user) and replace IP with actual IP
You can find your IP and username on your instance page (https://lightsail.aws.amazon.com/ls/webapp/home/instances > Select Instance> Manage)
LightsailDefaultPrivateKey-us-west-2.pem
chmod 600 [fileName]
at the command line to restrict file permission so only you can read itssh -i [fileName] [username]@[Public IP]
to establish the connection to Lightsail