607

I generate a ssh key pair on my mac and add the public key to my ubuntu server(in fact, it is a virtual machine on my mac),but when I try to login the ubuntu server,it says:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/tudouya/.ssh/vm/vm_id_rsa.pub' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /Users/tudouya/.ssh/vm/vm_id_rsa.pub
Permission denied (publickey,password).

I have tried many ways to solve this, change the key file mode, change the folder mode,as some answer on stackoverflow,but it doesn't work.
the key file permission:

vm dir:
drwxr-xr-x   4 tudouya  staff    136  4 29 10:37 vm

key file:
-rw-------  1 tudouya  staff  1679  4 29 10:30 vm_id_rsa
-rw-r--r--  1 tudouya  staff   391  4 29 10:30 vm_id_rsa.pub

please give me some idea... =========================================

I write the host infomation to ssh_config:

Host ubuntuvm
    Hostname 10.211.55.17
    PreferredAuthentications publickey
    IdentityFile /Users/tudouya/.ssh/vm/vm_id_rsa.pub

I run command "ssh -v ubuntuvm",it displays:

ssh -v ubuntuvm
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: /etc/ssh_config line 103: Applying options for *
debug1: /etc/ssh_config line 175: Applying options for ubuntuvm
debug1: Connecting to 10.211.55.17 [10.211.55.17] port 22.
debug1: Connection established.
debug1: identity file /Users/tudouya/.ssh/vm/vm_id_rsa.pub type 1
debug1: identity file /Users/tudouya/.ssh/vm/vm_id_rsa.pub-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-8
debug1: match: OpenSSH_6.6.1p1 Ubuntu-8 pat OpenSSH*
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5-etm@openssh.com none
debug1: kex: client->server aes128-ctr hmac-md5-etm@openssh.com none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 55:6d:4f:0f:23:51:ac:8e:70:01:ec:0e:62:9e:1c:10
debug1: Host '10.211.55.17' is known and matches the RSA host key.
debug1: Found key in /Users/tudouya/.ssh/known_hosts:54
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/tudouya/.ssh/vm/vm_id_rsa.pub
debug1: Server accepts key: pkalg ssh-rsa blen 279
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/tudouya/.ssh/vm/vm_id_rsa.pub' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /Users/tudouya/.ssh/vm/vm_id_rsa.pub
debug1: No more authentication methods to try.
Permission denied (publickey,password).
土豆丫
  • 6,175
  • 3
  • 14
  • 16
  • 1
    Could you edit your question to include the specific ssh command that you're running? If the key file in question is specified in the ssh config file, please include the relevant line(s) from the config file as well. – Kenster Apr 29 '15 at 10:10
  • To me was "bad permissions" – insign May 25 '17 at 20:23
  • 1
    I face this problem when add `-i id_rsa.pub` at the ssh command use for connection. Seems that enforcing to use some public key on the ssh command force to ask for password (even when the passphrase was empty, at least on my case) – Diego Andrés Díaz Espinoza Feb 04 '20 at 18:32
  • 2
    It's actually insane that these guys take the effort to print that message, including the current permission value, but don't tell you what should the permission be. It's like a "I could tell you, but this is Linux, so I'll keep it cryptic so you have to Google and waste time" attitude. – Chris Vilches Mar 31 '22 at 08:35
  • 1
    @DiegoAndrésDíazEspinoza for ssh -i you should specify a private key. For ssh-copy-id -i you should specify a public key! what's happening for you is you are doing ssh and specifying a public key(wrong move), and it thinks you're specifying a private key , uses it as a private key, that then fails so it asks for a password cos the ssh server is set to allow passwords when keys don't work – barlop Jun 12 '22 at 17:27

25 Answers25

1296

I suggest you to do:

chmod 400 ~/.ssh/id_rsa

It works fine for me.

Erazihel
  • 7,295
  • 6
  • 30
  • 53
Rick Benetti
  • 13,137
  • 2
  • 12
  • 6
  • 61
    `“Permissions 0644 for 'id_rsa.pub' are too open."` and the key was hence ignored. This was probably because i copied the key-file, from my another PC. But fixing permissions fixed the issue. thx! – parasrish Sep 07 '17 at 08:15
  • 2
    @xoxn--1'w3k4n Why is this so bad? It makes sense if people copied or otherwise poorly managed their read-write access to these sensitive files, that they should be fixed. – Gerard Feb 19 '18 at 09:48
  • 6
    This is not duct tape. If you copied over your ids, this makes sense. – ALisboa Apr 04 '18 at 21:28
  • 4
    Worked for me as a solution when copying ssh keys from old computer! – Nick Nov 12 '18 at 08:53
  • 9
    Works on WSL too – h-rai Apr 26 '19 at 02:38
  • I made this change on both the id_rsa.pub and id_rsa files and found this solved the problem for me. I also ensured the IdenetityFile in my config pointed at id_rsa – Michael Dally Feb 18 '20 at 14:05
275
debug1: identity file /Users/tudouya/.ssh/vm/vm_id_rsa.pub type 1

It appears that you're trying to use the wrong key file. The file with the ".pub" extension contains the public portion of the key. The corresponding file without the ".pub" extension contains the private part of the key. When you run an ssh client to connect to a remote server, you have to provide the private key file to the ssh client.

You probably have a line in the your .ssh/config file (or /etc/ssh_config) which looks like this:

IdentityFile .../.ssh/vm/vm_id_rsa.pub

You need to remove the ".pub" extension from the filename:

IdentityFile .../.ssh/vm/vm_id_rsa
Kenster
  • 23,465
  • 21
  • 80
  • 106
  • I was having this problem with SQLPro and had incorrectly selected the public (`.pub`) file instead of the private file. – Henry Aug 12 '16 at 04:03
  • 3
    I got this error when I did `ssh -i id_ed25519.pub` whereas doing `ssh -i id_ed25519` works – Boris Verkhovskiy Jan 15 '19 at 01:12
  • 1
    This should not be the accepted answer. @Rick Benetti's answer is simpler and applies to more situations. – whenitrains Oct 23 '20 at 02:19
  • Big thank! The real purpose, none of the suggested worked for me. The problem reproduces only on WSL2 while working on Ubuntu18 with *.pub – Yuriy Yakubskiy Jun 10 '21 at 17:55
  • this Doesn't work. I am following the documentation at https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent. And i am getting the same error as op mentioned. And this solution doesn't work for me – akshit bhatia May 21 '22 at 09:59
  • @akshitbhatia This question is about using the file with the ".pub" extension as the private key file. If you're using the correct key file--the one without the .pub extension--then this question isn't relevant to you. – Kenster May 21 '22 at 12:38
92

Key should be readable by the logged in user.

Try this:

chmod 400 ~/.ssh/Key file
chmod 400 ~/.ssh/vm_id_rsa.pub
Community
  • 1
  • 1
user3029620
  • 1,179
  • 8
  • 4
83
chmod 400 path/to/filename

This work for me. When I did this file I am able to connect to my EC2 instance

59

change your KEY permission to

chmod 400 your_key.pem

It should work !

Nikhil Babu
  • 725
  • 5
  • 8
  • https://www.cyberciti.biz/faq/unix-linux-bsd-chmod-numeric-permissions-notation-command/ – Ben Apr 30 '22 at 19:48
31

After running below command it works for me

sudo chmod 600 /path/to/my/key.pem
Ashok
  • 319
  • 3
  • 4
21

In my case, it was a .pem file. Turns out holds good for that too. Changed permissions of the file and it worked.

chmod 400 ~/.ssh/dev-shared.pem

Thanks for all of those who helped above.

Vaibhav Tripathi
  • 323
  • 2
  • 10
20

SSH keys are meant to be private so a 644 permission is too open.

Binary references to set Permissions

 r(read) = 4
 w(write) = 2
 x(execute) = 1

So by adding these numbers and by passing the summed digit to chmod command,We set the permission of file/directory. The first digit sets permission for the owner, second digit for group and the third one for all other users on the system who have no right to the file.

A permission of 644 means 
(4+2) = read/write permission for the owner
(4) = read permission for the group 
(4) = read permission for all other users 
 

By changing the the permission of the file to 400 using

chmod 400 <filename>

solves the issue. As it makes the key read-only accessible to the owner.

Ref: https://www.linux.com/training-tutorials/understanding-linux-file-permissions/

Ibnjunaid
  • 492
  • 1
  • 6
  • 14
17

Lot's of similar answers but no explanations...

The error is thrown because the private key file permissions are too open. It is a security risk.

Change the permissions on the private key file to be minimal (read only by owner)

  1. Change owner chown <unix-name> <private-key-file>
  2. Set minimal permissions (read only to file owner) chmod 400 <private-key-file>
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
16

If the keys are in the ~/.ssh directory , use

chmod 400 ~/.ssh/id_rsa

If the keys are in different directory, use

chmod 400 directory_path/id_rsa

This worked for me.

Anirban
  • 373
  • 4
  • 7
16
chmod 600 id_rsa

Run above command from path where key is stored in vm ex: cd /home/opc/.ssh

fcdt
  • 2,371
  • 5
  • 14
  • 26
Hari Krishna
  • 181
  • 1
  • 3
16

I have similar issue and solved it by changing the permission of the respective files and folder worked for me.

This is the solution which is worked for me:

$ chmod 0600 ~/.ssh/id_rsa.pub
$ chmod 0600 ~/.ssh/authorized_keys
$ chmod 0600 ~/.ssh/id_rsa
$ chmod 0700 ~/.ssh
Atul
  • 3,043
  • 27
  • 39
9

You have to run the command bellow

chmod 400 /path/to/my/key.pem
Majdi Chebil
  • 303
  • 3
  • 6
7

As for me, the default mode of id_rsa is 600, which means readable and writable.

After I push this file to a git repo and pull it from another pc, sometimes the mode of the private key file becomes -rw-r--r--.

When I pull the repo with ssh after specify the private key file, it failed and prompted warnings the same with you. Following is my script.

ssh-agent bash -c "ssh-add $PATH_OF_RSA/id_rsa; \
git pull git@gitee.com:someone/somerepo.git "

I fix this problem just by changing the mode to 600.

chmod 600 $PATH_TO_RSA/id_rsa
W.Perrin
  • 4,217
  • 32
  • 31
6

giving permision 400 makes the key private and not accessible by someone unknown. It makes the key as a protected one.

chmod 400 /Users/tudouya/.ssh/vm/vm_id_rsa.pub
5

Just run below to your pem's

sudo chmod 600 /path/to/my/key.pem 
Pravin Bansal
  • 4,315
  • 1
  • 28
  • 19
5

This problem had a quick and easy fix. You just had to change the permissions on the pem file using the following command.

chmod 400 /Users/yourUserName/pathOfYourFile/fileName.pem

This command will make the file read only and remove all other permissions on the file

user3040433
  • 85
  • 2
  • 4
4

If youre using a .ssh/config file try to

chmod 0400 .ssh/config

then:

chmod 0400 .ssh/<<KEYFILE_PATH>>
Shell_Leko
  • 512
  • 5
  • 13
4

This should do the trick:

chmod 600 id_rsa
As33
  • 67
  • 1
  • 4
  • request to call ```chmod 600 blah``` is not descriptive enough; as noted in an earlier comment, you need to run this command *in the location where the key is stored* – xxyjoel Jul 09 '21 at 22:49
3

Those who suggested chmod 400 id_rsa.pub did not sound right at all. It was quite possible that op used pub key instead of private key to ssh.

So it might be as simple as ssh -i /Users/tudouya/.ssh/vm/vm_id_rsa (the private key) user@host to fix it.

--- update ---

Check this article https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 for how to set up ssh key

Qiulang
  • 10,295
  • 11
  • 80
  • 129
  • Could be, although in my case when I searched for this issue and got to the answer the chmod 400 was what I needed to solve my problem. Thanks to all who helped! – J2N Sep 10 '18 at 14:59
2

chmod 400 /etc/ssh/* works for me.

Floern
  • 33,559
  • 24
  • 104
  • 119
Michal Sipek
  • 536
  • 1
  • 6
  • 23
  • 3
    You can do this so long as you realize you're affecting all keys in the directory. – J2N Sep 10 '18 at 14:58
1

I had this issue with a macbook m1, I later realized that i was missing sudo from the connection script

sudo ssh -i "key.pem" ubuntu@IP_Address

Ensure you are not missing the sudo in your connection script if you are using a macbook.

gagofure
  • 43
  • 1
  • 9
-1

i had similar issues and this was what i did and it worked.

sudo ssh -i webhost.pem ubuntu@ipaddress

  • 1
    This is the same solution as in [this other answer](https://stackoverflow.com/a/72498290/2227743). – Eric Aya Dec 06 '22 at 11:58
-2

There has been a lot of great explanation above, so I recommend reading and understanding.

Here is my simple step by step solution:

  1. On your terminal, run: open ~/.ssh/config

  2. In your file, you will see something similar to this (in my personal case):

Host *
  IgnoreUnknown UseKeychain
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519.pub

or like this (as per the example in this question).

Hostname 10.211.55.17
    PreferredAuthentications publickey
    IdentityFile /Users/tudouya/.ssh/vm/vm_id_rsa.pub
  1. Remove the ".pub" extention from the last line, which should look like:
Hostname 10.211.55.17
    PreferredAuthentications publickey
    IdentityFile /Users/tudouya/.ssh/vm/vm_id_rsa

or in my case:

Host *
  IgnoreUnknown UseKeychain
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519
  1. Save the file and test your ssh connection.
-12

I removed the .pub file, and it worked.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
  • 1
    Removing the .pub file is not necessary. It will also mean you don't have a record of the public key on your machine for later use. – Henry Aug 12 '16 at 04:03
  • 1
    If you have OpenSSH, you can regenerate the missing public key file from the private key by using `ssh-keygen -i -f /path/to/private.key > /desired/path/to/public.key`. So, it's not really lost. :) – dannysauer Oct 10 '16 at 14:15