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]
Tutorial details
Difficulty level Easy
Root privileges No
Requirements openssh client
Est. reading time 1m
[/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.

Syntax

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

Examples

Install key for server42, enter:

ssh-copy-id vivek@nas01

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

ssh-copy-id nixcraft@192.168.1.146

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@192.168.1.146's password: 
 
Number of key(s) added:        1
 
Now try logging into the machine, with:   "ssh 'nixcraft@192.168.1.146'"
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 vivek@server1.cybercit.biz'
ssh-copy-id '-p 2222 nixcraft@192.168.1.146'

OR

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

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@192.168.1.146

Sample outputs:

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

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


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • David Ramirez Mar 8, 2015 @ 4:44

    Hello – great tutorial ! However I would suggest to change “listing” to “listening” in the title.

  • Niels Jun 24, 2015 @ 8:38

    The second example code using the “-i identityfile” flag isn’t explained further. I think it’s a good idea to either delete that line or explain it further.

    Thanks!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum