ssh-copy-id
To use the ssh-copy-id
command effectively, follow the steps below. This command allows you to copy your SSH public key to a remote server, enabling passwordless login via SSH.
Prerequisites
SSH Key Pair: Make sure you have an SSH key pair generated. The default location is:
- Public key:
~/.ssh/id_rsa.pub
- Private key:
~/.ssh/id_rsa
If you don't have an SSH key, generate one with the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Just hit Enter for all prompts to use the default location and settings.
- Public key:
Remote Server Access: You need to have SSH access to the remote server using a password at least once to copy the key.
Steps to Use ssh-copy-id
1. Copy the Public Key to the Remote Server
Use the following command to copy your public key to the remote server:
ssh-copy-id user@remote-server-ip
Replace:
user
: Your username on the remote server.remote-server-ip
: The IP address or hostname of the remote server.
Example:
ssh-copy-id john@192.168.1.10
2. Provide the Password
When prompted, enter the password for the user
on the remote server. The ssh-copy-id
command will:
- Append your public key to the
~/.ssh/authorized_keys
file on the remote server. - Set the correct permissions on the
authorized_keys
file and directory.
3. Test Passwordless Login
After the key is copied, try logging into the remote server to verify the setup:
ssh user@remote-server-ip
If everything is configured correctly, you won't be prompted for a password.
Optional: Specify a Custom SSH Key
If you are using a non-default SSH key (for example, ~/.ssh/my_custom_key
), you can specify it with the -i
option:
ssh-copy-id -i ~/.ssh/my_custom_key.pub user@remote-server-ip
Common Issues and Solutions
Permission Denied (Public Key): Ensure that:
- The
~/.ssh
directory on the remote server has700
permissions. - The
~/.ssh/authorized_keys
file has600
permissions.
You can set the correct permissions manually:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
- The
No SSH Key Found: Make sure you generated the SSH key with
ssh-keygen
and the public key is available at~/.ssh/id_rsa.pub
.
Using ssh-copy-id
is a convenient way to set up passwordless SSH access, improving both security and ease of use!
댓글
댓글 쓰기