Connecting a GitHub repository to a Linux VPS (Virtual Private Server) involves several steps. Below is a general guide to help you achieve this:
Set Up SSH Keypair On your VPS
The SSH Keypair comprises a Private Key and a Public key. We can generate both of these keys on the VPS server using the following command:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
- Make sure you put the email you use on your GitHub account
Please follow the prompts and make sure you leave the passphrase field empty if you want to access GitHub without entering a password.
It’s important to note that when you run the ssh-keygen
command with the options -b 4096 -t rsa
, it generates a 4096-bit RSA key pair. The key pair consists of a private key and a public key. The default names for these keys are usually as follows:
- Private Key:
id_rsa
- Public Key:
id_rsa.pub
These files are typically stored in the ~/.ssh/
directory in your home folder. If the files with these names already exist, ssh-keygen
will prompt you to confirm whether you want to overwrite them or choose a different name.
Append the Public Key to the Authorized Keys
Appending the public key to the authorized_keys
file on the server is a crucial step in setting up SSH key-based authentication. This process establishes a trust relationship between your local machine and the remote server, allowing you to authenticate without entering a password each time you connect.
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
This command takes the content of the local public key file and appends it to the authorized_keys
file on the server. Each line in the authorized_keys
file represents an authorized public key, and when a user tries to connect to the server, the server checks whether the provided private key matches any of the public keys in this file. If a match is found, the user is granted access without a password prompt.
Display & Copy the Public key content
The content of the public key is what we will use in our GitHub account to complete the authentication. To achieve this, we need to view and copy the output of the id_rsa-pub
public key:
cat ~/.ssh/id_rsa.pub
Configure your GitHub
At this point, we need to set up your GitHub account to be able to connect to your VPS server;
Add the public SSH Key content
You need to copy the content of the public SSH Key you generated earlier. And then, go to your GitHub account settings > SSH and GPG keys > New SSH key.
Now, you will need to paste the key. Make sure you give your Key the appropriate name in GitHub for proper identification. This could be useful if you’re running multiple repositories that are connected to their own servers
Clone Repository on VPS
Cloning a GitHub repository when connecting it to a Virtual Private Server (VPS) is essential for obtaining a local copy of the source code, ensuring access to the version history through Git’s version control system, and acquiring dependencies and configuration files crucial for application setup.
This process facilitates collaboration, allowing easy updates and contributions to the project. By cloning, developers establish a connection between the GitHub repository and the VPS, ensuring that the server has the latest codebase and providing the necessary components for the successful deployment and operation of the application on the remote server.
Navigate to the right directory
On your VPS, navigate to the directory where you want to clone the repository.
cd /path/to/your/desired/directory
Create a clone copy
You can clone the repository using the SSH URL.
git clone git@github.com:username/repo.git
The provided [email protected]:username/repo.git
URL includes the Git protocol and the specific GitHub repository path, allowing you to fetch the code and collaborate on the project.
username
: This is your Github account Usernamerepo.git:
This is the target repository you want to clone on your VPS server. Make sure you replacerepo.git
with the actual name of your repository. It’s important to note that GitHub repository names usually have a.git
extension
Pull Changes on VPS
Periodically pull changes from the GitHub repository to keep your VPS up-to-date.
cd /path/to/your/cloned_repo
git pull
Now, your GitHub repository is connected to your Linux VPS. Make sure to secure your VPS and follow best practices for managing your repository and server.