Set Up SSH Keys for Secure Server Access
Streamlining Server Access: A Step-by-Step Guide to Setting Up SSH Keys
Secure Shell (SSH) keys offer a more secure way of logging into a server with SSH than using a password alone. While a password can eventually be cracked with enough time and computing power, SSH keys are nearly impossible to decipher by brute force alone. This post will guide you through generating an SSH key pair, copying the public key to your server, setting the correct permissions, and troubleshooting common issues.
Generate SSH Key Pair: Open a terminal on your local machine. Run the following command to generate a pair of SSH keys:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"This command generates a new RSA SSH key pair with a 4096-bit key length. Replace
"your_email@example.com"with your actual email address.When prompted, you can choose the default file location (usually
~/.ssh/id_rsa) or specify a different location.
Copy Public Key to Server: Once the keys are generated, you need to copy the public key to the server you want to connect to. Use the
ssh-copy-idcommand:
ssh-copy-id shaza@125.22.33.33Replace
shazawith your actual username on the server and125.22.33.33with the server's IP address or domain name.If
ssh-copy-idis not available, manually append the contents of your public key (~/.ssh/id_rsa.pub) to theauthorized_keysfile on the server.Set Appropriate Permissions on Server: Ensure that the permissions on the
~/.sshdirectory and~/.ssh/authorized_keysfile on the server are set correctly. Run the following command:
ssh shaza@125.22.33.33 "chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"This command sets the correct permissions on the server's
.sshdirectory andauthorized_keysfile.Test SSH Key-Based Authentication: Once the public key is copied and permissions are set, test SSH key-based authentication by attempting to SSH into the server:
ssh shaza@125.22.33.33If everything is set up correctly, you should be able to log in without entering a password.
In summary, the SSH key pair (
id_rsa and id_rsa.pub) is typically saved in the.sshdirectory within your home directory(~).The private key (id_rsa) should be kept secure and not shared with anyone, while the public key (id_rsa.pub) can be distributed to servers for authentication.
Troubleshoot Common Issues :
chmod 600 ~/.ssh/authorized_keys chmod: cannot access '/home/shaza/.ssh/authorized_keys': No such file or directory s
If you're receiving a "No such file or directory" error when running the chmod command on ~/.ssh/authorized_keys, it likely means that the authorized_keys file does not exist in the .ssh directory.
Here's what you can do to resolve this issue:
Check if the
.sshdirectory exists: First, confirm whether the.sshdirectory exists in your home directory. You can use thelscommand with the-aoption to show all files, including hidden ones:
ls -a ~/.sshIf the directory does not exist, you need to create it:
mkdir ~/.sshCreate
authorized_keysfile: If the.sshdirectory exists but theauthorized_keysfile is missing, you can create it:
touch ~/.ssh/authorized_keysSet appropriate permissions: Once the
authorized_keysfile is created, you can set the correct permissions using thechmodcommand:
chmod 600 ~/.ssh/authorized_keysThis command ensures that only the owner (you) has read and write permissions on the authorized_keys file.

