Use ssh-copy-id with an OpenSSH Server Listening On a Different Port

My OpenSSH server listening on TCP port number 2222. How can I use ssh-copy-id with an sshd server with listening on a different port on a Linux or Unix-like systems?

[donotprint][/donotprint]The ssh-copy-id is a shell script that uses ssh command to log into a remote machine using a login password to install your public key in a remote machine’s authorized_keys. The script also secure the remote user’s home, ~/.ssh, and ~/.ssh/autho-rized_keys files by changing the permissions.


The syntax is as follows to install ssh-keys:

ssh-copy-id user@server-name-here
ssh-copy-id -i /path/to/identity-file user@server-name-here


Install key for server42, enter:

ssh-copy-id vivek@nas01

Install key for for OpenSSH server listen on IP port 22 (default), enter:

ssh-copy-id nixcraft@

Sample outputs:

/usr/local/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/local/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
nixcraft@'s password: 
Number of key(s) added:        1
Now try logging into the machine, with:   "ssh 'nixcraft@'"
and check to make sure that only the key(s) you wanted were added.

Using ssh-copy-id with an SSH server listening on a different port # 2222

If your remote server listen on a different port than 22, you need to use the following syntax:

## old syntax. only valid for older ssh-copy-id (see below for new syntax) ##
ssh-copy-id '-p PORT-NUMBER-HERE user@server-name-here'
ssh-copy-id '-p 2222'
ssh-copy-id '-p 2222 nixcraft@'


## new syntax ##
ssh-copy-id -p 2222 nixcraft@

Sample outputs:

Fig.01: Using ssh-copy-id with an SSH server listening on a different port # 2222

Test it

Now try logging into the machine, with:

ssh -p 2222 nixcraft@

Sample outputs:

Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-46-generic x86_64)
 * Documentation:
Last login: Sat Mar  7 17:55:48 2015 from viveks-macbook-pro.local

See ssh-copy-id command man page for more info.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...