SSH to the Remote AWS Instance and Security Copy Files
1. SSH to the instance
The AWS console always ask us to login to the instance by using a key pair file (*.pem file, Privacy Enchanced Mail)
. Make sure you have downloaded the key file on the last step of launching an instance.
1.1 SSH on Mac
Open a terminal, and execute:1
ssh -i /path/of/you/key/file/key_file_name.pem username -of-the-instance
In this case,
if we have a key file named: 15619.pem
, and we put it under: /Users/15619/Demo/
; and we have an instance on ec2-1-5-6-19.compute-1.amazonaws.com
within username: ec2-user
,
then, we will run the command like this:1
ssh -i /Users/15619/Demo/15619.pem ec2-user@ec2-1-5-6-19.compute-1.amazonaws.com
Sometimes it will return a warning message that:
bad permissions: ignore key: /Users/15619/Demo/15619.pem
Permission denied (publickey).
In this case, you need to check and change the access permissions to the key pair file:1
chmod 600 /Users/15619/Demo/15619.pem
Now re-run the ssh command and you can login to the instance successfully.
1.2 SSH on Windows
On Windows I use PuTTY to ssh to the remote instance. Once you install it on you PC you need to generate a ppk file (PuTTY Private Key)
using PuTTYgen and the pem file.
First, lauch PuTTYgen:
Then, load the pem file to PuTTYgen:
Click the button Save private key
, then save it to you PC:
Now you are ready to ssh to the instance.
Launch PuTTY, fill the Host Name using the username and instance dns:1
ec2-user@ec2-1-5-6-19.compute-1.amazonaws.com
On the left side-panel, choose: Connection->SSH->Auth, click Browse… and select your ppk file,
Then click Open and you can login to the instance successfully.
2. Secure copy
Similarly, we use key file (pem/ppk file)
as the identifying flag when we want to transfer file between the remote AWS instance and the local machine, especially, if we want to transfer a folder we need to add a recusive flag -r.
2.1 scp on Mac
If you are under Mac environment, the command is:1
scp -i /path/of/you/key/file/key_file_name.pem path/of/the/source/directory/file_name path/of/the/destination/directory/file_name
Now you want to copy a file named Hello.java
under /Users/15619/Demo/
from your local device to the remote instance under /Java/Demo/
, the command is:1
scp -i /Users/15619/Demo/15619.pem /Users/15619/Demo/Hello.java ec2-user@ec2-1-5-6-19.compute-1.amazonaws.com:/Java/Demo/Hello.java
Conversely, if you want to copy a file named Demo.java
under /Java/Demo/
from the remote instance to you local device under /Users/15619/Demo/
, the command is:1
scp -i /Users/15619/Demo/15619.pem ec2-user@ec2-1-5-6-19.compute-1.amazonaws.com:/Java/Demo/Demo.java /Users/15619/Demo/Demo.java
2.2 scp on Windows
If you are under Windows environment, notice here you CANNOT use scp, alternatively, we use pscp instead, the command is:1
pscp -i /path/of/you/key/file/key_file_name.ppk path/of/the/source/directory/file_name path/of/the/destination/directory/file_name
Now you want to copy a file named Hello.java
under C:\Users\15619\Demo\
from your local device to the remote instance under /Java/Demo/
, the command is:1
pscp -i C:\Users\15619\Demo\15619.ppk C:\Users\15619\Demo\Hello.java ec2-user@ec2-1-5-6-19.compute-1.amazonaws.com:/Java/Demo/Hello.java
Conversely, if you want to copy a file named Demo.java
under /Java/Demo/
on the remote instance to you local device under C:\Users\15619\Demo\
, the command is:1
pscp -i C:\Users\15619\Demo\15619.ppk ec2-user@ec2-1-5-6-19.compute-1.amazonaws.com:/Java/Demo/Demo.java C:\Users\15619\Demo\Demo.java
2.3 scp recursively
If we need to copy a folder including all the files in it, then we should add -r
:
On Mac:1
scp -r -i /Users/15619/Demo/15619.pem ec2-user 2-1-5-6-19.compute-1.amazonaws.com:/Java/TestFiles/ /Users/15619/Demo/TestFiles/
On Windows:1
pscp -r -i C:\Users\15619\Demo\15619.ppk ec2-user@ec2-1-5-6-19.compute-1.amazonaws.com:/Java/Demo/TestFiles/ C:\Users\15619\Demo\TestFiles\