GitHub is a popular platform for hosting and managing source code repositories. It offers many features that make it easy to collaborate with other developers and contributors. One of the key features of GitHub is its support for SSH authentication, which allows you to securely access your repositories without entering your username and password every time. It's worth noting that Github no longer supports access to repositories using a username and password, so using SSH authentication or personal access tokens is essential. However, there may be situations where you need to configure a second SSH access to a GitHub repository in a server. Here's how to do it.
Step 1: Generate a new SSH key pair
The first step is to generate a new SSH key pair for the second access. Open the terminal and run the following command:
ssh-keygen -t rsa -b 4096 -C "username@servername.com"
Login to your server admin panel to get the username and servername, if you don't know how to find them, here is how to find your servername in Cpanel.
You will require a passcode for the RSA key pair, but I recommend leaving it empty by taping ENTER key, then specifying where to put the key usually it's here:
/home/username/.ssh/id_rsa_file_name
change the file_name to the repository name for example to defer between different keys.
This will create a new SSH key pair using the Ed25519 algorithm and associate it with the email address you provided. You will be prompted to choose a location to save the key pair. The default location is ~/.ssh/id_rsa_file_name
.
Step 2: Add the new public key to your Github account
The next step is to add the new public key to your Github account. Open the id_rsa_file_name.pub
file in a text editor and copy its contents, if you're in a putty session type
cat ~/.ssh/id_rsa_file_name.pub
Then, go to your GitHub repo settings and click on the "SSH and GPG keys" section. Click on the "New SSH key" button and paste the copied public key into the "Key" field. Give the key a descriptive name in the "Title" field, such as "Server 2 SSH key". Finally, click on the "Add SSH key" button to save the new key.
At this point, you have successfully configured an ssh connection to the repo from the server. these steps are the same for the first configuration. To configure the second connection follow this third step
Step 3: Adding the config file
This step is important so you could let the server defer between the two connections (keys).
vi ~/.ssh/config
On your keyboard, click "i", then type or copy-paste:
#public account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_first_file_name
#special account
Host github.com-2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_second_file_name
Change id_rsa_file_name
in both to the name you specified.
Step 4: Test the new SSH connection
The final step is to test the new SSH connection by cloning the repository using the git clone
command. Open the terminal and run the following command:
// to access via first configuration
git clone git@github.com:username/repo.git
// to access via second configuration
git clone git@github.com-2:username/repo.git
Replace username
with your GitHub username and repo
with the name of the repository, you want to clone. If the new SSH connection is working properly, you will be prompted to add the host to the list of known hosts. Type "ENTER" to add the host and continue cloning the repository.
Conclusion
Configuring a second SSH access to a GitHub repository in a server is a straightforward process that requires a few simple steps. By following these steps, you can securely access your GitHub repositories from multiple servers or devices without having to share your primary SSH key.