SSH密钥
使用SSH协议,您可以连接和验证远程服务器和服务。使用SSH密钥,您可以在每次访问时无需提供用户名或密码即可连接到github。
在生成新的SSH密钥之前,可以先检查一下是否可以使用已有的ssh密钥。
- 打开你的终端窗口。
- 输入ls -al ~/.ssh查看已有SSH密钥:$ ls -al ~/.ssh
- 检查目录列表是否存在已有的公共SSH密钥。一般情况下,公钥的文件名是以下之一:
- id_dsa.pub
- id_ecdsa.pub
- id_ed25519.pub
- id_rsa.pub
- 如果秘钥文件已存在,则可以重新生成,也可以复用之前的pub文件粘贴到github
- 打开你的终端窗口,windows环境建议安装gitbash:https://git-scm.com/download/win
- 使用以下命令,将会以你的邮箱作为标签创建一个新的SSH密钥,注意运行下面命令生成sshkey时不限目录,key都会生成在~/.ssh目录下。
$ ssh-keygen -t ed25519 -C "zhansan@qq.com"
Generating public/private ed25519 key pair.
当系统提示您“输入要保存密钥的文件”时,按Enter键。将接受默认文件位置。
> Enter a file in which to save the key (/Users/you/.ssh/id_ed25519): [Press enter]
- 在提示符下,输入安全密码,即可生成一个新的ssh密钥。
- 注意: 如果生成ssh秘钥的时候输入了密码,则每次clone/push代码时都需要输入密码才行。
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
- 复制SSH密钥。
如果您的SSH密钥文件的名称与示例代码的名称不同,请修改文件名以匹配您当前的设置。复制密钥时,请勿添加任何换行符或空格。
macOS:
$ pbcopy < ~/.ssh/id_ed25519.pub
GNU/Linux (requires the xclip package):
xclip -sel clip < ~/.ssh/id_ed25519.pub
Windows Command Line:
type %userprofile%\.ssh\id_ed25519.pub | clip
Git Bash on Windows / Windows PowerShell:
cat ~/.ssh/id_ed25519.pub | clip
- 如果命令行复制不起作用,您可以找到隐藏.ssh文件夹,使用文本编辑器中打开id_ed25519.pub文件,复制内容,注意复制的时候去掉字符串结尾的换行和空格。 在个人设置界面,点击左侧SSH Keys页签,进入SSH Keys页面,管理你的SSH密钥。
git config --global url."git@github.com:".insteadOf "https://github.com/"
出现这种问题,由于openssl8.8P1后禁用rsa算法,需要在ssh_config里添加配置
$ ssh -vT github.com
OpenSSH_9.3p1, OpenSSL 3.1.1 30 May 2023
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [127.0.0.1] port 22.
debug1: connect to address 127.0.0.1 port 22: Connection refused
ssh: connect to host github.com port 22: Connection refused
配置如下
Host github.com
HostkeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa
在SSH配置文件中,设置github的主机名为 ssh.github.com 设置443端口
vim ~/.ssh/config
Host github.com
Hostname ssh.github.com
Port 443
git上传仓库
Git 全局设置
git config --global user.name "zhangsan"
git config --global user.email "zhangsan@qq.com"
...或在命令行上创建一个新的存储库
git clone git@github.com:sec/reform.git
cd reform
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
...或从命令行推送现有空的存储库
cd existing_folder
git init
git remote add origin git@github.com:sec/reform.git
git add .
git commit -m "Initial commit"
git push -u origin master
...或从命令行推送已有存储库
cd existing_repo
git remote set-url origin git@github.com:sec/reform.git
git push --all git@github.com:sec/reform.git