第一步:
首先在本地创建密钥对
ssh-keygen -f /root/.ssh/id_rsa -N "" -b 2048 #这是非交互创建方式
如果已经存在密钥了,有的时候会报和权限相关的错误,需要将密钥的权限修改为600
然后拷贝公钥的内容
cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDwk9SsFCtUTnNHj4bHVKhGj3VYnGHXRX0AhhL7BN2c82OT/BWtPRSkbYOvsI6pd88MUv4aKdIENwR450wq5oGy0mz9apWvddy/QvprpFEWtTTDb0lRepbz8+DmKtO3GYcdGyEQeMhsQ5CSMJ0iXRTAGDOwT+p4LTzEPXn0JX5d0HFranTBZXLH3kgtU+ImAelXFy8eVkuRivhDIYAHLGXbRF1DXZOAYx/ecX7B5DoSaKKMFABPO7veZppv2SPJVwXWDXYe0JXa0/fCXetbNSl2ffHbDKgk8gK1RF+TnxuP6UZ05WG/h/4UYn7mYz/9tuA3SD5B7PbP/Yk+qrxBs5G5 root@room9pc01.tedu.cn
第二步
进入github账户中
点击右上角图像,打开设置,里面有添加ssh密钥,将拷贝的公钥内容粘贴到里面,标签随便写,最好是让自己知道是哪一台主机的公钥。然后点击添加,这个时候就在github账户中添加好了ssh公钥
第三步
在自己本机上和远程github仓库绑定
yum -y install git #安装git
git config --global user.name 'LonersBoy' #设置git用户名
git config --global user.email 'zhao1991mg@sina.cn' #设置git邮箱地址
git config --global core.editor vim #设置git默认代码编辑器
git config --list #查看git的所有设置
mkdir github #创建一个目录作为github的仓库
cd github #进入目录
git init #初始化仓库,此时就已经创建好了一个本地仓库,也可以不创建本地仓库,直接clone远程仓库
git remote add origin git@github.com:LonersBoy/Learn.git #这里的地址一定要选择ssh地址,不要选择http地址,否则依然会要求输入密码
git push -u origin --all #将本地仓库推送到远程仓库
git clone git@github.com:LonersBoy/Learn.git #直接将远程库clone到本地,clone的时候选择ssh
此时本地库就可以直接通过git push进行免密上传代码文件了
可能会遇见的报错
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
sign_and_send_pubkey: signing failed: agent refused operation
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
这个是不你的公钥出问题了,而是没有识别,所以需要进行ssh代理,执行一下以下命令就可以了
[root@room9pc01 Learn]# ssh-agent #开启代理
SSH_AUTH_SOCK=/tmp/ssh-pSewP85dFWYr/agent.12418; export SSH_AUTH_SOCK;
SSH_AGENT_PID=12419; export SSH_AGENT_PID;
echo Agent pid 12419;
[root@room9pc01 Learn]# ssh-add #添加代理
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
这样就可以很好的进行无密码上传代码了