I requested for HostGator to provide ssh access. Since then, I haven’t used it. I attempted to ssh into the server today and received the message ‘too many authentication failures’.
If you haven’t done so already, create a PubKey:
ssh-keygen -t dsa
The ssh-keygen program will ask a few questions such as the key name and the passphrase. Enter whatever you’d like. I’ll refer to the file as id_dsa.pub (which is the default name).
Navigate to your .ssh directory:
cd ~/.ssh
Make sure your file exists in this directory.
Now this one-liner does it all:
cat ~/.ssh/id_dsa.pub | ssh -p 2222 -o PubkeyAuthentication=no user@hostname 'cat >> .ssh/authorized_keys'
If you’re not familiar with the linux pipe, I will explain a bit. If you understand the above command, you’re done. Enjoy.
The first part before the vertical line {the ‘|‘ is called a pipe) dumps all of the text (cat) inside the file (~/.ssh/id_dsa.pub) into STDOUT, or the standard output stream. The pipe forces STDOUT from before the pipe to become STDIN (standard input) for the command after the pipe. Normally, on HostGator you would open an ssh session with ssh -p 2222 user@hostname. But, because there is a conflict in either existing keys on your machine or the authentication of those keys on the server, you’ll have to add -o PubkeyAuthentication=no. Finally, the command within single quotes at the end dumps the text in STDIN into a file handle (the ‘>‘ causes the dumping of this text to redirect STDIN to an existing filehandle, while two of those characters ‘»‘ redirects to a file, creating the file if it doesn’t exist).