118

I have a server with SSH running on a non-standard port. Instead of 22, it runs on 8129. To log in, I use:

ssh -p 8129 hostname

Now, whenever I need to set up a key for password-less login, I have to copy the public key and add it to authorized_keys manually. I discovered that the command ssh-copy-id could be used to simplify this process, but it seems like it does not have an option to specify the port of the ssh server.

Is there some way to tell ssh-copy-id to use port 8129, or should I just forget about this command and copy/paste manually as before?

Kevin
  • 40,767

10 Answers10

143
$ ssh-copy-id "-p 8129 user@host"

Source: http://it-ride.blogspot.com/2009/11/use-ssh-copy-id-on-different-port.html

NOTE: The port must be in front of the user@host or it will not resolve


Editor's note: as pointed out in comments and shown in other answers, ssh-copy-id as shipped by more recent versions of OpenSSH supports the -p <port_number> syntax (no quotes needed).

fra-san
  • 10,205
  • 2
  • 22
  • 43
Jsan
  • 1,446
  • 20
    It's really stupid, that ssh has syntax ssh -p 1234 user@host, ssh-copy-id "-p 1234 user@host" and finally scp -P 1234 user@host. It would be so nice to have the same syntax. – Tombart Feb 04 '15 at 12:58
  • 2
    @Tombart and then rsync has rsync -e "ssh -p 1234" user@host. I swear it's more hassle than it's worth using a custom port. – garetmckinley Sep 25 '15 at 04:10
  • 2
    @Colt McCormack's answer explains this is improved in new versions, and this peculiar syntax is no longer required. – meshy Jan 08 '16 at 11:46
  • 2
    FYI the full command requires the IP typed in twice, and should look something like: ssh-copy-id "root@192.168.0.100 -p 12345" -i ~/.ssh/id_rsa.pub 192.168.0.100 – degenerate Dec 02 '17 at 02:40
  • 1
    On macOS (I am on High Sierra) the quotes are not required. ie. ssh-copy-id -p 8129 user@host works. – Arjun Mehta May 21 '18 at 15:57
  • RHEL7 also does not require quotes – John Mar 09 '20 at 14:52
  • For me it failed when I used the quotes. What worked (openssh 8.6p1-1, arch in wsl): ssh-copy-id -i ~/.ssh/id_rsa -p 22222 user@192.168.0.1 – noname Jul 17 '21 at 19:02
  • if you using Git Bash on Windows, it should be remove the double quote and event the -i paramter if you just have 1 ssh id : ssh-copy-id -p {PORT} {USER}@{HOST}, – Luke Jan 16 '23 at 03:52
  • Update: For some reason on OpenSSH_8.9p1 Ubuntu-3ubuntu0.4, this is the syntax that works: ssh-copy-id "-p 1234" user@host – Nassim Bentarka Dec 03 '23 at 23:04
53

ssh-copy-id doesn't take any arguments that it could pass down to the underlying ssh command, but you can configure an alias in ~/.ssh/config.

Host myhost
HostName hostname
Port 8129

Then run ssh-copy-id myhost.

  • 3
    This also has the benefit of removing the need for the -p flag on regular ssh attempts. It is therefore not only the right answer to this question, it is The Right Thing, period. – Warren Young Jan 20 '12 at 14:00
  • Thanks for this. The 2nd line "HostName hostname" isn't necessary if you are satisfied with the host's natural hostname. – Lonnie Best Aug 09 '16 at 15:53
30

As of openssh-client_6.2 there is now a dedicated port flag for the command allowing for this syntax:

ssh-copy-id -p 8129 user@example

It also added support for adding other ssh options with the -o flag.

Here's is Ubuntu's man page for the appropriate version, introduced in 13.04: http://manpages.ubuntu.com/manpages/saucy/man1/ssh-copy-id.1.html

11

A quick look at the source indicates that ssh-copy-id appears to have no function that permits this. However, you could do something like the following instead:

ssh -p8129 user@host 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_*.pub
Chris Down
  • 125,559
  • 25
  • 270
  • 266
7

This works (from here):

ssh-copy-id -i ~/.ssh/id_rsa.pub '-p 221 username@host'
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
DJR
  • 71
5

I have always used scp to copy it over:

scp -P 8129 ~/.ssh/id_*.pub user@host:
ssh -p 8129 user@host 'cat id_*.pub >> ~/.ssh/authorized_keys'

Though I must say, I'll probably be using the other (one-line/connection) methods if I remember them in the future. But this is another option for you.

Kevin
  • 40,767
3

On CentOS7 is just:

 ssh-copy-id "-p 1234" user@host

Please take care to don't place user@host within the quotes or you'll get the following error in this distribution:

/usr/bin/ssh-copy-id: ERROR: Bad port ' 1234 user@host'
2

There four ways to achieve this:

1. Using ssh-copy-id

ssh-copy-id -p <port> <user>@<remote-host>

1.1 On CentOS

ssh-copy-id "<user>@<remote-host> -p <port>"

2. Using SSH

ssh -p <port> <user>@<remote-host> 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_*.pub

3. Using SCP

scp -P <port> ~/.ssh/id_*.pub <user>@<remote-host>:
ssh -p <port> <user>@<remote-host> 'cat id_*.pub >> ~/.ssh/authorized_keys'

4. Using ~/.ssh/config file

## Host <alias>
##   Hostname <remote-host>
##   Port <port>
##   User <user>
ssh-copy-id <alias>
Teocci
  • 151
0

With my macOS, this worked.

ssh-copy-id -i ~/.ssh/id_rsa.pub -p <port> user@host
Jin Kwon
  • 510
0

I use this command:

ssh-copy-id ssh://user@ip_addr:port

Example:

ssh-copy-id ssh://root@1.2.3.4:23

Carlos
  • 1