Transferring a file to an amazon ec2 instance using scp always gives me permission denied (publickey,gssapi-with-mic) - amazon-ec2

I am trying to transfer a file to an ec2 instance. I followed the Amazon's documentation, this is what my command looked like:
scp -i [the key's location] Documents/[the file's location] ec2-user#[public dns]:[home/[destination]]
where I replaced all the variables with the proper things, I am sure it's the correct key and it has permission 400. When I call the command, it tells me the RSA key fingerprint, asks me if I want to continue connecting. I type yes and it replies with
Permission denied (publickey,gssapi-with-mic)
lost connection
I have looked at many of the other similar questions on stack overflow and can't find a correct way to do it.
Also ssh traffic is enabled on port 22.

The example amazon provided is correct. It sounds like a folder permissions issue. If you created the folder you are trying to copy to with another user or another user created it, chances are you don't have permissions to copy to it or edit it.
If you have sudo abilities, you can try opening access for yourself. Though not recommended to be left this way, you could try this command:
sudo chmod 777 /folderlocation
That gives complete read/write/executable permissions to anyone (hence why you shouldn't leave it at 777) but it will give you the chance to test your scp command to rule out permissions.
Afterwards if you aren't familiar with permissions, I suggest you read up on it. this is an example: http://www.tuxfiles.org/linuxhelp/filepermissions.html It is generally suggested you lock down the folder as much as possible depending on the type of information held within.
If that was not the cause some other things you might want to check:
are you in the directory of your key when executing the 'scp -i keyname' command?
do you have permissions to use the folder you are transferring from?
Best of luck.

The problem may be the user name. I copied a file to my Amazon instance and first tried to use the command:
scp -r -i ../.ssh/Amazon_server_key_pair.pem ./empty.test ec2-user#ec2-xx-yy-zz-tt.compute-1.amazonaws.com:~
and got the error:Permission denied (publickey).
I then realized that my instance is an Ubuntu environment and the user user is then "ubuntu" the correct command that worked for me is then:
scp -r -i ../.ssh/Amazon_server_key_pair.pem ./empty.test ubuntu#ec2-xx-yy-zz-tt.us-west-2.compute.amazonaws.com:~
The file "empty.test" is a text file containing the text "testing ...". Replace the address of your virtual server with the correct address to your instance's Public DNS. I have replaced the ip to my instance with xx.yy.zz.tt.

I have to use ubuntu# instead of ec2-user# because when i ssh i was seeing ubuntu# in my terminal, try changing to the name you see at your terminal
Also you have to set permission for pem file in your computer
chmod 400 /path/my-key-pair.pem
The below code will copy file from your computer to Ec2 instance.
scp -i ~/location_of_your_ec2_key_pair.pem ~/location_of_transfer_file/sample.txt ubuntu#ec2_your_ec2_instance.compute.amazonaws.com:~/folder_to_which_it_needs_to_be_copied
The below code will copy file from Ec2 instance to your computer
scp -i ~/location_of_your_ec2_key_pair.pem ubuntu#ec2_your_ec2_instance.compute.amazonaws.com:~/location_of_transfer_file/sample.txt ~/folder_to_which_it_needs_to_be_copied

I was facing the same problem. Hope this will work for you.
scp -rp -i yourfile.pem ~/local_directory username#instance_url:directory
Permission should also be correct to make this work.

Might be ones uses wrong username. Happened to me, was the same error msg -> Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
lost connection

Related

Why does ec2 asks for password when i use an identity file?

I use the following command and i got the code from http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html
ssh -i my-key-pair.pem ec2-user#ec2-198-51-100-1.compute-1.amazonaws.com
I'm not sure if it's because I lost the .pem file and recreated it or what is going on here, but no matter if I use the user ubuntu, root, or ec2-user the terminal asks me for a password.
Your local private key must be shrouded as it should be. It can be disabled with key management tools if you really want, but not advised.
Double-check the file permissions on your key file. Do:
chmod 400 my-key-pair.pem
and try again.
It is also likely that the key file is just the wrong one.
You have to terminate the instance and copy it with a new ssh key. If a key is lost then access to the server is also lost.

EC2-generated key ssh setup OSX

Here's the situation:
I generated and downloaded an ssh key as a .pem file using the EC2 console
I gave this key to my colleague with a PC and he has been able to log to the server with it just fine using PuTTY.
I have been trying for hours to figure out where to put the .pem file such that I can call
ssh -v [SERVER IP ADDRESS] -l [USERNAME]
I know that the IP address and the username are accurate because that's what my colleague is using. Where do I put the .pem file, and what permissions do I set? It seems that every tutorial on this stuff assumes you generated the key using ssh-keygen, which apparently handles all this for you, and the directions on EC2 don't tell you what to do beyond downloading the key.
My current setup is this:
I copied the text inside the .pem file and put it in ~/.ssh/id_rsa
I set the permissions to chmod 600 id_rsa
I'm geting a massive error that ends with the line "Permission denied (publickey)"
Thanks for your time.
Just use ssh -i keyfile.pem user#host.com. This will use the identity file (.pem that EC2 gave you)

Using scp to copy a file to Amazon EC2 instance?

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 hk22#mec2-50-17-16-67.compute-1.amazonaws.com:~/.
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 hk22#mec2-50-17-16-67.compute-1.amazonaws.com:~/.
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.
Try specifying the user to be ec2-user, e.g.
scp -i myAmazonKey.pem phpMyAdmin-3.4.5-all-languages.tar.gz ec2-user#mec2-50-17-16-67.compute-1.amazonaws.com:~/.
See Connecting to Linux/UNIX Instances Using SSH.
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.
Your key must not be publicly viewable for SSH to work. Use this command if needed:
chmod 400 yourPublicKeyFile.pem
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 hk22#mec2-50-17-16-67.compute-1.amazonaws.com:~/.
Here are the details of what works for an EC2 instance:
scp -i /path/to/whatever.pem /users/me/path-to-file ec2-user#ec2-55-55-555-555.compute-1.amazonaws.com:~
Few notes for beginning:
Note the spaces between the three parameters given after the -i
scp stands for secure copy protocol. Knowing the words makes it easier to remember the command.
-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.
Note the :~ at the end of the destination for the EC2 instance.
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
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 \
ec2-user#ec2-11-111-11-11.compute-1.amazonaws.com:/home/ec2-user/remoteDir
The "ec2-user#ec2-11-111-11-11.compute-1.amazonaws.com" is copy-and-pasted from your ec2 instance's public DNS.
Send file from Local to Server:
scp -i .ssh/awsinstance.pem my_local_file
ubuntu#XX.XXX.XXX.XXX:/home/ubuntu
Download file from Server to Local:
scp -i .ssh/awsinstance.pem
ubuntu#XX.XXX.XXX.XXX:/home/ubuntu/server_file .
scp -i ~/path to pem file/file.pem -r(for directory) /PATH OF LOCAL/localfile user#hostname:PATH OF SERVER/serverdirectory
Below SCP format works for me
scp -i /path/my-key-pair.pem ec2-user#ec2-198-51-100-1.compute-1.amazonaws.com:~/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 ec2-user#ec2-198-51-100-1.compute-1.amazonaws.com:~/../../var/www/Filename.zip ~/Downloads
Public DNS
scp -i /path/my-key-pair.pem /path/my-file.txt ec2-user#my-instance-public-dns-name:path/
(IPv6)
scp -i /path/my-key-pair.pem /path/my-file.txt ec2-user#\[my-instance-IPv6-address\]:path/
SCP Commend
Send File from Local To Remote Server
sudo scp -i ../Downloads/new_bb_key.pem ./dump.zip ubuntu#13.127.124.129:~/.
Send File from Remote Server To Local
sudo scp -i ~/Downloads/new_bb_key.pem ubuntu#13.127.124.129:/home/ubuntu/LatestDBdump.zip Downloads/
try to use this command
if your instance is using ubuntu
scp -i myAmazonKey.pem phpMyAdmin-3.4.5-all-languages.tar.gz ec2-user#mec2-50-17-16-67.compute-1.amazonaws.com:~/.
you can get more info about your instance from here
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connection-prereqs.html
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:
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.
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
Check the permissions on the .pem file...openssh usually doesn't like world-readable private keys, and will fail (iir, scp doesn't do a great job of providing this feedback to the user).
Can you simply ssh with that key to your AWS host?
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
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.
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'

How to upload files and folders to AWS EC2 instance?

I use SSH to connect to my Ubuntu instance. With SSH I can administer files and folders on the instance, but how do I upload files and folders from my local machine to the instance?
Is it possible to do right from SSH session, without using SFTP clients?
Just to add a bit more detail to the scp command (included in OSx and most linux/unix):
scp -i myssh.pem local_file username#200.200.200.200:/home/username
Obviously - replace the pem file with the one used for ssh access. Obviously replace "username" and "200.200.200.." with valid values for your setup.
You can try kitten utility which is a wrapper around boto3. You can easily upload/download files and run commands on EC2 server or on multiple servers at once for that matter.
kitten put -i ~/.ssh/key.pem cat.jpg /tmp [SERVER NAME][SERVER IP]
Where server name is e.g ubuntu or ec2-user etc.
This will upload cat.jpg file to /tmp directory of server
As mentioned already, I've used WinSCP, which logs me in as "ec2-user" - then make sure to adjust that user's permissions via SSH. Example:
chown -R ec2-user /path/to/files
(Authenticate as the root user first.)
Whatever folder or files you need to edit via WinSCP, allow permissions on them (otherwise you will get a permission denied error when trying to upload/edit files in WinSCP).
you cannot copy files using ssh. you can use scp/sftp.
scp if you are on linux or winscp if you are on windows
You can use this:
scp -i yourkeypair.pem source destination
This Works Fine
scp -r -i myssh.pem /local/directory remote_username#10.10.0.2:/remote/directory
-r for recursive
You could also install and set up an FTP Server, which will allow you to set up users, and directories for them to upload to. That being said, I've upvoted the above because scp/sftp is the ideal method.
The easiest way is to install webmin and user the file manager (java plugin) from your browser.
//Go to home folder
cd ~
//Download the latest version
wget http://prdownloads.sourceforge.net/webadmin/webmin-1.660-1.noarch.rpm
//install
sudo rpm -U webmin-1.660-1.noarch.rpm
//Change default password of root user
passwd
Finally, open port 10000 in the security groups
Then, log into
https://server_name:10000
with user:root password:what_you_set_before

EC2 non root user login

Is there a way to log into an EC2 ubuntu ami or a way to set up an ubuntu ami so that non-root users can log in? I tried creating a user and logging in with the associated password. I also tried using the private key, copied the authorized-keys file into the .ssh directory of the non-root user's home directory and tried to log in to the box with that user account id. Neither method worked.
Thanks in advance.
So, this works, but the missing high-order bit of information here has to do with setting the right permission on the authorized-keys file in the home directory for the user. So, I copied /root/.ssh/authorized-key to /home/user, then did with
cp -r /root/.ssh /home/user
chown -R user /home/user/.ssh
This allowed me to use the keypair.pem file to log in.
Make sure you are sending your AWS keypair as the identity file, i.e.
ssh -i ~/.ssh/keypair.pem user#ec2-174-129-xxx-xx.compute-1.amazonaws.com
Also check that SSH is enabled in your security group
Assuming you would like to have users log in with a password so they need not supply a key every time, all you must do is turn on the ability to SSH in with a password. This option is turned off by default in all Linux AMIs.
vi, nano, pico, etc. into the following file with root privileges:
sudo vi /etc/ssg/sshd_config
Change the following setting to yes:
PasswordAuthentication = yes
Finally you must restart SSH (Since you are SSHed onto a remote machine, a simple reboot is fine.)
That's it! Of course, you must still add users with the adduser command and give them passwords with the passwd command for them to be able to login to your AMI. Checkout this link for more info on the OpenSSH SSH client configuration files.

Resources