Accessing Servers with SSH Keys

@fakhrulnugrohoJune 16, 2025

If you’re new to managing servers, you might have heard about using SSH (Secure Shell) for secure remote access. Using SSH keys is one of the safest methods. This guide will walk you through setting up and using SSH keys to connect to your server securely.


What Are SSH Keys?

SSH keys are like digital passwords used to log in to your server. They come in two parts:


Step 1: Generate Your SSH Keys (If You Don’t Have Them Already)

If you don’t already have an SSH key pair, you’ll need to generate one:

  1. Open Your Terminal or Command Prompt.

  2. Run the Key Generation Command:

    BASH
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
    • t rsa: Specifies the RSA algorithm.
    • b 4096: Sets the key size to 4096 bits for strong security.
    • C "your_email@example.com": Adds a label to identify the key.
  3. Press Enter to accept the default location to save the key (e.g., ~/.ssh/id_rsa).

  4. Enter a Passphrase (optional but recommended) for added security.


Step 2: If You Already Have SSH Keys

If you already have an SSH key pair:

  1. Locate Your Keys:
    • By default, keys are stored in ~/.ssh/.
    • Check for files named id_rsa (private key) and id_rsa.pub (public key) with:
      BASH
      ls ~/.ssh
      
  2. Use Your Existing Keys:
    • You can skip the key generation step and proceed to adding your public key to the server.

Step 3: Add Your Public Key to the Server

To connect to your server, you need to add your public key:

  1. Copy Your Public Key:

    BASH
    cat ~/.ssh/id_rsa.pub
    
    • Copy the output text.
  2. Log In to Your Server:

    • Use another method (e.g., password) to access the server.
  3. Add Your Public Key to the Server:

    BASH
    echo "your_public_key_contents" >> ~/.ssh/authorized_keys
    
    • Replace "your_public_key_contents" with the text you copied.

Step 4: Set the Right Permissions

Ensure the correct permissions are set for your key files:

BASH
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa

Step 5: Connect to Your Server Using SSH

Now you can log in to your server:

BASH
ssh -i ~/.ssh/id_rsa username@server_ip

Adjust the command if your server uses a different port or key file.


Extra Tips


Conclusion

Using SSH keys is a secure and effective way to manage your server. Follow these steps to set up and use SSH keys. If you encounter issues or need further help, feel free to reach out!