This topic explains how to set up Secure Shell (SSH) and HTTPS authentication for a repository.
Common issues with unauthorized Git command-line operations
Git command-line operations use two common authentication methods: SSH and HTTPS. When a Git command-line operation fails due to an authorization error, one of the following error messages might appear:
remote: Repository not found. Please make sure you have the correct access rights and the repository path is correct.
fatal: unable to access 'https://codeup.aliyun.com/...': The requested URL returned error: 403Or
Repository not found. Please make sure you have the correct access rights and the repository path is correct.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.Before you contact an administrator to resolve the authorization issue, confirm that you are using the correct account and the expected authentication method.
Git authentication method: SSH or HTTPS
For a cloned repository, when you run the git fetch or git push command, Git connects to the default remote server tracked by the current branch. This is usually the remote server specified by origin.
First, you can run the git status command to view the tracked remote branch:
$ git status
On branch master
Your branch is up to date with 'origin/master'.The output shows that the current master branch tracks the upstream origin/master branch.
To find the server address that origin points to, you can run git remote -v or the following command:
$ git remote get-url origin
git@codeup.aliyun.com:my/example.gitThe example output shows that the repository URL uses the SSH protocol. For the SSH protocol, two address formats are available:
A format that starts with the ssh:// protocol header, such as
ssh://git@codeup.aliyun.com:22/my/example.git.The address format used by the `scp` command:
git@codeup.aliyun.com:my/example.git.
For the HTTPS protocol, the repository address uses the standard URL format: https://[user]@host[:port]/path. For example: https://codeup.aliyun.com/my/example.git.
For the HTTPS protocol, the repository address uses the standard URL format: https://[user]@host[:port]/path. For example: https://codeup.aliyun.com/my/example.git. In this example, the server hostname is codeup.aliyun.com. If you omit the username, you are prompted for it on your first logon. A credential helper can also automatically provide the username from its cache. The last part of the URL is the repository path: my/example.git.
Git SSH authentication method
Git SSH authentication uses public key authentication. With this method, you generate a public-private key pair using ssh-keygen and upload the public key to your personal configuration on the code platform.
You can use SSH to associate a specific public-private key pair with a host alias. To do this, configure the ~/.ssh/config file as follows:
Host my.aliyun
User git
HostName codeup.aliyun.com
Port 22
IdentityFile /Users/my/.ssh/aliyun/id_rsaThis example configures the host alias my.aliyun for local use. When you clone the repository using ssh://my.aliyun/my/example.git, Git connects to codeup.aliyun.com on port 22 using the username git and the private key at /Users/my/.ssh/aliyun/id_rsa.
How to confirm the user account and key pair used for Git logon over SSH
For example, to check if authentication is successful and view the username for ssh://my.aliyun/my/example.git, you can use the ssh logon command:
$ ssh my.aliyun
Welcome to Codeup, gotgit!
Connection to codeup.aliyun.com closed.The output shows that while the Apsara Devops code platform server does not allow an interactive SSH logon, it confirms the logon account is gotgit before closing the connection.
You can also add the -v parameter to the SSH connection command to view the public-private key pair used for authentication, as shown in the following example:
$ ssh -v git@codeup.aliyun.com
... ...
debug1: Offering public key: /Users/my/.ssh/aliyun/id_rsa RSA SHA256:8lEmVm11YzWZtT1U1FRrOx3rpMN1gFpOe776biyNkrI explicit
debug1: Server accepts key: /Users/my/.ssh/aliyun/id_rsa RSA SHA256:8lEmVm11YzWZtT1U1FRrOx3rpMN1gFpOe776biyNkrI explicit
Authenticated to codeup.aliyun.com ([118.118.31.XXX.XX) using "publickey".
... ...On Windows, Git SSH authentication might use an alternative to the standard OpenSSH command. You can set a specific SSH command, such as plink.exe or tortoiseplink.exe, using GIT_SSH_COMMAND or core.sshcommand. The logon verification method is the same as described above.
Git HTTPS authentication method
When you access a remote Git repository using the HTTPS protocol, Git typically uses Basic Authentication. To log on, you use your username for the code platform and a security token created for Git authentication. To avoid repeatedly entering your username and token, Git provides a credential helper to cache your username and security token. This means you usually only need to enter your username and token on your first logon.
You can run the following command to view the credential helper used by your current system:
$ git config credential.helper
storeIn this example, the credential helper is set to store. This causes Git to use the internal git-credential-store command to cache the security token. On Windows, git-credential-wincred is a common credential helper.
How can you determine the username used for the current Git HTTPS authentication? It can be difficult to retrieve the username by simulating the interaction between Git and the credential helper.
To confirm that Git uses a specific username for HTTPS authentication, include the username in the URL. Do not include the token in the URL.
For example, if you include the username got*** in the URL, such as https://got***@codeup.aliyun.com/my/example.git, you can confirm that this username is used for all operations on the repository.
You can use the git remote command to modify the address of a cloned local repository.
To view the URL of the remote repository named origin, run the command:
git remote get-url origin.To set the URL of the remote repository named origin, run the command:
git remote set-url origin.