Using scp to copy a file to Amazon EC2 instance? [closed]
Asked Answered
M

19

285

I am trying to use my Mac Terminal to scp a file from Downloads (phpMyAdmin I downloaded online) to my Amazon EC2 instance.

The command I used was:

scp -i myAmazonKey.pem phpMyAdmin-3.4.5-all-languages.tar.gz  [email protected]:~/.

The error I got: Warning: Identity file myAmazonKey.pem not accessible: No such file or directory. Permission denied (publickey). lost connection

Both my myAmazonkey.pem and phpMyAdmin-3.4.5-all-languages.tar.gz are in Downloads, so then I tried

scp -i /Users/Hello_Kitty22/Downloads/myAmazonKey.pem /Users/Hello_Kitty22/Downloads/phpMyAdmin-3.4.5-all-languages.tar.gz  [email protected]:~/.

and the error I got: Warning: Identity file /User/Hello_Kitty22/Downloads/myAmazonkey.pem not accessible: No such file or directory. Permission denied (publickey). lost connection

Can anyone please tell me how to fix my problem?

p.s. there is a similar post: scp (secure copy) to ec2 instance without password but it doesn't answer my question.

Mogul answered 9/7, 2012 at 1:2 Comment(1)
I feel like when I start my Amazon instance online, I cannot access my local files in Downloads any moreMogul
S
548

Try specifying the user to be ec2-user, e.g.

scp -i myAmazonKey.pem phpMyAdmin-3.4.5-all-languages.tar.gz [email protected]:~/.

See Connecting to Linux/UNIX Instances Using SSH.

Samualsamuel answered 18/12, 2012 at 3:21 Comment(8)
works great. Note that your user default user may be "ubuntu" if you are running a ubuntu instance.Epicure
of course copying to / may not work. Thanks for enlightening!Mulciber
@DanielDropik I don't know if you're joking or not... but :/ would try copy the folder to the root of the system, which would give permission errors on all machines without running sudo (or as root).Belding
why it saying Please login as the user "ubuntu" rather than the user "root". what i need to do for copyInterweave
in my case i was using the user but , the difference i add the public dns mec2-50-17-16-67.compute-1.amazonaws.com instead of a domain nameExscind
I get ssh: connect to host ec2... port 22: Bad file number lost connection Any ideas?Kaduna
I had to switch around local file name and connection string, like this scp -i myAmazonKey.pem [email protected]:~/log.tar.gz log.tar.gz Carnes
scp -i Test-2-Public-Key.pem Test-2-Private-Key.pem [email protected]:~/. When you need to copy your private instant pem file to your public instant root folder.Steer
V
66

second directory is your target destination, don't use server name there. In other words, you don't need to mention machine name for the machine you're currently in.

scp -i /path/to/your/.pemkey -r /copy/from/path user@server:/copy/to/path

-r if it's a directory.

Valerle answered 22/1, 2014 at 10:2 Comment(2)
Thank you so much for this. It was the only solution that worked for me.Hispanicism
scp -i "xyz.pem" -r file_xyz.zip [email protected]:/path/Hyperopia
B
28

Your key must not be publicly viewable for SSH to work. Use this command if needed:

chmod 400 yourPublicKeyFile.pem
Bonnice answered 18/5, 2014 at 20:43 Comment(0)
S
18

You should be on you local machine to try the above scp command.

On your local machine try:

scp -i ~/Downloads/myAmazonKey.pem ~/Downloads/phpMyAdmin-3.4.5-all-languages.tar.gz  [email protected]:~/.
Silvas answered 8/10, 2012 at 3:15 Comment(0)
S
12

Here are the details of what works for an EC2 instance:

scp -i /path/to/whatever.pem /users/me/path-to-file [email protected]:~

Few notes for beginning:

  1. Note the spaces between the three parameters given after the -i
  2. scp stands for secure copy protocol. Knowing the words makes it easier to remember the command.
  3. -i dictates that you need to give the .pem file as the next param. If there is no -i, than you do not need a .pem.
  4. Note the :~ at the end of the destination for the EC2 instance.
Seismoscope answered 18/10, 2017 at 17:16 Comment(0)
G
11

This just worked for me. I used a combination of two other answers to this question.

scp -i /Users/me/documents/myKP.pem -r /Users/me/desktop/testDir \
       [email protected]:/home/ec2-user/remoteDir

The "[email protected]" is copy-and-pasted from your ec2 instance's public DNS.

Genipap answered 12/6, 2020 at 22:2 Comment(1)
This worked for me while the other answers for some reason had no effect. Not sure what the problem was. Ubuntu 20.04 to ubuntu 20.04.Kelleekelleher
Z
10

I had exactly same problem, my solution was to

scp -i /path/pem -r /path/file/ ec2-user@public aws dns name: (leave it blank here)

once you done this part, get into ssh server and mv file to desired location

Zetes answered 5/1, 2016 at 2:3 Comment(0)
V
9

Send file from Local to Server:

scp -i .ssh/awsinstance.pem my_local_file [email protected]:/home/ubuntu

Download file from Server to Local:

scp -i .ssh/awsinstance.pem [email protected]:/home/ubuntu/server_file .

Vitrify answered 14/3, 2019 at 9:5 Comment(1)
How do you know what your [email protected] is? I'm using Windows thoughWanyen
S
7

Below SCP format works for me

scp -i /path/my-key-pair.pem [email protected]:~/SampleFile.txt ~/SampleFile2.txt

SampleFile.txt: It will be the path from your root directory(In my case, /home/ubuntu). in my case the file which I wanted to download was at /var/www

SampleFile2.txt: It will be path of your machine's root path(In my case, /home/MyPCUserName)

So, I have to write below command

scp -i /path/my-key-pair.pem [email protected]:~/../../var/www/Filename.zip ~/Downloads
Surfactant answered 21/12, 2018 at 11:5 Comment(0)
E
6
scp -i ~/path to pem file/file.pem -r(for directory) /PATH OF LOCAL/localfile user@hostname:PATH OF SERVER/serverdirectory
Ecesis answered 16/9, 2014 at 8:31 Comment(0)
M
6
  1. Public DNS

    scp -i /path/my-key-pair.pem /path/my-file.txt ec2-user@my-instance-public-dns-name:path/
    
  2. (IPv6)

     scp -i /path/my-key-pair.pem /path/my-file.txt ec2-user@\[my-instance-IPv6-address\]:path/
    
Markmarkdown answered 5/9, 2021 at 6:6 Comment(1)
works for me, lol, i actually use ~/path to target thatZoe
R
5

try to use this command

if your instance is using ubuntu

scp -i myAmazonKey.pem phpMyAdmin-3.4.5-all-languages.tar.gz  [email protected]:~/.

you can get more info about your instance from here https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connection-prereqs.html

Responser answered 20/7, 2022 at 6:40 Comment(0)
A
4

SCP Commend

Send File from Local To Remote Server

sudo scp -i ../Downloads/new_bb_key.pem ./dump.zip [email protected]:~/.

Send File from Remote Server To Local

sudo scp -i ~/Downloads/new_bb_key.pem [email protected]:/home/ubuntu/LatestDBdump.zip Downloads/

Anandrous answered 25/1, 2021 at 10:12 Comment(0)
B
2

The process of using SCP to copy files from a local machine to an AWS EC2 Linux instance is covered step-by-step (including the points mentioned below) in this video.

To correct this particular issue with using SCP:

  1. You need to specify the correct Linux user. From Amazon:

    • For Amazon Linux, the user name is ec2-user.
    • For RHEL, the user name is ec2-user or root.
    • For Ubuntu, the user name is ubuntu or root.
    • For Centos, the user name is centos.
    • For Fedora, the user name is ec2-user.
    • For SUSE, the user name is ec2-user or root.
    • Otherwise, if ec2-user and root don't work, check with your AMI provider.
  2. Your private key must not be publicly visible. Run the following command so that only the root user can read the file.

    chmod 400 /path/to/yourKeyFile.pem
    
Byproduct answered 12/9, 2017 at 2:55 Comment(0)
B
0

First you should change the mode of .pem file from read and write mode to read only mode. This can be done just by a single command in terminal sudo chmod 400 your_public_key.pem

Bestir answered 26/9, 2015 at 7:17 Comment(0)
A
0

I tried all the suggestions mentioned above and nothing worked. I terminated the current instance, launched another one and repeated the same exact process. This time no problems. Sometimes it might be the remote ami's fault.

Atone answered 5/9, 2017 at 23:19 Comment(0)
M
0

if you want to copy a folder use the "-r" parameter

scp -r -i AmazonKey.pem /Users/yourUserName/desktop/yourFolderName [email protected]:~/.
Massotherapy answered 9/1, 2024 at 10:9 Comment(0)
O
0

I find a way with aws-cli ssm, this method can work with private subnet instances, the ec2 instance do not need a public ip.

step 1: login to EC2 instance with aws ssm start-session

aws --profile ${AWS_PROFILE} ssm start-session --target ${targetInstanceId}

step 2: add public key to the ec2 instance

echo ${pubKey} >> ~/.ssh/authorized_keys

step 3: open a port forwarding with aws ssm start-session

aws --profile ${AWS_PROFILE} ssm start-session --target ${targetInstanceId} \
    --document-name AWS-StartPortForwardingSession \
    --parameters '{"portNumber":["'${remotePort}'"],"localPortNumber":["'${localPort}'"]}'
echo "${targetInstanceId}:${remotePort} ==> localhost:${localPort}"

step 4: scp

scp -P ${localPort} some/file ec2-user@localhost:/some/path/

and you can simplify these steps with ~/.ssh/config and makefile utils. this is my util script:

aws --profile ${AWS_PROFILE} ec2 describe-instances | yq '.Reservations.[].Instances.[]
  |select(.State.Name == "running")
  |.InstanceId + " " + .PrivateIpAddress + " " + (.Tags.[]|select(.Key == "Name").Value)' \
| tee out/ec2-running-instances-$ENV.yml | nl

if [[ x${instanceIdx} = x ]]; then
    Input instanceIdx "eg: 1"
fi
sel=$(cat out/ec2-running-instances-$ENV.yml | sed -n ${instanceIdx}p)
set -- $sel
targetInstanceId=$1

if [[ x$forward = x ]]; then # login 
    aws --profile ${AWS_PROFILE} ssm start-session --target ${targetInstanceId}
else # port forward
    if [[ x${remotePort} = x ]]; then
        Input remotePort "eg: 33306"
    fi
    if [[ x${localPort} = x ]]; then
        Input localPort "eg: 33306"
    fi
    echo "${targetInstanceId}:${remotePort} ==> localhost:${localPort}"
    aws --profile ${AWS_PROFILE} ssm start-session --target ${targetInstanceId} \
    --document-name AWS-StartPortForwardingSession \
    --parameters '{"portNumber":["'${remotePort}'"],"localPortNumber":["'${localPort}'"]}'
fi
Olive answered 8/3, 2024 at 3:11 Comment(0)
M
-1

I would use:

scp -i "path to .pem file" "file to be copeide from local machine" username@amazoninstance: 'destination folder to copy file on remote machine'

Mcelhaney answered 11/10, 2012 at 14:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.