Here is what I have tried, and I got an error:
$ cat /home/tim/.ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'
Password:
cat: >>: No such file or directory
cat: .ssh/authorized_keys: No such file or directory
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHere is what I have tried, and I got an error:
$ cat /home/tim/.ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'
Password:
cat: >>: No such file or directory
cat: .ssh/authorized_keys: No such file or directory
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 [email protected]
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 [email protected]
>>
is handled by your shell, and you're running the command through SSH instead of through a shell. His fix of having SSH run a shell, which then runs your command, should work
Jan 18, 2012 at 20:22
ssh-copy-id
won't work, right?
You could always do something like this:
scp ~/.ssh/id_rsa.pub [email protected]:/tmp/id_rsa.pub
ssh [email protected]
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.
cat
or otherwise). What you're describing is the old-fashioned way; ssh-copy-id
is recommended because there's less risk of typos or giving files wrong permissions.
Jan 18, 2012 at 23:19
cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> ~/.ssh/authorized_keys'
.
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 [email protected] 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.
ssh [email protected] 'cat >> .ssh/authorized_keys' < /home/tim/.ssh/id_rsa.pub
also work, leaving sh -c
command
in other cases it is run using shell: bash -c
command
. I was not able to find in the OpenSSH documentation about how it is decided which way will OpenSSH use. It seems to be decided on the server side. --- To wrap it up: your shorter code might or might not work. --- I tried to explain this in my answer.
Mar 13, 2021 at 12:27
ssh-copy-id
Taken from the answers/comment to the Original Poster(OP)'s question:
ssh <user>@<host> 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa.pub | ssh <user>@<host> 'cat >> ~/.ssh/authorized_keys'
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 [email protected]
Now you can run ssh-copy-id with no arguments:
ssh-copy-id
This creates ~/.ssh/authorized_keys
and fills in the basic info required from ssh-agent.
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
Indeed the the ssh-copy-id
command does exactly this (from the openssh-client
package):
ssh-copy-id user@host
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 user@host"
Note:
The port must be in front of the user@host
or it will not resolve.
2) We can specify a file with a public key:
ssh-copy-id -i ~/.ssh/id_rsa.pub user@host
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.