How to copy files with scp to an ec2 instace? - amazon-web-services

i am trying to copy files to an EC2 instance with the scp command as follows:
scp -i "key-pair-name.pem" somefile.txt ec2xxxx.us-west-2.compute.amazonaws.com:~
I get the following error:
Permission denied (publickey).
lost connection
I am using an amazon linux machine
I am able to ssh just fine.
I've gone over Q&A here, with no luck.

Try specifying the username, eg:
scp -i "key-pair-name.pem" somefile.txt user#ec2xxxx.us-west-2.compute.amazonaws.com:~

Related

scp from local computer to EC2 instance (with WSL) - Permission denied (publickey)

I am following this guidance and try to copy a simple code from local directory into EC2 instance.
Up to and incl. section Connect to your Linux instance using WSL, everything works as in the AWS guidance. But when I try to copy a python file from local computer into the EC2 instance using the code below,
sudo mkdir WSL-path
cd WSL-path
sudo cp /mnt/c/.../test-pair2.pem test-pair2.pem
sudo cp /mnt/c/.../hello_world.py hello_world.py
sudo chmod -R 400 test-pair2.pem
scp -i test-pair2.pem hello_world.py ec2-user#ec2-18-184-76-175.eu-central-1.compute.amazonaws.com:~
I cannot connect to the instance and get the following:
Load key "test-pair2.pem": Permission denied
Permission denied (publickey).
lost connection
The key in the local instance can be only read by the user, with the default I could not even connect to EC2. But otherwise I think I am following the AWS manual. Would much appreciate explanation what I am doing wrong.
If you have permission denied check the following factors:
Is the key you're using using permissions 400. To check run ls -lah test-pair2.pem. If not fix it by using chmod 400.
Is the owner of the file you, if not run chown $USERNAME test-pair2.pem.
As it works with sudo we know that the host is the issue, not the target.
So problem solved - silly mistake - adding SUDO works:
sudo scp -i test-pair2.pem hello_world.py ec2-user#ec2-18-184-76-175.eu-central-1.compute.amazonaws.com:~

Permission denied when connection to ec2 intance, i have given the file permisson 400, but still not working

I have created an ec2-intance on AWS. But when im trying to connect to it by using my .pem file, im getting error message: Permission denied (publickey,gssapi-keyex,gssapi-with-mic). I have changed the permission to this file by chmod 400 myfile.pm.
This is the command i use to connect to my instance: ssh -i ec2demo.pem ec2demo#ec2-35-158-140-25.eu-central-1.compute.amazonaws.com
I also searched for the issue on internet, and some people say i need to type chmod 600 myfile.pem. It still not works. Im using macOS Mojave, and the ssh client integrated. Do i need to install the AWS-CLI to make it works? Or should it work without AWS-CLI? And is it better to use ssh client from homebrew, or?
Thanx for help
When launching a new Amazon Linux instance on Amazon EC2, the public half of the keypair is copied to:
/users/ec2-user/.ssh/authorized_keys
You can then login to the instance using the private half of the keypair:
ssh -i key.pem ec2-user#1.2.3.4
(Or, you can use a DNS name instead of an IP address.)
It sounds like you have not logged into this instance yet, so make sure you login as ec2-user instead of ec2demo. The name of the instance does not impact the Linux user on the instance.

Cannot scp a file into gcloud: Permission denied (publickey)

I'm trying to copy a file into a compute instance using gcloud like this:
gcloud compute scp ./file.txt.bz2 root#instance-1:/home/mehran/
And I'm facing an error:
No zone specified. Using zone [us-central1-c] for instance: [instance-1].
root####.###.###.###: Permission denied (publickey).
lost connection
ERROR: (gcloud.compute.scp) [/usr/bin/scp] exited with return code [1].
I've also tested without root:
gcloud compute scp ./file.txt.bz2 instance-1:/home/mehran/
And facing the same error. This is in the case that I can ssh into the same instance without any issue:
gcloud compute ssh instance-1
Apparently, my user does not have the write permission to the home folder! Even a chmod 777 did not help. I ended up copying to /tmp folder and then pick it up from there!
For some reason, the ssh (web-based) and scp were using different users. That's why my scp user didn't have access to that folder. It was trying to write into another user's home folder.
Even though this post seems solved, I had a similar issue: I could SSH into the instance but could not scp. Turns out the folder on my instance did not have read/write permissions so I could not copy (write) into that folder.
Give permissions:
chmod 777 <folder-name>
Copy file/folder to folder in instance:
sudo gcloud compute scp <file-to-copy> <username>#<instance-name>:<folder-to-copy-to-which-requires-permissions> --zone <zone> --project <project-id>

scp to EC2 Permission denied (publickey) but ssh works

I can ssh to my ubuntu AWS EC2 instance but can't do scp to file transfer
I am using below command at my mac
sudo scp - i file.pem /Users/me/proposal.pdf ubuntu#ec2-IP.compute-1.amazonaws.com:/mnt/projectFolder/
And I am seeing error ubuntu#ec2-IP.compute-1.amazonaws.com Permission denied (publickey) Lost connection
What I have tried:
Followed this link and tried Beau's answer
Made sure my .pem file has 400 permission
Made sure my authorized_keys file in .sshd folder has my .pem file as authorized
What am I missing?
Looks like a typo in your command. The command should be (note the -i):
sudo scp -i file.pem /Users/me/proposal.pdf ubuntu#ec2-IP.compute-1.amazonaws.com:/mnt/projectFolder/
I had a similar problem that was caused by the EC2 instance having no read/execute permissions to /usr/bin/scp.
$ ls -l /usr/bin/scp
---------- 1 root root 133720 Nov 23 07:38 /usr/bin/scp
Issue was solved by running sudo chmod 755 /usr/bin/scp
I know this was not the problem that OP had but future searches may bring up this question and help someone else.

permission denied to transfer file from my computer to EC2 with scp command

The instance is launched and I can connect perfectly from my computer. However when I am trying to upload a file to ec2 using the following command:
scp -r -i key.pem path/file ec2-54-195-205-200.eu-west-1.compute.amazonaws.com:/media/ephemeral0/
I have always the following error:
Permission denied (publickey).
lost connection
The username seems to be missing in your scp command. Try adding it before the remote host, separated by a #. For example, with username admin:
scp -r -i key.pem path/file admin#ec2-54-195-205-200.eu-west-1.compute.amazonaws.com:/media/ephemeral0/
The default username in EC2 varies depending on the OS. See this answer for some hints.