How to setup git in VPS Server?

Contents

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

  1. Open Git Bash on your computer.
  2. Enter the following command, replacing your_server_user_name and your_server_ip_address with your actual server credentials:

    ssh your_server_user_name@your_server_ip_address

    Example:

    ssh root@378.345.93.198
  3. 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:

  1. 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.

  2. You will be prompted to specify a file to save the key. Press Enter to save it in the default location (~/.ssh/id_ed25519).
  3. 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.
  4. 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

  1. Run the following command to view the public SSH key:

    cat ~/.ssh/id_ed25519.pub
  2. Copy the entire output, which is your public key.

Add the Key to GitHub

  1. Log in to your GitHub account.
  2. Navigate to Settings > SSH and GPG Keys.
  3. Click New SSH Key.
  4. In the form:
    • Title: Provide a descriptive name for the key (e.g., "VPS Key").
    • Key: Paste the copied public key.
  5. 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:

  1. Run the following command:

    ssh -T git@github.com
  2. 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:

  1. Log in to your server using an SSH client.
  2. Generate an SSH key using ssh-keygen.
  3. Start the SSH agent with eval "$(ssh-agent -s)".
  4. Add the SSH key to the agent using ssh-add.
  5. Copy the public key and add it to your GitHub account via the Settings > SSH and GPG Keys section.
  6. 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!