Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Git 安全 SSH


Git 安全

到目前为止,我们一直使用 HTTPS 连接到我们的远程仓库。

HTTPS 通常可以正常工作,但是如果您在不安全的网络中工作,则应使用 SSH。有时,项目会要求您使用 SSH。


什么是 SSH

SSH 是一种安全的 shell 网络协议,用于网络管理、远程文件传输和远程系统访问。

SSH 使用一对 SSH 密钥来建立经过身份验证和加密的安全网络协议。它允许在不安全的开放网络上进行安全的远程通信。

SSH 密钥用于启动安全的“握手”。在生成密钥对时,您将生成一个“公钥”和一个“私钥”。

“公钥”是您与远程方共享的密钥。可以将其理解为锁。

“私钥”是您自己保存在安全位置的密钥。可以将其理解为锁的钥匙。

SSH 密钥是通过安全算法生成的。它非常复杂,但它使用素数和大型随机数来生成公钥和私钥。

它的设计使得可以从私钥派生出公钥,但反之则不行。


生成 SSH 密钥对

在 Linux、Apple 的命令行以及 Windows 的 Git Bash 中,您可以生成 SSH 密钥。

让我们一步一步地进行操作。

首先使用您的电子邮件作为标签创建一个新的密钥

示例

ssh-keygen -t rsa -b 4096 -C "test@w3schools.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/user/.ssh/id_rsa):
Created directory '/Users/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/user/.ssh/id_rsa
Your public key has been saved in /Users/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:******************************************* test@w3schools.com
The key's randomart image is:
+---[RSA 4096]----+
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
+----[SHA256]-----+

在此创建过程中,系统会提示您以下内容

Enter file in which to save the key (/c/Users/user/.ssh/id_rsa):

选择文件位置,或按“Enter”使用默认文件位置。

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

输入安全密码将创建额外的安全层。防止任何获得计算机访问权限的人在没有密码的情况下使用该密钥。但是,每次使用 SSH 密钥时,都需要提供密码。

现在我们将此 SSH 密钥对添加到 SSH 代理(使用上面的文件位置)

示例

ssh-add /Users/user/.ssh/id_rsa
Enter passphrase for /Users/user/.ssh/id_rsa:
Identity added: /Users/user/.ssh/id_rsa (test@w3schools.com)

如果添加了密码,系统会提示您提供密码。

现在 SSH 密钥对已准备就绪。


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.