What it does
ssh-copy-id logs in to the server (with your password, one last time), adds your public key to ~/.ssh/authorized_keys of the remote user, and sets the right permissions on ~/.ssh and the file. It does not add a key twice.
By default it uses all your keys (or the ones in ssh-agent). With -i you choose one key.
How to write it
ssh-copy-id [-f] [-n] [-s] [-x] [-i [IDENTITY_FILE]] [-t TARGET_PATH] [-p PORT] [-o SSH_OPTION] [USER@]HOSTOptions 9
Most used
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Install your key on a server
ssh-copy-id student@server2Asks for the password of student on server2. Next logins use the key.
2Install one specific key
ssh-copy-id -i ~/.ssh/deploy_ed25519.pub deploy@web01Only this key is added.
3Use another port
ssh-copy-id -p 2222 admin@bastion.example.comSame -p as ssh.
4See which keys would be copied
ssh-copy-id -n student@server2A dry run.
5Copy a key through a jump host
ssh-copy-id -o ProxyJump=admin@bastion.example.com admin@10.10.0.15Works for servers in a private network.
6Test right after copying
BatchModefails instead of asking for a password, so the test is honest.7Install keys for root (when root login with password is still allowed)
ssh-copy-id root@server2Then disable password login for root in sshd_config (chapter 20).
8Copy the key of an automation account
sudo -u ansible ssh-copy-id -i /home/ansible/.ssh/id_ed25519.pub ansible@web01The key of the ansible user, installed for ansible on web01.
9Install the key into a custom file
ssh-copy-id -t /home/student/.ssh/authorized_keys2 student@server2For servers with a special AuthorizedKeysFile setting.
10Use SFTP mode on a restricted server
ssh-copy-id -s sftpuser@files.example.comWhen the account has no shell.
11Do it by hand when ssh-copy-id is missing
cat ~/.ssh/id_ed25519.pub | ssh student@server2 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'What ssh-copy-id does for you.
Nothing matches this filter.
Watch out
- The server must allow password login for this first copy (or you add the key another way).
- Wrong permissions on the remote
~(group-writable),~/.sshorauthorized_keysmake sshd ignore the key.
Tips to remember
- One time:
ssh-copy-id user@host. Forever:ssh user@hostwithout password.