When managing remote servers, using SSH keys instead of passwords not only enhances security but also streamlines the login process. In this blog post, we’ll go through the steps to set up key-based authentication for SSH, allowing you to log in without entering a password every time.
## Step 1: Generate a New SSH Key Pair
First, you need to create a new SSH key pair on your client machine. Open a terminal and run:
ssh-keygen -t rsa -b 4096 -f ~/ssh_keys/it-is-a-server-key.pem
This will create a 4096-bit RSA key pair with the private key saved as `it-is-a-server-key.pem` and the public key saved as `it-is-a-server-key.pem.pub`.
## Step 2: Configure the SSH Client
Next, we’ll configure the SSH client to use the new key when connecting to the server. Open or create the `~/.ssh/config` file and add:
Host <server-name-used-for-ssh>
HostName <ip-here>
Port 22
User <the-user-in-server-usually-root>
IdentitiesOnly yes
IdentityFile ~/ssh_keys/it-is-a-server-key.pem
Replace the placeholders with the appropriate values for your server.
## Step 3: Copy the Public Key to the Server
Now, we’ll copy the public key to the server’s `authorized_keys` file using the `ssh-copy-id` command:
ssh-copy-id -i ~/ssh_keys/it-is-a-server-key.pem.pub <the-user-in-server-usually-root>@<ip-here>
This will append the public key to the server’s `~/.ssh/authorized_keys` file for the specified user.
## Step 4: Log In Without a Password
With everything set up, you can now use the custom host name to log in:
ssh <server-name-used-for-ssh>
And that’s it! You’ll be logged into the server without being prompted for a password.
## Conclusion
SSH key-based authentication is a robust and convenient way to manage remote servers. By following these steps, you can simplify your workflow and enhance security. Always remember to keep your private keys secure, as they grant access to any system that recognizes them.