Using ssh-copy-id with an alternate SSH port

If you’re anything like me, you’re somewhat security-minded.

Because of this, you’ve most likely moved the SSH port on your server(s) to
something other than the standard port 22.

Nothing wrong with a little bit of security through obscurity on top of all of
the other crap you’ve done to lock things down.

Unfortunately, one time I had locked myself out of a box because I had failed to copy over my private key before changing the port number.

Frustrating, but nothing a bit of digging around wouldn’t solve.

Prior to OpenSSH 6.2:

ssh-copy-id "username@hostname -p ####"
Zsh

Since OpenSSH 6.2:

ssh-copy-id -p #### username@hostname
Zsh

You could also go the route of adding the port to your ~/.ssh/config:

Host hostname
  HostName #.#.#.#
  Username username
  Port ####
Zsh

Then use ssh-copy-id hostname as you normally would!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.