Making an SSH key and adding it to GitHub - michael-D-S/SYS-265 GitHub Wiki

Check for Existing SSH Keys on web01: Before creating new ones, check if you already have SSH keys on your system. Open your terminal and enter:

bash ls -al ~/.ssh This command lists the files in your .ssh directory (if it exists). Look for files named id_rsa or id_ed25519 (or similar) and their corresponding .pub files (e.g., id_rsa.pub). If you find these, you might already have keys. If not, proceed to the next step.

Generate a New SSH Key Pair on web01: If you don't have SSH keys or want to create a new one for this purpose, use the following command:

bash ssh-keygen -t ed25519 -C "[email protected]" Replace "[email protected]" with your actual email address associated with your GitHub account.

When prompted to "Enter a file in which to save the key," you can press Enter to accept the default location (~/.ssh/id_ed25519). Important: If you already have existing keys, consider giving the new key a different name to avoid overwriting your existing keys.

When prompted to "Enter passphrase (empty for no passphrase)," you can enter a passphrase for extra security or leave it empty by pressing Enter twice. For this lab and general ease of use, it's probably fine to leave it empty. However, in a production environment, using a passphrase is highly recommended.

Add the SSH Key to Your GitHub Account:

Copy the Public Key: You need to copy the contents of your public key (.pub file) to your GitHub account. Use the following command to display the public key:

bash cat ~/.ssh/id_ed25519.pub Select and copy the entire output, starting with ssh-ed25519 and ending with your email address.

Add the Key to GitHub:

Go to your GitHub account in a web browser.

Click on your profile picture in the upper-right corner and select "Settings."

In the left sidebar, click on "SSH and GPG keys."

Click the "New SSH key" or "Add SSH key" button.

In the "Title" field, enter a descriptive name for the key (e.g., "web01").

In the "Key" field, paste the contents of your public key that you copied in the previous step.

Click "Add SSH key." You may be prompted to confirm your password.

Test the SSH Connection:

After adding the SSH key to your GitHub account, test the connection with the following command from web01:

bash ssh -T [email protected] You might see a warning message about an unknown host. Type yes to continue. If everything is set up correctly, you should see a message like:

text Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access. Replace "yourusername" with your actual GitHub username. This message confirms that SSH authentication is working from web01.

Update Your Remote Repository URL (if necessary):

If you were previously using an HTTPS URL for your remote repository, you'll need to update it to use the SSH URL.

Check the current remote URL:

bash git remote -v This will show you the URLs for your origin remote. If they start with https://, you need to change them.

Change the remote URL:

bash git remote set-url origin [email protected]:yourusername/yourrepository.git Replace yourusername and yourrepository with your GitHub username and repository name.

Verify the change: Run git remote -v again to confirm the remote URL has been updated.

Try Pushing Again:

Now, try pushing your changes to GitHub from web01:

bash git push origin main You should no longer be prompted for your username and password, and the push should succeed using SSH authentication.