100

Mac OSX Lion 10.7.

In an effort to get around weird environment stuff (homebrew wasn't installing wget, and I had all sorts of weird blocks and errors), I uninstalled zschrc and homebrew and a bunch of other stuff, then installed fish shell.

Now, whenever I try to push/pull to/from github, I get this error:

The authenticity of host 'github.com (204.232.175.90)' can't be established.
RSA key fingerprint is <string of colon-separated chars that I should probs keep private>.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/Users/sasha/.ssh/known_hosts).

So I tried to check the permissions of my ~./ssh folder, and got this, which looks fine to me:

-rw-r--r--  1 sasha  staff    97B Jul  9 22:56 config
-rw-------  1 sasha  staff   1.7K May 16  2012 id_rsa
-rw-r--r--  1 sasha  staff   403B May 16  2012 id_rsa.pub
drwx------  5 sasha  staff   170B Jul 15 09:56 known_hosts

All that's in known_hosts is a pem file I used for ssh'ing (also with the "authenticity..." prompt) to an Amazon ec2 instance, though I tried copying id_rsa and id_rsa.pub there when things got desperate.

Any idea what's going on? I'd love to fix this so I don't get prompted all the many times I push/pull.

EDIT I followed these instructions successfully a while ago, so I do have my ssh keys on Github, and they're recognized, so that when I run ssh -T [email protected], I get

Hi sashafklein! You've successfully authenticated, but GitHub does not provide shell access.

It seems to be exclusively my local computer that's unhappy with my ssh situation.

1
  • For anyone here because they were trying to connect with DigitalOcean's doctl tool, try connecting to the IP directly using ssh first.
    – Jxek
    Feb 16, 2023 at 17:21

21 Answers 21

183

In your specific case, your known_hosts is a folder, so you need to remove it first.

For other people which experiencing similar issue, please check the right permission to your ~/ssh/known_hosts as it may be owned by different user (e.g. root). So you may try to run:

sudo chown -v $USER ~/.ssh/known_hosts

to fix it.

9
  • 4
    Still getting an error: Failed to add the host to the list of known hosts (/Users/igorganapolsky/.ssh/known_hosts). Apr 8, 2016 at 16:39
  • Should one change also group to $USER or leave -rw-r--r-- 1 $USER root?
    – Suncatcher
    Jul 17, 2017 at 9:31
  • 1
    @Suncatcher Safer is to leave root group. Usually users in root group are having sudo access anyway. Secondly data in known_hosts file isn't so secret.
    – kenorb
    Jul 17, 2017 at 10:56
  • 3
    Don't forget that he home directory and the .ssh directory must also have the correct permissions (read,write,execute for user only)
    – tjb
    Sep 17, 2018 at 6:01
  • 1
    Also, make sure you do this on your local machine and not the remote computer you are connecting to.
    – G_Style
    Jan 24, 2019 at 22:34
85

This is the solution i needed.

sudo chmod 700 ~/.ssh/
sudo chmod 600 ~/.ssh/*
sudo chown -R ${USER} ~/.ssh/
sudo chgrp -R ${USER} ~/.ssh/
4
  • Is changing the group really needed? Jan 8, 2020 at 13:22
  • 1
    @DmitriZaitsev Try without and see if it works, otherwise it's needed. Mar 2, 2020 at 7:15
  • 7
    I just try with 2 first commands and it worked, thanks you.
    – duyetpt
    Jun 22, 2020 at 2:51
  • This worked for me! I was struggling for 2 hours with this weird error: update_known_hosts: hostfile_replace_entries failed for /home/USER/.ssh/known_hosts: Permission denied Mar 29, 2023 at 11:36
35

For guys on Ubuntu, if you get this error:

Failed to add the host to the list of known hosts

Then simply delete the known_hosts file, and re-run your ssh. This will regenerate the known_host file with appropriate permissions, and add the remote host you are trying to ssh into to this file.

2
  • 2
    how to re-run my ssh?
    – Maviles
    Jul 13, 2016 at 2:44
  • 1
    @Maviles just connect to ssh server again, the ssl client will recreate that known_hosts file.
    – hldev
    Feb 20, 2022 at 19:31
27

I think the OP's question is solved by deleting the ~/.ssh/known_hosts (which was a folder, not a file). But for other's who might be having this issue, I noticed that one of my servers had weird permissions (400):

-r--------. 1 user user 396 Jan 7 11:12 /home/user/.ssh/known_hosts

So I solved this by adding owner/user PLUS write.

chmod u+w ~/.ssh/known_hosts

Thus. ~/.ssh/known_hosts needs to be a flat file, and must be owned by you, and you need to be able to read and write to it.

You could always declare known_hosts bankruptcy, delete it, and continue doing things as normal, and connecting to things (git / ssh) will regenerate a new known_hosts that should work just fine.

2
  • 2
    In my system (after a restore) the file was owned by root... chown user.user ~/.ssh/known_hosts fixed that.
    – Paolo
    Feb 9, 2015 at 22:15
  • 1
    Funny. How did I face this same quite a stupid issue. chmod u+w fixed the issue. Must be some faulty safety script…
    – gluk47
    Dec 22, 2015 at 23:52
24

Shouldn't known_hosts be a flat file, not a directory?

If that's not the problem, then this page on Github might be of some help. Try using SSH with the -v or -vv flag to see verbose error messages. It might give you a better idea of what's failing.

3
  • 1
    Weird. I guess I must have turned it into a directory, though I can't remember when. Moving/renaming it fixed everything. Thanks!
    – Sasha
    Jul 16, 2013 at 5:16
  • Had the same issue on WSL and after removing the file it went back to normal. Running the command created a new one. Thank you.
    – csalmeida
    Apr 24, 2019 at 2:30
  • i created the directory, known_hosts.d and work, perfectly
    – Cassol
    Sep 20, 2022 at 7:04
10

This command worked for me,

sudo chown -v $USER ~/.ssh/known_hosts

as mentioned by @kenorb.

The error was coming due to broken permissions, for the current user.

7

Okay so ideal permissions look like this
For ssh directory (You can get this by typing ls -ld ~/.ssh/)
drwx------ 2 oroborus oroborus 4096 Nov 28 12:05 /home/oroborus/.ssh/

d means directory, rwx means the user oroborus has read write and execute permission. Here oroborus is my computer name, you can find yours by echoing $USER. The second oroborus is actually the group. You can read more about what does each field mean here. It is very important to learn this because if you are working on ubuntu/osx or any Linux distro chances are you will encounter it again.

Now to make your permission look like this, you need to type
sudo chmod 700 ~/.ssh

7 in binary is 111 which means read 1 write 1 and execute 1, you can decode 6 by similar logic means only read-write permissions

You have given your user read write and execute permissions. Make sure your file permissions look like this.

total 20
-rw------- 1 oroborus oroborus  418 Nov  8  2014 authorized_keys
-rw------- 1 oroborus oroborus   34 Oct 19 14:25 config
-rw------- 1 oroborus oroborus 1679 Nov 15  2015 id_rsa
-rw------- 1 oroborus oroborus  418 Nov 15  2015 id_rsa.pub
-rw-r--r-- 1 oroborus root      222 Nov 28 12:12 known_hosts

You have given here read-write permission to your user here for all files. You can see this by typing ls -l ~/.ssh/

This issue occurs because ssh is a program is trying to write to a file called known_hosts in its folder. While writing if it knows that it doesn't have sufficient permissions it will not write in that file and hence fail. This is my understanding of the issue, more knowledgeable people can throw more light in this. Hope it helps

5

just in case anyone else encounters this error message and the permissions on .ssh and .ssh/known_hosts look good.

My problem was that I had installed gh from snap and snap apps have limited access to the file system and apparently could not access .ssh. The solution is to remove the snap installation and install from apt.

1
  • 2
    This issue still persists :<
    – Radost
    Dec 13, 2022 at 19:10
4

to me, i just do this :

rm -rf ~/.ssh/known_hosts

then :

i just ssh to the target host and all will be okay. This only if you dont know, what permission and the default owner of "known_hosts" file.

3

#check permissions of directory and files.

ls -la ~/.ssh/

#in my case, ~/.ssh/known_hosts was owned by root.

sudo chown xxx:xxx ~/.ssh/known_hosts

#where xxx = my username

#then ssh to some server, the warning message will still appear until you have ssh'd in successfully once, to verify, exit and ssh in again.

2

It happened to me simply because of broken permissions. My user did not have read nor write access to that file. Fixing permissions fixed the problem

2

I generated the "ssh" key again and added to my git account. This worked for me.

Please find following commands to generate the "ssh-key":

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

-> This creates a new ssh key, using the provided email as a label.

Generating public/private rsa key pair.

-> When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.

Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]

-> At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases"

Enter passphrase (empty for no passphrase): [Type a passphrase]

Enter same passphrase again: [Type passphrase again]

-> Your key is generated, to copy the key:

$ sudo cat /root/.ssh/id_rsa-pub

Hope this works!

0
2

For anyone interested, this one worked for me in Ubuntu:

  1. Go to .ssh directory.

    $ cd ~/.ssh
    
  2. Remove the known_hosts file.

    $ rm known_hosts
    
  3. Re-push your Git changes.

2
  • Deleting the known host fixed the issue on my side Sep 9, 2022 at 10:56
  • I'm glad it worked for you.
    – Yves
    Sep 10, 2022 at 0:06
2

It may be due to the fact that the known_hosts file is owned by another user i.e root in most cases. You can visit the path directory given (/home/taimoor/.ssh/known_hosts in my case) and check if the root is the owner and change it to the default owner.

Example:

Error Description - Error Description

Before changing the owner - Before changing the owner

After changing the owner - enter image description here

1

I was having this issue and found that within ~/.ssh/config I had a line that read:

UserKnownHostsFile=/home/.ssh-agent/known_hosts

I just modified this line to read:

UserKnownHostsFile=~/.ssh/known_hosts

That fixed the problem for me.

1

"Failed to add the host to the list of know hosts"

Was also my error. I tried chancing owner and rights.

And then i did see that it tried to write to "~/.ssh/known_hosts.d/"

Failed to add the host to the list of known hosts (~/.ssh/known_hosts.d/<hostname>).

if this also happens to you, just create the folder:

mkdir ~/.ssh/known_hosts.d
chmod 700 ~/.ssh/known_hosts.d

While this wouldn't have Solved the Problem of the Original Poster, (since his system tryed to write to the "~/.ssh/known_hosts" file and not to an systemd directory like "~/.ssh/known_hosts.d/") i thought it would help others who searched for the same error message.

The difference is that when an system tries to write to an known_hosts file all host data will be contained there. While in the directory it will create an file for each host in that directory.

For more info on such directories -> https://askubuntu.com/questions/7648/many-directories-have-a-d-suffix-extension-what-does-it-mean

2
  • Your answer could be improved by explaining why this solves the OP's problem.
    – lmonninger
    May 13, 2022 at 6:40
  • 1
    @lmonninger hope this edit is the improvment you meant, though everyone should be able to edit it now (hope i didnt misinterpret that "community wiki" checkbox ...)
    – Silver Sky
    May 14, 2022 at 12:56
0

Check permissions of the file, if it is good check parent directories

I had to correct

/home/sravindr/.ssh permissions which worked for me

0
0

it works with me when I tried the following commands

sudo chown $my_user .ssh/id_rsa
sudo chown $my_user .ssh/id_rsa.pub
sudo chown $my_user .ssh/known_hosts
0

This thing worked for me

here is the official link from GitHub you’ll need to remove the old key by running this command

$ ssh-keygen -R github.com

Or automatically update GitHub.com’s RSA SSH key in your ~/.ssh/known_hosts, by running the following in your terminal:

$ ssh-keygen -R github.com
$ curl -L https://api.github.com/meta | jq -r '.ssh_keys | .[]' | sed -e 's/^/github.com /' >> ~/.ssh/known_hosts
-1

This command worked for me,

sudo chmod +x ~/.ssh/known_hosts 
-3

I couldn't solve this despite all the above answers.

My solution was to move from ssh to https.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.