To install or append an SSH key to the authorized_keys
file on a remote Linux/Unix server, you can use the ssh-copy-id
command. Here are the steps to do so:
- Open a terminal window on your local machine.
- Generate an SSH key pair (if you haven’t already):
- Use the following command to generate a new SSH key pair:
ssh-keygen -t rsa -b 4096
- Follow the prompts to create the key pair. The default location for the key pair is
~/.ssh/id_rsa
for the private key and~/.ssh/id_rsa.pub
for the public key.
- Use the following command to generate a new SSH key pair:
- Copy the public key to the remote server:
- Use the
ssh-copy-id
command to copy the public key to the remote server. For example, to copy the key to a user account named “remoteuser” on a server with IP address “192.0.2.1”, use the following command:ssh-copy-id remoteuser@192.0.2.1
- This will prompt you for the password for the remote user account. Enter the password to log in to the remote server and copy the public key to the
authorized_keys
file for the user.
- Use the
- Test the SSH connection:
- Try to log in to the remote server using SSH. If the key was installed correctly, you should be able to log in without entering a password.
Alternatively, you can manually append the contents of your local public key to the authorized_keys
file on the remote server using a text editor. Here are the steps to do so:
- Open a terminal window on your local machine.
- Display the contents of your public key:
- Use the following command to display the contents of your public key:
cat ~/.ssh/id_rsa.pub
- This will display the key in the terminal window.
- Use the following command to display the contents of your public key:
- Connect to the remote server using SSH:
- Use the following command to connect to the remote server using SSH:
ssh remoteuser@192.0.2.1
- This will prompt you for the password for the remote user account. Enter the password to log in to the remote server.
- Use the following command to connect to the remote server using SSH:
- Edit the
authorized_keys
file:- Use a text editor (such as
nano
orvim
) to edit theauthorized_keys
file for the remote user account. For example, to edit the file for the user “remoteuser”, you can use the following command:nano ~/.ssh/authorized_keys
- Append the contents of your public key to the end of the file.
- Use a text editor (such as
- Save and exit the file:
- Save the changes you made to the file and exit the text editor.
- Test the SSH connection:
- Try to log in to the remote server using SSH. If the key was installed correctly, you should be able to log in without entering a password.