This guide will walk you through the process of setting up passwordless login to a remote server using SSH key pairs. This method is more secure and convenient than using a password.
Step 1: Generate an SSH Key PairFirst, you need to create a new public/private key pair on your local machine.
- Open a terminal and run the
ssh-keygencommand: - When prompted, save the key to a specific file. For this example, use:
/home/user/.ssh/id_rsa_my_remote_host - You can set a passphrase for an extra layer of security or leave it blank for a fully automated login.
This creates two files:
- Private Key:
id_rsa_my_remote_host(Keep this secure and private on your local machine) - Public Key:
id_rsa_my_remote_host.pub(This will be copied to the server)
Next, you need to install your public key on the remote server.
- Copy the entire contents of your public key file:
id_rsa_my_remote_host.pub. - Log into the remote server using your password (this will be the last time you need it).
- On the remote server, open or create the file
~/.ssh/authorized_keys. - Paste your public key on a new line in this file. If the file already contains other keys, simply add yours at the end.
- Save the file and exit.
You can now connect without a password using the private key.
ssh my_remote_user@my_remote_host_name
To make connecting even easier, you can create a configuration file that remembers your connection details. This eliminates the need to remember usernames, hostnames, or specify the key file every time.
Edit or create the file ~/.ssh/config on your local machine and add the following block:
Host my_remote_host_name
User my_remote_user
Hostname my_host_ip_or_name
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_my_remote_host
Host: A friendly alias you will use to connect (e.g.,ssh my_server).User: Your username on the remote server.Hostname: The actual IP address or domain of the server.IdentityFile: The path to the private key you generated.
