设备添加ssh 给git仓库
Generating a new SSH key pair
Before creating an SSH key pair, make sure to understand the
different types of keys.
To create a new SSH key pair:
- Open a terminal on Linux or macOS, or Git Bash / WSL on Windows.
- Generate a new ED25519 SSH key pair:
ssh-keygen -t ed25519 -C "email@example.com"
Or, if you want to use RSA:
ssh-keygen -t rsa -b 4096 -C "email@example.com"
The -C flag adds a comment in the key in case you have multiple of them
and want to tell which is which. It is optional. - Next, you will be prompted to input a file path to save your SSH key pair to.
If you don't already have an SSH key pair and aren't generating a deploy key,
use the suggested path by pressing
Enter. Using the suggested path will normally allow your SSH client
to automatically use the SSH key pair with no additional configuration.
If you already have an SSH key pair with the suggested file path, you will need
to input a new file path and declare what host
this SSH key pair will be used for in your~/.ssh/config
file. - Once the path is decided, you will be prompted to input a password to
secure your new SSH key pair. It's a best practice to use a password,
but it's not required and you can skip creating it by pressing
Enter twice.
If, in any case, you want to add or change the password of your SSH key pair,
you can use the -p flag:
ssh-keygen -p -f <keyname>
Adding an SSH key to your GitLab account
-
Copy your public SSH key to the clipboard by using one of the commands below
depending on your Operating System:
macOS:
pbcopy < ~/.ssh/id_ed25519.pub
WSL / GNU/Linux (requires the xclip package):
xclip -sel clip < ~/.ssh/id_ed25519.pub
Git Bash on Windows:
cat ~/.ssh/id_ed25519.pub | clip
You can also open the key in a graphical editor and copy it from there,
but be careful not to accidentally change anything.
NOTE: Note:
If you opted to create an RSA key, the name might differ. -
Add your public SSH key to your GitLab account by:
1.Clicking your avatar in the upper right corner and selecting Settings.
2.Navigating to SSH Keys and pasting your public key in the Key field. If you:
- Created the key with a comment, this will appear in the Title field.
- Created the key without a comment, give your key an identifiable title like Work Laptop or Home Workstation.
3.Click the Add key button.
NOTE: Note:
If you manually copied your public SSH key make sure you copied the entire
key starting with ssh-ed25519 (or ssh-rsa) and ending with your email.