Using SSH keys for server access enhances security and streamlines the login process by eliminating the need for passwords. This quick guide will show you how to set up and use an SSH key for secure server access.
Step 1: Generate SSH Key Pair (if needed)
If you don't have an SSH key pair
, generate one on your local machine:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Follow the prompts to save the key pair to a secure location.
Step 2: Copy the Public Key to the Server
If your public key
isn't already on the server, use ssh-copy-id
to add it:
ssh-copy-id -i ~/.ssh/id_rsa.pub user@server_address
Alternatively, manually copy the public key
:
cat ~/.ssh/id_rsa.pub | ssh user@server_address 'cat >> ~/.ssh/authorized_keys'
Step 3: Set Permissions
Ensure correct permissions
for the SSH directory and key files:
On your local machine:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
On the server:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Step 4: SSH into the Server
If your public key is already on the server, you can simply connect using:
ssh user@server_address
If you need to specify the private key
file:
ssh -i ~/.ssh/id_rsa user@server_address
Replace user
with your username and server_address
with the server's IP address or hostname.
Optional: Configure SSH for Easier Access
Edit ~/.ssh/config
to automate key usage:
Host server_alias
HostName server_address
User user
IdentityFile ~/.ssh/id_rsa
Now connect using the alias:
ssh server_alias
Conclusion
SSH key authentication provides a secure and efficient way to access your server. By following these steps, you can easily set up and use SSH keys for enhanced security.