Setting up SSH on your Virtual Private Server (VPS) for GitHub is essential for secure and seamless interaction with your repositories. In this guide, we will walk you through the process step-by-step, from logging into your server to testing the SSH connection with GitHub. By the end of this tutorial, you will have SSH successfully configured and ready for use.
Step 1: Log in to Your Server Using SSH
The first step is to access your server via SSH. You can use an SSH client like PuTTY or Git Bash to log in.
Using Git Bash
- Open Git Bash on your computer.
Enter the following command, replacing
your_server_user_name
andyour_server_ip_address
with your actual server credentials:ssh your_server_user_name@your_server_ip_address
Example:
ssh root@378.345.93.198
- When prompted, enter your server password. After successful authentication, you will be logged into your server.
Step 2: Generate an SSH Key
SSH keys provide a secure way to authenticate with GitHub. To generate an SSH key, follow these steps:
In the terminal, run the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
Replace
your_email@example.com
with the email address linked to your GitHub account.- You will be prompted to specify a file to save the key. Press Enter to save it in the default location (
~/.ssh/id_ed25519
). - Next, you will be asked for a passphrase:
- Leave it empty for a password-less setup and press Enter.
- If you set a passphrase, you will need to enter it every time you interact with GitHub.
- Confirm the passphrase (if entered) or press Enter again. Your SSH key will now be generated.
Step 3: Start the SSH Agent
To use your SSH key, you need to add it to the SSH agent. Start the SSH agent in the background using the following command:
eval "$(ssh-agent -s)"
This will initialize the SSH agent and display its process ID.
Step 4: Add Your SSH Key to the SSH Agent
To add the SSH key to the agent, use this command:
ssh-add ~/.ssh/id_ed25519
If successful, you will see a confirmation message indicating that the key has been added.
Step 5: Add the SSH Key to Your GitHub Account
Now that your SSH key is ready, it needs to be added to your GitHub account for authentication.
Locate the SSH Key
Run the following command to view the public SSH key:
cat ~/.ssh/id_ed25519.pub
- Copy the entire output, which is your public key.
Add the Key to GitHub
- Log in to your GitHub account.
- Navigate to Settings > SSH and GPG Keys.
- Click New SSH Key.
- In the form:
- Title: Provide a descriptive name for the key (e.g., "VPS Key").
- Key: Paste the copied public key.
- Click Add SSH Key to save it.
Step 6: Test the SSH Connection
To ensure everything is set up correctly, test the SSH connection with GitHub:
Run the following command:
ssh -T git@github.com
If the setup is successful, you will see a message like this:
Hi your_github_username! You've successfully authenticated, but GitHub does not provide shell access.
Congratulations! You have successfully configured SSH for GitHub on your server.
Summary
Here’s a quick recap of the steps involved:
- Log in to your server using an SSH client.
- Generate an SSH key using
ssh-keygen
. - Start the SSH agent with
eval "$(ssh-agent -s)"
. - Add the SSH key to the agent using
ssh-add
. - Copy the public key and add it to your GitHub account via the Settings > SSH and GPG Keys section.
- Test the SSH connection with
ssh -T git@github.com
.
Setting up SSH ensures a secure and hassle-free way to interact with GitHub repositories from your server. If you encounter any issues, double-check the commands and ensure that the SSH key is properly added to your GitHub account. Happy coding!