How to set up key login for ubuntu ssh
- Blog
- 2023.08.13
- 0 views
To set up SSH key-based login on an Ubuntu system, you'll need to generate an SSH key pair, add the public key to the remote server, and configure the SSH server to allow key-based authentication. Here's a step-by-step guide:
1. Generate an SSH Key Pair (if you don't already have one):
Open a terminal on your local machine and run the following command to generate an SSH key pair (if you haven't done this already):
ssh-keygen -t rsa -b 4096
Follow the prompts to specify the key's location and set a passphrase if you'd like.
2.Copy the Public Key to the Remote Server:
Use the ssh-copy-id command to copy your public key to the remote server. Replace username and remote_server with the appropriate values:
ssh-copy-id username@remote_server
You'll need to enter the password for the remote user when prompted. This command will copy your public key to the remote server's ~/.ssh/authorized_keys file.
3.Configure SSH Server for Key-Based Authentication:
On the remote server, you need to make sure SSH is configured to allow key-based authentication. Edit the SSH server configuration file:
sudo nano /etc/ssh/sshd_config
Find the following lines and make sure they are set as shown:
PubkeyAuthentication yes
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
Save the file and then restart the SSH server:
sudo service ssh restart
4.Test the Key-Based Login:
Now you should be able to log in using your SSH key:
ssh username@remote_server
If you've set a passphrase for your SSH key, you'll be prompted to enter it.
Remember to keep your private key secure, and don't share it with anyone. Using SSH key-based authentication is more secure than password-based authentication, as it eliminates the need for passwords and relies on cryptographic keys.