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

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:~

Related

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.

Amazon EC2 ssh operation times out

I have an ec2 host from January this year which was working fine. But recently I saw that my java app there was not responding and hence I tried to ssh to my ec2 host with the .pem key. Although, ssh 22 port is allowed for all in the security group, yet it times out.
I cannot lose the data in the mysql server that lives there. So I tried to add a rule to open 3306 port and access externally to that server. But that times out too. Double checked the security groups. They seems to be okay. Here's the screenshot of my security groups.
Then, I took an image of the instance and created another instance from that image. Guess what, I cannot ssh to that host either.
Then, just to assure myself, I created another instance but this time a fresh one i.e. not from the image. I can easily ssh into my host.
What am I missing here? Or what's the problem with my previous host? I've already search for the answer in SO and none could help me to solve my problem.
You could try creating a fresh instance, then stop your old instance, detach the volume, attach the volume to your new instance and mount the volume. Then at least you would have access to the drive and could save your mysql data.
#dmohr's answer was not an exact solution but helped me a lot. However, my aws guru #leapoffaith managed to recover the data for me using the following steps. He provided me the steps he followed later and then I thought of posting these here as well as I found these useful for other developers who might face the same issue like me.
Get a new ec2 instance.
Stop the corrupted ec2, detach it’s EBS volume, attach the volume to the new instance.
Then mount the volume with your newly created EC2 instance. Use the following commands to mount :
Make a new mount point : mkdir mount_point
To mount : sudo mount /dev/xvdf1 mount_point/ (note that the device name can be different).
Get permission of the previous mysql data:
sudo chown -R $USER:$(id -gn $USER) mount_point/var/lib/mysql
Install mysql in the new ec2 host :
sudo apt-get update
sudo apt-get install mysql-server - Remember to use the same root password that you used to connect to your mysql database from your server application.
sudo mysql_secure_installation
Stop mysql service : sudo service mysql stop
Copy your database folder from the mount_point’s mysql folder to /var/lib/mysql folder. For example, sudo cp -r yourdb /var/lib/mysql/
If InnoDB Engine,
sudo cp ibdata1 /var/lib/mysql/
sudo cp ib_logfile0 /var/lib/mysql/
sudo cp ib_logfile1 /var/lib/mysql/
Give ‘mysql’ user permission :
sudo chown -R mysql:mysql yourdb/
sudo chmod -R 777 mysql/yourdb
Start mysql service : sudo service mysql start
Unmount the EBS : sudo umount -d /dev/xvdf1

How to use commands in AWS EC2 elastic beanstalk

I am trying to fix a problem I have with my program I uploaded to AWS in elastic beanstalk tomcat. I found someone who seems to have had a similar problem, but I can't find where I execute their solution.
AWS EC2 tomcat permission denied creating/writing to file
The answer said that I should use the following commands:
chmod o+x /home
chmod o+x /home/ec2-user
I want to see if this will fix my problem, however I have looked everywhere and have found no information regarding where I actually put these commands.
Is your problem fixed if you run those commands manually? (i.e. eb ssh into your instance and then sudo chmod o+x /home then sudo chmod o+x /home/ec2-user)
If so, you could automate running those commands using an EB extension file. The documentation is here but it would look something like this:
.ebextensions/01-fix-permissions.config
commands:
fix_home_permissions:
command: "chmod o+x /home"
fix_ec2user_permissions:
command: "chmod o+x /home/ec2-user"

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.

couldn't get root access to aws ec2

I'm trying to access aws instance using ssh with private keys downloaded from aws.
I got access using ec2-user which their documentation suggested.
$ ssh -i upd_prayag.pem ec2-user#ec2-54-209-155-100.compute-1.amazonaws.com
Apparently, I find two users there,
ec2-user
prayagupd
What I want is ls the user prayagupd to check whether the directory I have cloned from github exists there.
Unfortunately I see permission denied message on that user.
I get Permission denied (publickey) on
$ sudo ssh -i upd_prayag.pem ec2-user#ec2-54-209-155-100.compute-1.amazonaws.com
They suggest sudo su - should work but it has been asking password, some of the links I found through google is making me anxious that I can't get root access.
Reference
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html
If the instance is created from an AWS image, then you cannot get root access.