Using SSH with Azure DevOps: A Step-by-Step Guide
Azure DevOps supports SSH for securely connecting to Git repositories, ensuring both security and ease of use for developers. In this guide, we will cover everything from generating SSH keys to configuring and testing connections with Azure DevOps.
1. What is SSH and Why Use It with Azure DevOps?
Secure Shell (SSH) is a protocol used to establish secure connections between your computer and remote servers. Using SSH for Azure DevOps:
- Enhances security by avoiding plain-text passwords.
- Simplifies access to multiple repositories with a single key.
- Speeds up authentication by leveraging local key pairs.
2. Prerequisites
Before starting, ensure you have the following:
- Git Installed: If not, download and install Git.
- Azure DevOps Account: Sign up at Azure DevOps.
3. Generate an SSH Key
Step 1: Open Your Terminal
- On Linux/macOS: Use your terminal.
- On Windows: Use Git Bash or PowerShell.
Step 2: Generate a New SSH Key Pair
Run the following command:
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
-t rsa: Specifies the RSA algorithm.-b 4096: Sets the key size to 4096 bits for added security.-C "your-email@example.com": Adds a label (your email) to the key.
You will see a prompt:
Enter file in which to save the key (/home/username/.ssh/id_rsa):
Press Enter to save it in the default location (~/.ssh/id_rsa) or specify a custom path.
Step 3: Protect the Key with a Passphrase
(Optional) Enter a passphrase for added security, or press Enter to leave it blank.
4. Add Your SSH Key to Azure DevOps
Step 1: Copy the Public Key
Run the following command to display the public key:
cat ~/.ssh/id_rsa.pub
Copy the entire output, which starts with ssh-rsa.
Step 2: Add the Key to Azure DevOps
- Log in to Azure DevOps.
- Click on your profile picture (top-right) and select Security.
- Under SSH Public Keys, click + Add.
- Paste your public key in the Key Data field.
- Provide a description (e.g., “Work Laptop”) and click Add.
5. Configure Your Local Git Repository
Step 1: Set the SSH Remote URL
To connect your local repository to Azure DevOps, replace the HTTPS remote URL with an SSH URL. Use the following command:
git remote set-url origin git@ssh.dev.azure.com:v3/OrganizationName/ProjectName/RepositoryName
Replace:
OrganizationName: Your Azure DevOps organization.ProjectName: Your project name.RepositoryName: Your repository name.
Step 2: Verify the Remote URL
Run the following command to confirm the remote URL:
git remote -v
The output should display the SSH URL.
6. Test the SSH Connection
To ensure the SSH setup is correct, test the connection:
ssh -T git@ssh.dev.azure.com
If everything is set up correctly, you will see a message like:
Welcome to Azure Repos!
7. Adding Your SSH Key to the SSH Agent
To avoid entering the passphrase every time, add your private key to the SSH agent.
Step 1: Start the SSH Agent
eval "$(ssh-agent -s)"
Step 2: Add Your Private Key
ssh-add ~/.ssh/id_rsa
8. Using Git Commands with SSH
Now that SSH is configured, you can interact with Azure DevOps repositories using Git commands:
- Clone a Repository:
git clone git@ssh.dev.azure.com:v3/OrganizationName/ProjectName/RepositoryName - Push Changes:
git push origin branch_name - Pull Changes:
git pull origin branch_name
9. Troubleshooting Common Issues
Permission Denied (Publickey)
- Ensure the correct SSH key is added to the Azure DevOps account.
- Verify the SSH key is loaded in the SSH agent:
ssh-add -l
Connection Timeout
- Check your network/firewall settings to ensure SSH traffic is allowed.
Wrong Remote URL
- Confirm the remote URL uses the SSH format:
git remote -v
10. Summary
Using SSH with Azure DevOps provides a secure and efficient way to interact with Git repositories. By following this guide, you can generate an SSH key, configure Azure DevOps, and seamlessly perform Git operations.
