CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Security & Hardening / ssh-copy-id

ssh-copy-id

Install your public key on a server

Beginnersshkeysaccess

ssh-copy-id logs in to a server once (usually with a password) and appends your public key to ~/.ssh/authorized_keys with the right permissions, skipping keys that are already there. After that, you log in with the key instead of a password.

Syntaxssh-copy-id [-i [IDENTITY.pub]] [-p PORT] [-o SSH_OPTION] [-f] [-n] [USER@]HOST

Watch it run

You have password access to a new VM and want to switch to key-based login.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
ssh-copy-id [email protected]

It found your default key, checked the server didn't already accept it, then asked for your password once to install it. That's the last time you type the password for this server.

Anatomy of a command

$
commandssh-copy-id

A small shell script shipped with the OpenSSH client that uses ssh to install keys.

How it works

ssh-copy-id is a shell script around ssh. First it tries to log in using the new key itself (with password prompts disabled) to find out which of the keys are already accepted, and drops those. Then it opens one normal ssh session, where you authenticate however you can today (password, or an existing key passed with -o IdentityFile), and runs a small remote shell snippet: create ~/.ssh with mode 700 if missing, append the keys to ~/.ssh/authorized_keys, set the file to 600, and restore the SELinux context with restorecon if available. It never touches the private key. Permissions matter because sshd's StrictModes (on by default) ignores authorized_keys if the home directory, ~/.ssh or the file is writable by group or others. On cloud images password login is usually disabled, so ssh-copy-id needs another way in first, and many teams manage keys with cloud-init, configuration management or SSM instead.

ssh-copy-id uses one password login to append your public key; afterwards the server verifies a signature made with your private key.

runssh-ed25519ssh + passwordappend keysignaturesessionYoulaptopid_ed25519private, 600id_ed25519.pubpublic keyssh-copy-idshell scriptsshd10.0.1.25:22authorized_keys~/.ssh, 600Remote shellkunal
1/7
Read your public key

ssh-copy-id picks the key to install: -i FILE, keys in ssh-agent, or your newest ~/.ssh/id_*.pub. Only the public half is ever read.

← → to step

Options you'll actually use

-i FILEPublic key file to install (.pub is added if you give the private key name)
-p PORTConnect to a non-standard SSH port
-o OPTIONPass an ssh option, e.g. -o 'IdentityFile ~/.ssh/old.pem' to log in
-nDry run: show which keys would be installed, change nothing
-fForce: install without checking whether the key is already there
-sUse SFTP to upload the key (for servers without a normal shell)
-t PATHInstall into a different file than ~/.ssh/authorized_keys

Common mistakes

ssh-copy-id -i ~/.ssh/id_ed25519 thinking you must point at the private key

It works (it adds .pub itself), but people then try copying the private key by hand elsewhere. Only the .pub ever goes to a server.

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host
Leaving PasswordAuthentication yes after everyone has keys

The server stays open to password brute-forcing for no reason.

Set PasswordAuthentication no in /etc/ssh/sshd_config.d/ and reload ssh, after testing key login in a second session
Fixing 'still asks for password' by chmod 777 on ~/.ssh

StrictModes makes sshd ignore keys in group- or world-writable locations; looser permissions make it worse.

chmod 755 ~ (or 750), chmod 700 ~/.ssh, chmod 600 ~/.ssh/authorized_keys, owned by the user

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the single command that would install ~/.ssh/lab_key.pub for user deploy on 10.0.1.30 port 2222, as a dry run. (The sandbox has no remote server, so just compose it.)

  2. 2

    Do by hand what ssh-copy-id does, for your own account on this machine: install ~/.ssh/lab_key.pub into ~/.ssh/authorized_keys with the correct permissions.

  3. 3

    Check that your home directory, ~/.ssh and authorized_keys would pass sshd's StrictModes.

Related commands

ssh-keygenCreate and manage SSH keyssshLog in to remote servers securelychmodChange file permissionsfail2ban-clientInspect and control fail2ban's brute-force bans
Previousssh-keygenNextopenssl
Linux / Security & Hardening / ssh-copy-id

ssh-copy-id

Install your public key on a server

Beginnersshkeysaccess

ssh-copy-id logs in to a server once (usually with a password) and appends your public key to ~/.ssh/authorized_keys with the right permissions, skipping keys that are already there. After that, you log in with the key instead of a password.

Syntaxssh-copy-id [-i [IDENTITY.pub]] [-p PORT] [-o SSH_OPTION] [-f] [-n] [USER@]HOST

Watch it run

You have password access to a new VM and want to switch to key-based login.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
ssh-copy-id [email protected]

It found your default key, checked the server didn't already accept it, then asked for your password once to install it. That's the last time you type the password for this server.

Anatomy of a command

$
commandssh-copy-id

A small shell script shipped with the OpenSSH client that uses ssh to install keys.

How it works

ssh-copy-id is a shell script around ssh. First it tries to log in using the new key itself (with password prompts disabled) to find out which of the keys are already accepted, and drops those. Then it opens one normal ssh session, where you authenticate however you can today (password, or an existing key passed with -o IdentityFile), and runs a small remote shell snippet: create ~/.ssh with mode 700 if missing, append the keys to ~/.ssh/authorized_keys, set the file to 600, and restore the SELinux context with restorecon if available. It never touches the private key. Permissions matter because sshd's StrictModes (on by default) ignores authorized_keys if the home directory, ~/.ssh or the file is writable by group or others. On cloud images password login is usually disabled, so ssh-copy-id needs another way in first, and many teams manage keys with cloud-init, configuration management or SSM instead.

ssh-copy-id uses one password login to append your public key; afterwards the server verifies a signature made with your private key.

runssh-ed25519ssh + passwordappend keysignaturesessionYoulaptopid_ed25519private, 600id_ed25519.pubpublic keyssh-copy-idshell scriptsshd10.0.1.25:22authorized_keys~/.ssh, 600Remote shellkunal
1/7
Read your public key

ssh-copy-id picks the key to install: -i FILE, keys in ssh-agent, or your newest ~/.ssh/id_*.pub. Only the public half is ever read.

← → to step

Options you'll actually use

-i FILEPublic key file to install (.pub is added if you give the private key name)
-p PORTConnect to a non-standard SSH port
-o OPTIONPass an ssh option, e.g. -o 'IdentityFile ~/.ssh/old.pem' to log in
-nDry run: show which keys would be installed, change nothing
-fForce: install without checking whether the key is already there
-sUse SFTP to upload the key (for servers without a normal shell)
-t PATHInstall into a different file than ~/.ssh/authorized_keys

Common mistakes

ssh-copy-id -i ~/.ssh/id_ed25519 thinking you must point at the private key

It works (it adds .pub itself), but people then try copying the private key by hand elsewhere. Only the .pub ever goes to a server.

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host
Leaving PasswordAuthentication yes after everyone has keys

The server stays open to password brute-forcing for no reason.

Set PasswordAuthentication no in /etc/ssh/sshd_config.d/ and reload ssh, after testing key login in a second session
Fixing 'still asks for password' by chmod 777 on ~/.ssh

StrictModes makes sshd ignore keys in group- or world-writable locations; looser permissions make it worse.

chmod 755 ~ (or 750), chmod 700 ~/.ssh, chmod 600 ~/.ssh/authorized_keys, owned by the user

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the single command that would install ~/.ssh/lab_key.pub for user deploy on 10.0.1.30 port 2222, as a dry run. (The sandbox has no remote server, so just compose it.)

  2. 2

    Do by hand what ssh-copy-id does, for your own account on this machine: install ~/.ssh/lab_key.pub into ~/.ssh/authorized_keys with the correct permissions.

  3. 3

    Check that your home directory, ~/.ssh and authorized_keys would pass sshd's StrictModes.

Related commands

ssh-keygenCreate and manage SSH keyssshLog in to remote servers securelychmodChange file permissionsfail2ban-clientInspect and control fail2ban's brute-force bans
Previousssh-keygenNextopenssl