Skip to content

ssh配置无密码本地登录

1. 本地环境 操作

1.1 生成私钥、公钥

shell
ssh-keygen -t rsa -b 4096 -C "xxx@xxx.com"  # 你的邮箱,最好是跟git账号对应的邮箱(如果有)一样
# 一路三次回车键,此时,在:~/.ssh/会有两个文件 id_rsa(私钥)和 id_rsa.pub(公钥),然后开起来 ssh 代理:

1.2 开启 ssh 代理

shell
eval "$(ssh-agent -s)"
# Agent pid 26794

1.3 把 ssh key 加入到 ssh 代理中

shell
ssh-add ~/.ssh/id_rsa
# Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

1.4 查看公钥文件内容

shell
cat ~/.ssh/id_rsa.pub # 先记录起来,接下来服务器需要用到

2. 服务器 (非root账号) 操作

我们进入到 git_manager 用户登录的终端中,进行操作:

PS:非root账号登录。如果没有,新建,并赋予跟root相同权限

2.1 为该账号生成私钥、公钥

重新再把刚才的流程走一遍,一模一样的步骤:
为了方便以后从 GitHub 或者 Gitee 之类的仓库获得仓库的权限

shell
ssh-keygen -t rsa -b 4096 -C "xxx@xxx.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

2.2 创建授权文件

shell
vi .ssh/authorized_keys
# 将本地环境的id_rsa.pub的公钥粘贴在这里
# 保存,退出
wq!

2.3 重启ssh服务

shell
# 修改 600 可读授权(这个非必须,如果重启ssh,无密码登录无效时才需要)
chmod 600 ~/.ssh/authorized_keys

# 重启ssh服务
# CentOS 6或以下
service ssh restart
# CentOS 7或以上
systemctl restart sshd

2.4 验证

在本地环境,打开一个cmd终端,执行:

shell
ssh git_manager@192.168.85.130

通过 ssh git_manager@192.168.85.130 就直接登录了,就不需要输入密码了

最近更新