SSH Key
📚

SSH Key

Created
Sep 1, 2025 10:05 PM
Tags

How to Check Your SSH Key and Use It on Other Servers

1. Check for Existing SSH Keys

To verify your SSH key is available:

  1. List Files in the ~/.ssh Directory:
  2. ls -al ~/.ss

    Look for your private key (e.g., id_ed25519) and its corresponding public key (e.g., id_ed25519.pub).

  3. Inspect the Public Key: To check the content of your public key:
  4. cat ~/.ssh/id_ed25519.pub

    Copy this key when you need to add it to a server or service.

2. Add the SSH Key to a New Server

To use your SSH key for authentication on another server, follow these steps:

A. Add Your Public Key to the Server

  1. Copy your public key to the server using the ssh-copy-id command:
    1. ssh-copy-id -i ~/.ssh/id_ed25519 username@server_ip_or_hostname
    2. Replace username with your server username.
    3. Replace server_ip_or_hostname with the server's IP address or hostname.
  2. If the ssh-copy-id command is not available, you can manually append your public key to the server's ~/.ssh/authorized_keys file:
    • SSH into the server using a password:
    • ssh username@server_ip_or_hostname
    • Edit the authorized_keys file:
    • nano ~/.ssh/authorized_keys
    • Paste your public key (from id_ed25519.pub) into the file, save, and exit.
  3. Set correct permissions for the .ssh directory and authorized_keys file:
  4. chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys

B. Test Your Connection

After adding your public key to the server, test the connection:

ssh username@server_ip_or_hostname

You should be logged in without being prompted for a password.

Test the SSH Connection

  • For GitHub:
  • bash
    Copy code
    ssh -T git@github.com
    
    
  • For other servers:
  • bash
    Copy code
    ssh username@hostname
    
    

You should see a success message indicating that your SSH setup is working correctly.

3. Types of Servers Where SSH Keys Can Be Used

SSH keys can be used on a wide variety of servers and services:

A. Remote Linux/Unix Servers

  • SSH keys are commonly used to manage Linux or Unix-based servers for:
    • Web hosting
    • Application deployment
    • System administration
  • Example: Access an Ubuntu or CentOS server on AWS, DigitalOcean, or Linode.

B. Cloud Platforms

  • Most cloud providers support SSH key-based authentication for virtual machines:
    • AWS: Add your public key to the EC2 instance.
    • Google Cloud: Use it for Compute Engine instances.
    • Azure: Securely access Virtual Machines (VMs).

C. Git Hosting Services

  • Platforms like GitHubGitLab, and Bitbucket use SSH keys for secure repository access (e.g., cloning, pushing, pulling).

D. Docker and Kubernetes

  • SSH keys can be used for deploying and managing containers and clusters:
    • Access to Docker hosts via SSH.
    • Secure connection to Kubernetes nodes.

E. Custom Application Servers

  • You can use SSH keys for any server or application that supports OpenSSH or similar protocols:
    • Database servers (e.g., MySQL, PostgreSQL).
    • File servers (e.g., SFTP, SCP).
    • CI/CD pipelines and automation tools (e.g., Jenkins, Ansible).

F. VPN Gateways

  • SSH keys are often used to access VPN gateways securely.

4. Managing SSH Keys for Multiple Servers

A. Use the SSH Config File

To manage multiple servers with different SSH keys, configure the ~/.ssh/config file. Example:

plaintext
Copy code
Host server1
    HostName server1.example.com
    User username1
    IdentityFile ~/.ssh/id_ed25519

Host server2
    HostName server2.example.com
    User username2
    IdentityFile ~/.ssh/id_ed25519_other

This allows you to connect using:

bash
Copy code
ssh server1
ssh server2

B. Generate Additional Keys (Optional)

If you need separate keys for different servers:

bash
Copy code
ssh-keygen -t ed25519 -b 4096 -C "your_email@example.com"

Save the key with a unique name (e.g., ~/.ssh/id_ed25519_server2).

5. Troubleshooting SSH Key Issues

  • Permission Denied (Publickey): Ensure the public key is added to the server and matches the private key on your system.
  • File Permissions: Ensure correct permissions for your local key files:
  • bash
    Copy code
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/id_ed25519
    chmod 644 ~/.ssh/id_ed25519.pub