Network

The ssh-copy-id command

Copies your public key to a server so you can log in over SSH without a password.

Syntax

ssh-copy-id [-i [archivo.pub]] [-p puerto] [-n | -f] usuario@hostTry it in the terminal →

Most useful options

-i [archivo.pub]Specific public key to copy. Without a file, it uses the newest .pub in ~/.ssh
-p puertoSSH port of the server, if it is not 22
-nDry run: shows what would be installed, but does not copy any key
-fCopies without first checking whether the key is already installed on the server
-t rutaFile on the server where the key is added. By default, .ssh/authorized_keys
-o opciónPasses an ssh option, for example -o IdentitiesOnly=yes
-sUses sftp instead of remote commands. Useful if the server only allows sftp

Copy-ready examples

$ ssh-keygen -t ed25519 && ssh-copy-id invitado@servidor-aulacreates the key and installs it on the server in one step.
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub invitado@servidor-aulacopies a specific key, without depending on which one is the newest.
$ ssh-copy-id -p 2222 invitado@servidor-aulacopies the key to a server that listens for SSH on port 2222.
$ ssh-copy-id -n -i ~/.ssh/id_ed25519.pub invitado@servidor-auladry run: shows what would be installed without copying anything.
$ ssh-copy-id -f -i ~/.ssh/id_servidor.pub invitado@servidor-aulaforces the copy without checking first whether the key is already there. Use it only if you know why.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
ERROR: failed to open ID file 'noexiste.pub': No such file or directoryThe file you pass with -i does not exist or you have no permission to read it.Check the name with ls ~/.ssh and, if you have no key, create one with ssh-keygen.
ERROR: Too many arguments. Expecting a target hostname, got:You gave more than one target in the command.Write a single target in the form user@host.

Typical mistakes

  • It only works if you already have password access to the server the first time.
  • If you have no public key, it fails: create one first with ssh-keygen.
  • If the key is already installed, it does not duplicate it and warns that all keys already exist.
  • Without -i, if the SSH agent has keys loaded, it copies those instead of the default .pub. Use -i to choose yours.

Differences from similar commands

  • ssh-keygen: creates the key this command installs. Without a key, ssh-copy-id has nothing to copy.
  • ssh: opens the session on the server. ssh-copy-id only prepares passwordless login.
  • scp: copies files between machines. After ssh-copy-id, scp will no longer ask for a password.

Security

Anyone with the matching private key logs in as that user. Review ~/.ssh/authorized_keys on the server and remove keys from machines you no longer use.

Related commands

FAQ

What happens on the server?

Your public key is added to that user's ~/.ssh/authorized_keys.

What happens if I run ssh-copy-id twice?

The second time it detects that the key is already installed and does not repeat it, unless you use -f.

What if the server does not use port 22?

Give the port with -p, for example -p 2222. The connection uses that same port.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course