GitLab and SSH keys

本文详细介绍了如何在GitBash中配置用户名和邮箱,生成SSH密钥,并将其添加到GitLab中,实现安全的远程仓库访问。步骤包括检查SSH密钥存在情况,生成新的SSH密钥,复制并粘贴SSH密钥至GitLab账户。

下载安装好gitbash

1.配置用户名和邮箱:

举例:

$ git config --global user.name "zxm 

$ git config --global user.email "zxm@xxx.com"

2.检查是否有ssh key:

cat ~/.ssh/id_rsa.pub

为空说明没有,需要生成

3.生成ssh key:

ssh-keygen -t rsa -C "your.email@example.com" -b 4096

4.复制ssh key:

cat ~/.ssh/id_rsa.pub | clip

5.登陆gitlab,点开Profile Settings--》SSH Keys,把刚才复制的内容粘贴进去。注意是以ssh-rsa开头,以邮箱结尾

 

 

彩蛋:查看git bash中用户名和邮箱:

git config --list

 

GitLab and SSH keys Git is a distributed version control system, which means you can work locally, then share or "push" your changes to a server. In this case, the server is GitLab. GitLab uses the SSH protocol to securely communicate with Git. When you use SSH keys to authenticate to the GitLab remote server, you don't need to supply your username and password each time. Prerequisites To use SSH to communicate with GitLab, you need: The OpenSSH client, which comes pre-installed on GNU/Linux, macOS, and Windows 10. SSH version 6.5 or later. Earlier versions used an MD5 signature, which is not secure. To view the version of SSH installed on your system, run ssh -V. Supported SSH key types To communicate with GitLab, you can use the following SSH key types: ED25519 RSA DSA (Deprecated in GitLab 11.0.) ECDSA (As noted in Practical Cryptography With Go, the security issues related to DSA also apply to ECDSA.) Administrators can restrict which keys are permitted and their minimum lengths. ED25519 SSH keys The book Practical Cryptography With Go suggests that ED25519 keys are more secure and performant than RSA keys. OpenSSH 6.5 introduced ED25519 SSH keys in 2014 and they should be available on most operating systems. RSA SSH keys Available documentation suggests that ED25519 is more secure than RSA. If you use an RSA key, the US National Institute of Science and Technology in Publication 800-57 Part 3 (PDF) recommends a key size of at least 2048 bits. The default key size depends on your version of ssh-keygen. Review the man page for your installed ssh-keygen command for details. See if you have an existing SSH key pair Before you create a key pair, see if a key pair already exists. On Windows, Linux, or macOS, go to your home directory. Go to the .ssh/ subdirectory. If the .ssh/ subdirectory doesn't exist, you are either not in the home directory, or you haven't used ssh before. In the latter case, you need to generate an SSH key pair. See if a file with one of the following formats exists: Algorithm Public key Private key ED25519 (preferred) id_ed25519.pub id_ed25519 RSA (at least 2048-bit key size) id_rsa.pub id_rsa DSA (deprecated) id_dsa.pub id_dsa ECDSA id_ecdsa.pub id_ecdsa Generate an SSH key pair If you do not have an existing SSH key pair, generate a new one. Open a terminal. Type ssh-keygen -t followed by the key type and an optional comment. This comment is included in the .pub file that's created. You may want to use an email address for the comment. For example, for ED25519: ssh-keygen -t ed25519 -C "<comment>" For 2048-bit RSA: ssh-keygen -t rsa -b 2048 -C "<comment>" Press Enter. Output similar to the following is displayed: Generating public/private ed25519 key pair. Enter file in which to save the key (/home/user/.ssh/id_ed25519): Accept the suggested filename and directory, unless you are generating a deploy key or want to save in a specific directory where you store other keys. You can also dedicate the SSH key pair to a specific host. Specify a passphrase: Enter passphrase (empty for no passphrase): Enter same passphrase again: A confirmation is displayed, including information about where your files are stored. A public and private key are generated. Add the public SSH key to your GitLab account and keep the private key secure. Configure SSH to point to a different directory If you did not save your SSH key pair in the default directory, configure your SSH client to point to the directory where the private key is stored. Open a terminal and run this command: eval $(ssh-agent -s) ssh-add <directory to private SSH key> Save these settings in the ~/.ssh/config file. For example: # GitLab.com Host gitlab.com PreferredAuthentications publickey IdentityFile ~/.ssh/gitlab_com_rsa # Private GitLab instance Host gitlab.company.com PreferredAuthentications publickey IdentityFile ~/.ssh/example_com_rsa For more information on these settings, see the man ssh_config page in the SSH configuration manual. Public SSH keys must be unique to GitLab because they bind to your account. Your SSH key is the only identifier you have when you push code with SSH. It must uniquely map to a single user. Update your SSH key passphrase You can update the passphrase for your SSH key. Open a terminal and run this command: ssh-keygen -p -f /path/to/ssh_key At the prompts, type the passphrase and press Enter. Upgrade your RSA key pair to a more secure format If your version of OpenSSH is between 6.5 and 7.8, you can save your private RSA SSH keys in a more secure OpenSSH format. Open a terminal and run this command: ssh-keygen -o -f ~/.ssh/id_rsa Alternatively, you can generate a new RSA key with the more secure encryption format with the following command: ssh-keygen -o -t rsa -b 4096 -C "<comment>" Add an SSH key to your GitLab account To use SSH with GitLab, copy your public key to your GitLab account. Copy the contents of your public key file. You can do this manually or use a script. For example, to copy an ED25519 key to the clipboard: macOS: tr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopy Linux (requires the xclip package): xclip -sel clip < ~/.ssh/id_ed25519.pub Git Bash on Windows: cat ~/.ssh/id_ed25519.pub | clip Replace id_ed25519.pub with your filename. For example, use id_rsa.pub for RSA. Sign in to GitLab. In the top right corner, select your avatar. Select Preferences. From the left sidebar, select SSH Keys. In the Key box, paste the contents of your public key. If you manually copied the key, make sure you copy the entire key, which starts with ssh-ed25519 or ssh-rsa, and may end with a comment. In the Title text box, type a description, like Work Laptop or Home Workstation. Optional. In the Expires at box, select an expiration date. (Introduced in GitLab 12.9.) The expiration date is informational only, and does not prevent you from using the key. However, administrators can view expiration dates and use them for guidance when deleting keys. GitLab checks all SSH keys at 02:00 AM UTC every day. It emails an expiration notice for all SSH keys that expire on the current date. (Introduced in GitLab 13.11.) GitLab checks all SSH keys at 01:00 AM UTC every day. It emails an expiration notice for all SSH keys that are scheduled to expire seven days from now. (Introduced in GitLab 13.11.) Select Add key. 翻译一下
07-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值