title: id_rsa.pub file SSH Error: invalid format
body: <p>My co worker and I have been trying to get my macbook to ssh into our AWS production server with no luck, this did work several months ago, but when we went to do it yesterday - we kept getting the following error message.</p>
  
  <p>(I have replaced my username and hostname with a placeholder on all below sections)</p>
  
  <pre><code>$ ssh titan-data
  Load key "/Users/aronlilland/.ssh/id_rsa.pub": invalid format
  user@my_ip_address: Permission denied (publickey).
  </code></pre>
  
  <h3>Relavant config file data</h3>
  
  <pre><code>&lt;!-- config file --&gt;
  Host titan-data
      HostName my_ip_address
      User user
      IdentityFile ~/.ssh/id_rsa.pub
      AddKeysToAgent yes
  </code></pre>
  
  <p>I have gone through every step imaginable to get it working again, these are the following steps I have done in order:</p>
  
  <h3>Step 1) ensured that the .pub file was added on the server</h3>
  
  <p>this step was repeated several times, I regenerated my .pub file 4 times through out this process of elimination. The error seems to be something local though, not something from the server, someone will need to correct me if I am wrong.</p>
  
  <h3>Step 2) checked that the following folders and files have the correct permissions</h3>
  
  <pre><code>drwx------    6 aronlilland  staff   192B Jan 18 09:41 .ssh/
  </code></pre>
  
  <h1> </h1>
  
  <pre><code>drwx------    6 aronlilland  staff   192B Jan 18 09:41 ./
  drwxr-xr-x+ 104 aronlilland  staff   3.3K Jan 16 16:09 ../
  -rw-r--r--    1 aronlilland  staff   114B Jan 18 09:37 config
  -rw-------    1 aronlilland  staff   1.7K Jan 18 09:41 id_rsa
  -r--------@   1 aronlilland  staff   414B Jan 18 09:41 id_rsa.pub
  -rw-r--r--    1 aronlilland  staff   4.6K Jan 18 09:48 known_hosts
  </code></pre>
  
  <p>this did not have any effect on fixing the error message.</p>
  
  <h3>Step 3) deleted my id_rsa file and id_rsa.pub file and regenerate a new one, having my coworker add the new .pub file to the server</h3>
  
  <ol>
  <li>first way i generated my keys
  <code>ssh-keygen -t rsa -b 4096 -C "your_email@example.com"</code> as suggested in the <a href="https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/" rel="noreferrer">github documentation for generating a new SSH key</a>
  
  <ul>
  <li>added the new pub file to the server through my co worker, did not work. still getting the same error.</li>
  </ul></li>
  <li>Second way I generated my .pub file after deleting the old one, was to run <code>ssh-keygen -t rsa</code> as suggested in the <a href="https://docs.joyent.com/public-cloud/getting-started/ssh-keys/generating-an-ssh-key-manually/manually-generating-your-ssh-key-in-mac-os-x" rel="noreferrer">joyent manually generating your ssh key on OSX documentation</a>
  
  <ul>
  <li>still got the same error</li>
  </ul></li>
  </ol>
  
  <h3>Step 4) Upgraded ssh with homebrew</h3>
  
  <p>this was done following the documentation listed <a href="https://www.timhilliard.com/blog/upgrading-openssh-client-mac-osx-using-homebrew" rel="noreferrer">here, by Tim Hilliard</a></p>
  
  <h3>Step 5) deleted the ip address from my "known hosts" file (reaching for straws)</h3>
  
  <p>did not do anything, just had to confirm the IP address again.</p>
  
  <h2>Edit 1)</h2>
  
  <p>One thing I did do recently, and I remember seeing error messages during the installation, was I installed another version of python the other night, so there are two versions of python installed on my computer. Im not sure if this would cause any issues with ssh.</p>
  
  <pre><code>$ python -V
  Python 2.7.10
  $ python3 -V
  Python 3.6.4
  $ brew doctor
  Please note that these warnings are just used to help the Homebrew maintainers
  with debugging if you file an issue. If everything you use Homebrew for is
  working fine: please don't worry or file an issue; just ignore this. 
  Thanks!
  
  Warning: Python is installed at /Library/Frameworks/Python.framework
  
  Homebrew only supports building against the System-provided Python or a
  brewed Python. In particular, Pythons installed to /Library can interfere
  with other software installs.
  </code></pre>
  
  <h2>Edit 2)</h2>
  
  <p>added <code>-v</code> to ssh command, suggested by Alfabravo in comments</p>
  
  <pre><code>aronlilland:.ssh$ ssh titan-data -v
  OpenSSH_7.6p1, OpenSSL 1.0.2n  7 Dec 2017
  debug1: Reading configuration data /Users/aronlilland/.ssh/config
  debug1: /Users/aronlilland/.ssh/config line 1: Applying options for titan-data
  debug1: Reading configuration data /usr/local/etc/ssh/ssh_config
  debug1: Connecting to my_ip_address [my_ip_address] port 22.
  debug1: Connection established.
  debug1: identity file /Users/aronlilland/.ssh/id_rsa.pub type 0
  debug1: key_load_public: No such file or directory
  debug1: identity file /Users/aronlilland/.ssh/id_rsa.pub-cert type -1
  debug1: Local version string SSH-2.0-OpenSSH_7.6
  debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
  debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.2 pat OpenSSH* compat 0x04000000
  debug1: Authenticating to my_ip_address:22 as 'user'
  debug1: SSH2_MSG_KEXINIT sent
  debug1: SSH2_MSG_KEXINIT received
  debug1: kex: algorithm: curve25519-sha256@libssh.org
  debug1: kex: host key algorithm: ecdsa-sha2-nistp256
  debug1: kex: server-&gt;client cipher: chacha20-poly1305@openssh.com MAC: &lt;implicit&gt; compression: none
  debug1: kex: client-&gt;server cipher: chacha20-poly1305@openssh.com MAC: &lt;implicit&gt; compression: none
  debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
  debug1: Server host key: ecdsa-sha2-nistp256 SHA256:2l8QDc7x/fyzshKaU32jj9MeFtMAccu25MZBFNQuStQ
  debug1: Host 'my_ip_address' is known and matches the ECDSA host key.
  debug1: Found key in /Users/aronlilland/.ssh/known_hosts:19
  debug1: rekey after 134217728 blocks
  debug1: SSH2_MSG_NEWKEYS sent
  debug1: expecting SSH2_MSG_NEWKEYS
  debug1: SSH2_MSG_NEWKEYS received
  debug1: rekey after 134217728 blocks
  debug1: SSH2_MSG_EXT_INFO received
  debug1: kex_input_ext_info: server-sig-algs=&lt;rsa-sha2-256,rsa-sha2-512&gt;
  debug1: SSH2_MSG_SERVICE_ACCEPT received
  debug1: Authentications that can continue: publickey
  debug1: Next authentication method: publickey
  debug1: Offering public key: RSA SHA256:MuOjm+6gAzaxGr/n/If4LVjk/0H5/VT6zvib9/9C7c8 /Users/aronlilland/.ssh/id_rsa.pub
  debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
  Load key "/Users/aronlilland/.ssh/id_rsa.pub": invalid format
  debug1: No more authentication methods to try.
  user@my_ip_address: Permission denied (publickey).
  aronlilland:.ssh$ 
  </code></pre>
  
  <h2>Edit 3)</h2>
  
  <p>Checked to see if the .pub file is a valid format</p>
  
  <pre><code>aronlilland:.ssh$ ssh-keygen -l -f id_rsa.pub
  2048 SHA256:MuOjm+6gAzaxGr/n/If4LVjk/0H5/VT6zvib9/9C7c8 aronlilland@arons-mbp.cacc.local (RSA)
  aronlilland:.ssh$ touch foo.txt
  aronlilland:.ssh$ ls
  config       foo.txt      id_rsa       id_rsa.pub   known_hosts
  aronlilland:.ssh$ ssh-keygen -l -f foo.txt
  foo.txt is not a public key file.
  </code></pre>
  
  <h2>Edit 4)</h2>
  
  <p>Confirming that my id_rsa key and id_rsa.pub validate (I chopped off the end of my ssh-rsa key, there's no reason to include it on the web)</p>
  
  <pre><code>aronlilland:.ssh$ ssh-keygen -y -f id_rsa
  Enter passphrase: 
  ssh-rsa AAAAB3NzaC1yc2EAAAADAQA .......
  </code></pre>
  
  <p>Confirming permissions that updating file permissions did not change access (I tried 0444, 444, 0644, 644 just to be sure)</p>
  
  <pre><code>aronlilland:.ssh$ chmod 0444 id_rsa.pub
  aronlilland:.ssh$ ssh titan-data
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  Permissions 0444 for '/Users/aronlilland/.ssh/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.
  Load key "/Users/aronlilland/.ssh/id_rsa.pub": bad permissions
  user@my_ip_address: Permission denied (publickey).
  </code></pre>
  
  <p>and the other</p>
  
  <pre><code>aronlilland:.ssh$ chmod 0644 id_rsa.pub
  aronlilland:.ssh$ ssh titan-data
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  Permissions 0644 for '/Users/aronlilland/.ssh/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.
  Load key "/Users/aronlilland/.ssh/id_rsa.pub": bad permissions
  user@my_ip_address: Permission denied (publickey).
  </code></pre>
  
  <p>setting it back to <code>chmod 400 id_rsa.pub</code></p>
  
  <h2>Edit 5)</h2>
  
  <p>running <code>-vv</code></p>
  
  <pre><code>aronlilland:.ssh$ chmod 400 id_rsa.pub
  aronlilland:.ssh$ ssh titan-data -vv
  OpenSSH_7.6p1, OpenSSL 1.0.2n  7 Dec 2017
  debug1: Reading configuration data /Users/aronlilland/.ssh/config
  debug1: /Users/aronlilland/.ssh/config line 1: Applying options for titan-data
  debug1: Reading configuration data /usr/local/etc/ssh/ssh_config
  debug2: resolving "my_ip_address" port 22
  debug2: ssh_connect_direct: needpriv 0
  debug1: Connecting to my_ip_address [my_ip_address] port 22.
  debug1: Connection established.
  debug1: identity file /Users/aronlilland/.ssh/id_rsa.pub type 0
  debug1: key_load_public: No such file or directory
  debug1: identity file /Users/aronlilland/.ssh/id_rsa.pub-cert type -1
  debug1: Local version string SSH-2.0-OpenSSH_7.6
  debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
  debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.2 pat OpenSSH* compat 0x04000000
  debug2: fd 3 setting O_NONBLOCK
  debug1: Authenticating to my_ip_address:22 as 'user'
  debug1: SSH2_MSG_KEXINIT sent
  debug1: SSH2_MSG_KEXINIT received
  debug2: local client KEXINIT proposal
  debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
  debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
  debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
  debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
  debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
  debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
  debug2: compression ctos: none,zlib@openssh.com,zlib
  debug2: compression stoc: none,zlib@openssh.com,zlib
  debug2: languages ctos: 
  debug2: languages stoc: 
  debug2: first_kex_follows 0 
  debug2: reserved 0 
  debug2: peer server KEXINIT proposal
  debug2: KEX algorithms: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
  debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
  debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
  debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
  debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
  debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
  debug2: compression ctos: none,zlib@openssh.com
  debug2: compression stoc: none,zlib@openssh.com
  debug2: languages ctos: 
  debug2: languages stoc: 
  debug2: first_kex_follows 0 
  debug2: reserved 0 
  debug1: kex: algorithm: curve25519-sha256@libssh.org
  debug1: kex: host key algorithm: ecdsa-sha2-nistp256
  debug1: kex: server-&gt;client cipher: chacha20-poly1305@openssh.com MAC: &lt;implicit&gt; compression: none
  debug1: kex: client-&gt;server cipher: chacha20-poly1305@openssh.com MAC: &lt;implicit&gt; compression: none
  debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
  debug1: Server host key: ecdsa-sha2-nistp256 SHA256:2l8QDc7x/fyzshKaU32jj9MeFtMAccu25MZBFNQuStQ
  debug1: Host 'my_ip_address' is known and matches the ECDSA host key.
  debug1: Found key in /Users/aronlilland/.ssh/known_hosts:19
  debug2: set_newkeys: mode 1
  debug1: rekey after 134217728 blocks
  debug1: SSH2_MSG_NEWKEYS sent
  debug1: expecting SSH2_MSG_NEWKEYS
  debug1: SSH2_MSG_NEWKEYS received
  debug2: set_newkeys: mode 0
  debug1: rekey after 134217728 blocks
  debug2: key: /Users/aronlilland/.ssh/id_rsa.pub (0x7fe9ff41c510), explicit
  debug1: SSH2_MSG_EXT_INFO received
  debug1: kex_input_ext_info: server-sig-algs=&lt;rsa-sha2-256,rsa-sha2-512&gt;
  debug2: service_accept: ssh-userauth
  debug1: SSH2_MSG_SERVICE_ACCEPT received
  debug1: Authentications that can continue: publickey
  debug1: Next authentication method: publickey
  debug1: Offering public key: RSA SHA256:MuOjm+6gAzaxGr/n/If4LVjk/0H5/VT6zvib9/9C7c8 /Users/aronlilland/.ssh/id_rsa.pub
  debug2: we sent a publickey packet, wait for reply
  debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
  debug2: input_userauth_pk_ok: fp SHA256:MuOjm+6gAzaxGr/n/If4LVjk/0H5/VT6zvib9/9C7c8
  Load key "/Users/aronlilland/.ssh/id_rsa.pub": invalid format
  debug2: we did not send a packet, disable method
  debug1: No more authentication methods to try.
  user@my_ip_address: Permission denied (publickey).
  </code></pre>