Add Public SSH Key to Remote Server in a Single Command

Add Public SSH Key to Remote Server in a Single Command

From HowToGeek

If you want to setup SSH keys to allow logging in without a password, you can do so with a single command.

The first thing you’ll need to do is make sure you’ve run the keygen command to generate the keys:

ssh-keygen -t rsa

Then use this command to push the key to the remote server, modifying it to match your server name.

cat ~/.ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'

If you have issues….

Modify /etc/ssh/sshd_config and uncomment these two lines:

#RSAAuthentication yes
#PubkeyAuthentication yes

Restart sshd:

/etc/init.d/ssh restart

If this is your problem you will see a message similar to the following upon the remote machine, in the file /var/log/auth:

Jun  3 10:23:57 localhost sshd[18461]: Authentication refused:
 bad ownership or modes for directory /home//.ssh

To fix this error you need to login to the machine (with your password!) and run the following command:

chmod 700 .ssh

If you encounter this error: Bad owner or permissions on /home//.ssh/config your permissions might look something like this:

# ls -l .ssh/config
-rw-rw-r-- 1 braker braker 32 Jul  3 03:32 .ssh/config

As a general rule any SSH configuration files should only be writable by the owner. Once the file permissions are corrected, key-based authentication should work between your servers.

Here’s the fix:
The file permissions should be set to -rw——- which correlates to chmod 600. This will give the user read/write access to the file while restricting access to anyone else on the server.

# chmod 600 ~/.ssh/config

# chmod 600 .ssh/authorized_keys

 

1 comment to Add Public SSH Key to Remote Server in a Single Command

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>