Here is what I have tried, and I got an error:
$ cat /home/tim/.ssh/id_rsa.pub | ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="baced3d7fad0cfc9ce94c9d5d7df94d5ced2dfc894c9dfc8ccdfc8">[email protected]</a> 'cat >> .ssh/authorized_keys' Password: cat: >>: No such file or directory cat: .ssh/authorized_keys: No such file or directory
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
OpenSSH comes with a command to do this, ssh-copy-id. You just give it the remote address and it adds your public key to the authorized_keys file on the remote machine:
$ ssh-copy-id <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8ccd1d5f8d2cdcbcc96cbd7d5dd96d7ccd0ddca96cbddcaceddca">[email protected]</a>
You may need to use the -i flag to locate your public key on your local machine:
$ ssh-copy-id -i ~/.ssh/id_rsa.pub <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0a4b9bd90baa5a3a4fea3bfbdb5febfa4b8b5a2fea3b5a2a6b5a2">[email protected]</a>
Method 2
You could always do something like this:
scp ~/.ssh/id_rsa.pub <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20555345526052454d4f54450e4558414d504c450e434f4d">[email protected]</a>:/tmp/id_rsa.pub ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec999f899eac9e8981839889c289948d819c8089c28f8381">[email protected]</a> cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
I am not sure if you can cat from a local machine into an ssh session. Just move it to /tmp as suggested.
Edit: This is exactly what ssh-copy-id does. Just like Michael said.
Method 3
This answer describes how to make the intended way shown in the question working.
You can execute a shell on the remote computer to interpret the special meaning of the >> redirection operator:
ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4034292d002a3533346e332f2d256e2f342825326e332532362532">[email protected]</a> sh -c "'cat >> .ssh/authorized_keys'" < /home/tim/.ssh/id_rsa.pub
The redirection operator >> is normally interpreted by a shell.
When you execute ssh host 'command >> file' then it is not guaranteed that command >> file will be interpreted by a shell. In your case command >> file is executed instead of the shell without special interpretation and >> was given to the command as an argument — the same way as running command '>>' file in a shell.
Some versions of SSH (OpenSSH_5.9) will automatically invoke shell on the remote server and pass the command(s) to it when they detect tokens to be interpreted by a shell like ; > >> etc.
Method 4
openssh does provide ssh-copy-id. The sequence would be:
-
Generate a decent 4k key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa4k
-
Start your ssh-agent up and suck in information like
SSH_AGENT_PID, etc.ssh-agent -s > ~/mysshagent source ~/mysshagent rm ~/mysshagent
-
Now start loading keys into your SSH Agent
ssh-add ~/.ssh/id_rsa4k
-
Check that it is loaded
ssh-add -l ssh-add -L
This will show you what you have in the ssh-agent
-
Now actually SSH to a remote system
ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3a6a0b6a1bdb2beb693a1b6bebca7b6bbbca0a7fdbdb6a7a4bca1b8">[email protected]</a>
-
Now you can run ssh-copy-id with no arguments:
ssh-copy-id
This creates
~/.ssh/authorized_keysand fills in the basic info required from ssh-agent.
Method 5
I had troubles with ssh-copy-id when choosing another port than 22…
so here is my oneliner with a different ssh-port (e.g. 7572):
ssh yourServer.dom -p7572 "mkdir .ssh; chmod 700 .ssh; umask 177; sh -c 'cat >> .ssh/authorized_keys'" < .ssh/id_rsa.pub
Method 6
Indeed the the ssh-copy-id command does exactly this (from the openssh-client package):
ssh-copy-id <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d686e786f5d75726e69">[email protected]</a>
Note: host means IP address or domain.
I would like also to add some additional information to this
1) We can specify a different port for SSH on destination server:
ssh-copy-id "-p 8127 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99eceafcebd9f1f6eaed">[email protected]</a>"
Note:
The port must be in front of the [email protected] or it will not resolve.
2) We can specify a file with a public key:
ssh-copy-id -i ~/.ssh/id_rsa.pub <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bfef8eef9cbe3e4f8ff">[email protected]</a>
Note:
The -i option allows us to indicate the appropriate location of the name with the file that contains the public key.
Sometimes it can come in handy, especially if we store it in a non-standard location or we have more than one public key on our computer and we want to point to a specific one.
Method 7
Avoiding ssh-copy-id
Taken from the answers/comment to the Original Poster(OP)’s question:
1. with redirection (ssh at beginning)
ssh <user>@<host> 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
2. with pipe (ssh not at beginning)
cat ~/.ssh/id_rsa.pub | ssh <user>@<host> 'cat >> ~/.ssh/authorized_keys'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0