How do you copy the public key to a ssh-server?2019 Community Moderator ElectionIs it possible to run ssh-copy-id on port other than 22?SCP without password prompt using different usernameContinue shell commands once connected to SSHHow to copy a public key to your serverIs my default remote shell a trouble maker?I've installed a Public Key, but my login doesn't ask for the passphrasePassword-Less SSH Login not workingCopy ssh public key to multiple Linux hostsssh public key authentication not workingssh-copy-id: why is the end of my public key different on my local vs remote?Cannot ssh without passwordStill getting a password prompt with ssh with public key authentication?CentOS: user with separate public key fileSSH is requiring password when public key is on remote hostSSH public key exchange
Will adding a BY-SA image to a blog post make the entire post BY-SA?
Can I sign legal documents with a smiley face?
Did arcade monitors have same pixel aspect ratio as TV sets?
Why did the EU agree to delay the Brexit deadline?
Translation of Scottish 16th century church stained glass
Did US corporations pay demonstrators in the German demonstrations against article 13?
Transformation of random variables and joint distributions
How do I implement a file system driver driver in Linux?
Some numbers are more equivalent than others
Should I stop contributing to retirement accounts?
Query about absorption line spectra
A Permanent Norse Presence in America
How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?
Reply 'no position' while the job posting is still there
Varistor? Purpose and principle
Proving a function is onto where f(x)=|x|.
What (else) happened July 1st 1858 in London?
We have a love-hate relationship
Why did the HMS Bounty go back to a time when whales are already rare?
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
Why is Arduino resetting while driving motors?
Why do IPv6 unique local addresses have to have a /48 prefix?
Drawing a topological "handle" with Tikz
Is it improper etiquette to ask your opponent what his/her rating is before the game?
How do you copy the public key to a ssh-server?
2019 Community Moderator ElectionIs it possible to run ssh-copy-id on port other than 22?SCP without password prompt using different usernameContinue shell commands once connected to SSHHow to copy a public key to your serverIs my default remote shell a trouble maker?I've installed a Public Key, but my login doesn't ask for the passphrasePassword-Less SSH Login not workingCopy ssh public key to multiple Linux hostsssh public key authentication not workingssh-copy-id: why is the end of my public key different on my local vs remote?Cannot ssh without passwordStill getting a password prompt with ssh with public key authentication?CentOS: user with separate public key fileSSH is requiring password when public key is on remote hostSSH public key exchange
Here is what I have tried, and I got an error:
$ cat /home/tim/.ssh/id_rsa.pub | ssh tim@just.some.other.server 'cat >> .ssh/authorized_keys'
Password:
cat: >>: No such file or directory
cat: .ssh/authorized_keys: No such file or directory
ssh
add a comment |
Here is what I have tried, and I got an error:
$ cat /home/tim/.ssh/id_rsa.pub | ssh tim@just.some.other.server 'cat >> .ssh/authorized_keys'
Password:
cat: >>: No such file or directory
cat: .ssh/authorized_keys: No such file or directory
ssh
Why not do it in two steps? Copy it across and then append it?
– Faheem Mitha
Jan 18 '12 at 20:01
@FaheemMitha: That works, thanks! I actually might realize the cause of trouble. Please see my new post?
– Tim
Jan 18 '12 at 21:23
add a comment |
Here is what I have tried, and I got an error:
$ cat /home/tim/.ssh/id_rsa.pub | ssh tim@just.some.other.server 'cat >> .ssh/authorized_keys'
Password:
cat: >>: No such file or directory
cat: .ssh/authorized_keys: No such file or directory
ssh
Here is what I have tried, and I got an error:
$ cat /home/tim/.ssh/id_rsa.pub | ssh tim@just.some.other.server 'cat >> .ssh/authorized_keys'
Password:
cat: >>: No such file or directory
cat: .ssh/authorized_keys: No such file or directory
ssh
ssh
edited Jun 3 '13 at 6:32
Anthon
61.3k17105168
61.3k17105168
asked Jan 18 '12 at 19:31
TimTim
28k78269488
28k78269488
Why not do it in two steps? Copy it across and then append it?
– Faheem Mitha
Jan 18 '12 at 20:01
@FaheemMitha: That works, thanks! I actually might realize the cause of trouble. Please see my new post?
– Tim
Jan 18 '12 at 21:23
add a comment |
Why not do it in two steps? Copy it across and then append it?
– Faheem Mitha
Jan 18 '12 at 20:01
@FaheemMitha: That works, thanks! I actually might realize the cause of trouble. Please see my new post?
– Tim
Jan 18 '12 at 21:23
Why not do it in two steps? Copy it across and then append it?
– Faheem Mitha
Jan 18 '12 at 20:01
Why not do it in two steps? Copy it across and then append it?
– Faheem Mitha
Jan 18 '12 at 20:01
@FaheemMitha: That works, thanks! I actually might realize the cause of trouble. Please see my new post?
– Tim
Jan 18 '12 at 21:23
@FaheemMitha: That works, thanks! I actually might realize the cause of trouble. Please see my new post?
– Tim
Jan 18 '12 at 21:23
add a comment |
6 Answers
6
active
oldest
votes
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 tim@just.some.other.server
1
Thanks! Why my command doesn't work?
– Tim
Jan 18 '12 at 20:18
@Tim This answer explained it;>>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
– Michael Mrozek♦
Jan 18 '12 at 20:22
Thanks! (1) ssh-copy-id doesn't work either. I manually copy the file to the remote and append its content, then it works. I wonder why is this? I found that my default shell on the server is some script, which I update to my original post, and might be the reason. Please have a look. (2) I wonder if ssh-copy-id is just copy the public key to the remote, it doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:37
1
Let's assume the SSH server is configured in such a way that it only accepts public key authentication as a authentication mechanism. In that case, usingssh-copy-idwon't work, right?
– Abdull
Jun 7 '16 at 15:24
1
@Abdull Not unless you already have some other key on that machine to connect with. It's just connecting over SSH
– Michael Mrozek♦
Jun 7 '16 at 15:31
|
show 2 more comments
You could always do something like this:
scp ~/.ssh/id_rsa.pub user@remote.example.com:/tmp/id_rsa.pub
ssh user@remote.example.com
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.
Thanks! I wonder if ssh-copy-id is just copy the public key to the remote. It doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:51
No it doesn't create it. Just adds it.
– Mr. Monkey
Jan 18 '12 at 21:22
@Mr.Monkey Yes, you can pipe data into an ssh session (fromcator otherwise). What you're describing is the old-fashioned way;ssh-copy-idis recommended because there's less risk of typos or giving files wrong permissions.
– Gilles
Jan 18 '12 at 23:19
@Gilles , You not always have access to the server to the client, especially when you're preparing a computer for its operation, so this method is much better than using ssh-cpy-id because you do not need to take the equipment or connect to the network before setting.
– e-info128
Oct 26 '16 at 19:13
1
Or you can just pipe directly to the destination:cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> ~/.ssh/authorized_keys'.
– Pablo Bianchi
Jan 28 '18 at 2:12
add a comment |
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 tim@just.some.other.server 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.
add a comment |
openssh does provide ssh-copy-id. The sequence would be:
Generate a decent 4k key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa4kStart your ssh-agent up and suck in information like
SSH_AGENT_PID, etc.ssh-agent -s > ~/mysshagent
source ~/mysshagent
rm ~/mysshagentNow start loading keys into your SSH Agent
ssh-add ~/.ssh/id_rsa4kCheck that it is loaded
ssh-add -l
ssh-add -LThis will show you what you have in the ssh-agent
Now actually SSH to a remote system
ssh username@remotehost.networkNow you can run ssh-copy-id with no arguments:
ssh-copy-idThis creates
~/.ssh/authorized_keysand fills in the basic info required from ssh-agent.
BTW, I created a small script at github.com/centic9/generate-and-send-ssh-key which runs most of these steps in one go and additionally ensures file/directory permissions which usually always caused me headaches...
– centic
Oct 7 '15 at 11:24
This is a great method to use when password login is disabled. It allows adding a new key while authenticating with a prior key.
– MountainX
Feb 19 '18 at 22:45
add a comment |
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
add a comment |
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.
Source
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.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f29386%2fhow-do-you-copy-the-public-key-to-a-ssh-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 tim@just.some.other.server
1
Thanks! Why my command doesn't work?
– Tim
Jan 18 '12 at 20:18
@Tim This answer explained it;>>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
– Michael Mrozek♦
Jan 18 '12 at 20:22
Thanks! (1) ssh-copy-id doesn't work either. I manually copy the file to the remote and append its content, then it works. I wonder why is this? I found that my default shell on the server is some script, which I update to my original post, and might be the reason. Please have a look. (2) I wonder if ssh-copy-id is just copy the public key to the remote, it doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:37
1
Let's assume the SSH server is configured in such a way that it only accepts public key authentication as a authentication mechanism. In that case, usingssh-copy-idwon't work, right?
– Abdull
Jun 7 '16 at 15:24
1
@Abdull Not unless you already have some other key on that machine to connect with. It's just connecting over SSH
– Michael Mrozek♦
Jun 7 '16 at 15:31
|
show 2 more comments
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 tim@just.some.other.server
1
Thanks! Why my command doesn't work?
– Tim
Jan 18 '12 at 20:18
@Tim This answer explained it;>>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
– Michael Mrozek♦
Jan 18 '12 at 20:22
Thanks! (1) ssh-copy-id doesn't work either. I manually copy the file to the remote and append its content, then it works. I wonder why is this? I found that my default shell on the server is some script, which I update to my original post, and might be the reason. Please have a look. (2) I wonder if ssh-copy-id is just copy the public key to the remote, it doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:37
1
Let's assume the SSH server is configured in such a way that it only accepts public key authentication as a authentication mechanism. In that case, usingssh-copy-idwon't work, right?
– Abdull
Jun 7 '16 at 15:24
1
@Abdull Not unless you already have some other key on that machine to connect with. It's just connecting over SSH
– Michael Mrozek♦
Jun 7 '16 at 15:31
|
show 2 more comments
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 tim@just.some.other.server
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 tim@just.some.other.server
answered Jan 18 '12 at 20:01
Michael Mrozek♦Michael Mrozek
62k29193213
62k29193213
1
Thanks! Why my command doesn't work?
– Tim
Jan 18 '12 at 20:18
@Tim This answer explained it;>>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
– Michael Mrozek♦
Jan 18 '12 at 20:22
Thanks! (1) ssh-copy-id doesn't work either. I manually copy the file to the remote and append its content, then it works. I wonder why is this? I found that my default shell on the server is some script, which I update to my original post, and might be the reason. Please have a look. (2) I wonder if ssh-copy-id is just copy the public key to the remote, it doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:37
1
Let's assume the SSH server is configured in such a way that it only accepts public key authentication as a authentication mechanism. In that case, usingssh-copy-idwon't work, right?
– Abdull
Jun 7 '16 at 15:24
1
@Abdull Not unless you already have some other key on that machine to connect with. It's just connecting over SSH
– Michael Mrozek♦
Jun 7 '16 at 15:31
|
show 2 more comments
1
Thanks! Why my command doesn't work?
– Tim
Jan 18 '12 at 20:18
@Tim This answer explained it;>>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
– Michael Mrozek♦
Jan 18 '12 at 20:22
Thanks! (1) ssh-copy-id doesn't work either. I manually copy the file to the remote and append its content, then it works. I wonder why is this? I found that my default shell on the server is some script, which I update to my original post, and might be the reason. Please have a look. (2) I wonder if ssh-copy-id is just copy the public key to the remote, it doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:37
1
Let's assume the SSH server is configured in such a way that it only accepts public key authentication as a authentication mechanism. In that case, usingssh-copy-idwon't work, right?
– Abdull
Jun 7 '16 at 15:24
1
@Abdull Not unless you already have some other key on that machine to connect with. It's just connecting over SSH
– Michael Mrozek♦
Jun 7 '16 at 15:31
1
1
Thanks! Why my command doesn't work?
– Tim
Jan 18 '12 at 20:18
Thanks! Why my command doesn't work?
– Tim
Jan 18 '12 at 20:18
@Tim This answer explained it;
>> 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– Michael Mrozek♦
Jan 18 '12 at 20:22
@Tim This answer explained it;
>> 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– Michael Mrozek♦
Jan 18 '12 at 20:22
Thanks! (1) ssh-copy-id doesn't work either. I manually copy the file to the remote and append its content, then it works. I wonder why is this? I found that my default shell on the server is some script, which I update to my original post, and might be the reason. Please have a look. (2) I wonder if ssh-copy-id is just copy the public key to the remote, it doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:37
Thanks! (1) ssh-copy-id doesn't work either. I manually copy the file to the remote and append its content, then it works. I wonder why is this? I found that my default shell on the server is some script, which I update to my original post, and might be the reason. Please have a look. (2) I wonder if ssh-copy-id is just copy the public key to the remote, it doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:37
1
1
Let's assume the SSH server is configured in such a way that it only accepts public key authentication as a authentication mechanism. In that case, using
ssh-copy-id won't work, right?– Abdull
Jun 7 '16 at 15:24
Let's assume the SSH server is configured in such a way that it only accepts public key authentication as a authentication mechanism. In that case, using
ssh-copy-id won't work, right?– Abdull
Jun 7 '16 at 15:24
1
1
@Abdull Not unless you already have some other key on that machine to connect with. It's just connecting over SSH
– Michael Mrozek♦
Jun 7 '16 at 15:31
@Abdull Not unless you already have some other key on that machine to connect with. It's just connecting over SSH
– Michael Mrozek♦
Jun 7 '16 at 15:31
|
show 2 more comments
You could always do something like this:
scp ~/.ssh/id_rsa.pub user@remote.example.com:/tmp/id_rsa.pub
ssh user@remote.example.com
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.
Thanks! I wonder if ssh-copy-id is just copy the public key to the remote. It doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:51
No it doesn't create it. Just adds it.
– Mr. Monkey
Jan 18 '12 at 21:22
@Mr.Monkey Yes, you can pipe data into an ssh session (fromcator otherwise). What you're describing is the old-fashioned way;ssh-copy-idis recommended because there's less risk of typos or giving files wrong permissions.
– Gilles
Jan 18 '12 at 23:19
@Gilles , You not always have access to the server to the client, especially when you're preparing a computer for its operation, so this method is much better than using ssh-cpy-id because you do not need to take the equipment or connect to the network before setting.
– e-info128
Oct 26 '16 at 19:13
1
Or you can just pipe directly to the destination:cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> ~/.ssh/authorized_keys'.
– Pablo Bianchi
Jan 28 '18 at 2:12
add a comment |
You could always do something like this:
scp ~/.ssh/id_rsa.pub user@remote.example.com:/tmp/id_rsa.pub
ssh user@remote.example.com
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.
Thanks! I wonder if ssh-copy-id is just copy the public key to the remote. It doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:51
No it doesn't create it. Just adds it.
– Mr. Monkey
Jan 18 '12 at 21:22
@Mr.Monkey Yes, you can pipe data into an ssh session (fromcator otherwise). What you're describing is the old-fashioned way;ssh-copy-idis recommended because there's less risk of typos or giving files wrong permissions.
– Gilles
Jan 18 '12 at 23:19
@Gilles , You not always have access to the server to the client, especially when you're preparing a computer for its operation, so this method is much better than using ssh-cpy-id because you do not need to take the equipment or connect to the network before setting.
– e-info128
Oct 26 '16 at 19:13
1
Or you can just pipe directly to the destination:cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> ~/.ssh/authorized_keys'.
– Pablo Bianchi
Jan 28 '18 at 2:12
add a comment |
You could always do something like this:
scp ~/.ssh/id_rsa.pub user@remote.example.com:/tmp/id_rsa.pub
ssh user@remote.example.com
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.
You could always do something like this:
scp ~/.ssh/id_rsa.pub user@remote.example.com:/tmp/id_rsa.pub
ssh user@remote.example.com
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.
edited Mar 24 '17 at 14:17
Ward Muylaert
1094
1094
answered Jan 18 '12 at 20:06
Mr. MonkeyMr. Monkey
43249
43249
Thanks! I wonder if ssh-copy-id is just copy the public key to the remote. It doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:51
No it doesn't create it. Just adds it.
– Mr. Monkey
Jan 18 '12 at 21:22
@Mr.Monkey Yes, you can pipe data into an ssh session (fromcator otherwise). What you're describing is the old-fashioned way;ssh-copy-idis recommended because there's less risk of typos or giving files wrong permissions.
– Gilles
Jan 18 '12 at 23:19
@Gilles , You not always have access to the server to the client, especially when you're preparing a computer for its operation, so this method is much better than using ssh-cpy-id because you do not need to take the equipment or connect to the network before setting.
– e-info128
Oct 26 '16 at 19:13
1
Or you can just pipe directly to the destination:cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> ~/.ssh/authorized_keys'.
– Pablo Bianchi
Jan 28 '18 at 2:12
add a comment |
Thanks! I wonder if ssh-copy-id is just copy the public key to the remote. It doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:51
No it doesn't create it. Just adds it.
– Mr. Monkey
Jan 18 '12 at 21:22
@Mr.Monkey Yes, you can pipe data into an ssh session (fromcator otherwise). What you're describing is the old-fashioned way;ssh-copy-idis recommended because there's less risk of typos or giving files wrong permissions.
– Gilles
Jan 18 '12 at 23:19
@Gilles , You not always have access to the server to the client, especially when you're preparing a computer for its operation, so this method is much better than using ssh-cpy-id because you do not need to take the equipment or connect to the network before setting.
– e-info128
Oct 26 '16 at 19:13
1
Or you can just pipe directly to the destination:cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> ~/.ssh/authorized_keys'.
– Pablo Bianchi
Jan 28 '18 at 2:12
Thanks! I wonder if ssh-copy-id is just copy the public key to the remote. It doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:51
Thanks! I wonder if ssh-copy-id is just copy the public key to the remote. It doesn't create the private and public key, does it?
– Tim
Jan 18 '12 at 20:51
No it doesn't create it. Just adds it.
– Mr. Monkey
Jan 18 '12 at 21:22
No it doesn't create it. Just adds it.
– Mr. Monkey
Jan 18 '12 at 21:22
@Mr.Monkey Yes, you can pipe data into an ssh session (from
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.– Gilles
Jan 18 '12 at 23:19
@Mr.Monkey Yes, you can pipe data into an ssh session (from
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.– Gilles
Jan 18 '12 at 23:19
@Gilles , You not always have access to the server to the client, especially when you're preparing a computer for its operation, so this method is much better than using ssh-cpy-id because you do not need to take the equipment or connect to the network before setting.
– e-info128
Oct 26 '16 at 19:13
@Gilles , You not always have access to the server to the client, especially when you're preparing a computer for its operation, so this method is much better than using ssh-cpy-id because you do not need to take the equipment or connect to the network before setting.
– e-info128
Oct 26 '16 at 19:13
1
1
Or you can just pipe directly to the destination:
cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> ~/.ssh/authorized_keys'.– Pablo Bianchi
Jan 28 '18 at 2:12
Or you can just pipe directly to the destination:
cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> ~/.ssh/authorized_keys'.– Pablo Bianchi
Jan 28 '18 at 2:12
add a comment |
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 tim@just.some.other.server 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.
add a comment |
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 tim@just.some.other.server 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.
add a comment |
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 tim@just.some.other.server 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.
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 tim@just.some.other.server 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.
answered Dec 20 '13 at 13:38
paboukpabouk
1,6051724
1,6051724
add a comment |
add a comment |
openssh does provide ssh-copy-id. The sequence would be:
Generate a decent 4k key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa4kStart your ssh-agent up and suck in information like
SSH_AGENT_PID, etc.ssh-agent -s > ~/mysshagent
source ~/mysshagent
rm ~/mysshagentNow start loading keys into your SSH Agent
ssh-add ~/.ssh/id_rsa4kCheck that it is loaded
ssh-add -l
ssh-add -LThis will show you what you have in the ssh-agent
Now actually SSH to a remote system
ssh username@remotehost.networkNow you can run ssh-copy-id with no arguments:
ssh-copy-idThis creates
~/.ssh/authorized_keysand fills in the basic info required from ssh-agent.
BTW, I created a small script at github.com/centic9/generate-and-send-ssh-key which runs most of these steps in one go and additionally ensures file/directory permissions which usually always caused me headaches...
– centic
Oct 7 '15 at 11:24
This is a great method to use when password login is disabled. It allows adding a new key while authenticating with a prior key.
– MountainX
Feb 19 '18 at 22:45
add a comment |
openssh does provide ssh-copy-id. The sequence would be:
Generate a decent 4k key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa4kStart your ssh-agent up and suck in information like
SSH_AGENT_PID, etc.ssh-agent -s > ~/mysshagent
source ~/mysshagent
rm ~/mysshagentNow start loading keys into your SSH Agent
ssh-add ~/.ssh/id_rsa4kCheck that it is loaded
ssh-add -l
ssh-add -LThis will show you what you have in the ssh-agent
Now actually SSH to a remote system
ssh username@remotehost.networkNow you can run ssh-copy-id with no arguments:
ssh-copy-idThis creates
~/.ssh/authorized_keysand fills in the basic info required from ssh-agent.
BTW, I created a small script at github.com/centic9/generate-and-send-ssh-key which runs most of these steps in one go and additionally ensures file/directory permissions which usually always caused me headaches...
– centic
Oct 7 '15 at 11:24
This is a great method to use when password login is disabled. It allows adding a new key while authenticating with a prior key.
– MountainX
Feb 19 '18 at 22:45
add a comment |
openssh does provide ssh-copy-id. The sequence would be:
Generate a decent 4k key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa4kStart your ssh-agent up and suck in information like
SSH_AGENT_PID, etc.ssh-agent -s > ~/mysshagent
source ~/mysshagent
rm ~/mysshagentNow start loading keys into your SSH Agent
ssh-add ~/.ssh/id_rsa4kCheck that it is loaded
ssh-add -l
ssh-add -LThis will show you what you have in the ssh-agent
Now actually SSH to a remote system
ssh username@remotehost.networkNow you can run ssh-copy-id with no arguments:
ssh-copy-idThis creates
~/.ssh/authorized_keysand fills in the basic info required from ssh-agent.
openssh does provide ssh-copy-id. The sequence would be:
Generate a decent 4k key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa4kStart your ssh-agent up and suck in information like
SSH_AGENT_PID, etc.ssh-agent -s > ~/mysshagent
source ~/mysshagent
rm ~/mysshagentNow start loading keys into your SSH Agent
ssh-add ~/.ssh/id_rsa4kCheck that it is loaded
ssh-add -l
ssh-add -LThis will show you what you have in the ssh-agent
Now actually SSH to a remote system
ssh username@remotehost.networkNow you can run ssh-copy-id with no arguments:
ssh-copy-idThis creates
~/.ssh/authorized_keysand fills in the basic info required from ssh-agent.
edited Jul 14 '15 at 15:18
Michael Mrozek♦
62k29193213
62k29193213
answered Jul 14 '15 at 8:09
Christipher J THOMPSONChristipher J THOMPSON
311
311
BTW, I created a small script at github.com/centic9/generate-and-send-ssh-key which runs most of these steps in one go and additionally ensures file/directory permissions which usually always caused me headaches...
– centic
Oct 7 '15 at 11:24
This is a great method to use when password login is disabled. It allows adding a new key while authenticating with a prior key.
– MountainX
Feb 19 '18 at 22:45
add a comment |
BTW, I created a small script at github.com/centic9/generate-and-send-ssh-key which runs most of these steps in one go and additionally ensures file/directory permissions which usually always caused me headaches...
– centic
Oct 7 '15 at 11:24
This is a great method to use when password login is disabled. It allows adding a new key while authenticating with a prior key.
– MountainX
Feb 19 '18 at 22:45
BTW, I created a small script at github.com/centic9/generate-and-send-ssh-key which runs most of these steps in one go and additionally ensures file/directory permissions which usually always caused me headaches...
– centic
Oct 7 '15 at 11:24
BTW, I created a small script at github.com/centic9/generate-and-send-ssh-key which runs most of these steps in one go and additionally ensures file/directory permissions which usually always caused me headaches...
– centic
Oct 7 '15 at 11:24
This is a great method to use when password login is disabled. It allows adding a new key while authenticating with a prior key.
– MountainX
Feb 19 '18 at 22:45
This is a great method to use when password login is disabled. It allows adding a new key while authenticating with a prior key.
– MountainX
Feb 19 '18 at 22:45
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Feb 27 '18 at 21:28
user2664227user2664227
111
111
add a comment |
add a comment |
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.
Source
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.
add a comment |
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.
Source
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.
add a comment |
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.
Source
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.
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.
Source
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.
edited yesterday
answered yesterday
simhumilecosimhumileco
207210
207210
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f29386%2fhow-do-you-copy-the-public-key-to-a-ssh-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why not do it in two steps? Copy it across and then append it?
– Faheem Mitha
Jan 18 '12 at 20:01
@FaheemMitha: That works, thanks! I actually might realize the cause of trouble. Please see my new post?
– Tim
Jan 18 '12 at 21:23