ssh免密登录
步骤1:在本机生成密钥
步骤1:在本机生成密钥
ssh-keygen -t rsa
此时,在 .ssh/ 目录下会多出两个文件:
Mac-mini:~ kyle$ ls -al .ssh/
total 24
drwx------ 5 kyle staff 170 Aug 20 22:41 .
drwxr-xr-x+ 47 kyle staff 1598 Aug 20 22:05 ..
-rw------- 1 kyle staff 1679 Aug 20 22:41 id_rsa
-rw-r--r-- 1 kyle staff 401 Aug 20 22:41 id_rsa.pub
-rw------- 1 kyle staff 803 Aug 17 19:27 known_hosts
步骤二:把生成的公共密钥添加到服务器端
ssh 登录 服务器,在服务器用户根目录中新建 .ssh/ 文件夹,并修改文件夹权限 700
mkdir .ssh
chmod 700 .ssh
上传本地公共密钥到服务器上
不要手动复制粘贴 id_rsa.pub 中的内容,是没用的,粘贴后的大小是不一样的。
cat .ssh/id_rsa.pub | ssh root@kylebing.cn -p 1122 'cat >> .ssh/authorized_keys'
[root@host ~]
total 12
drwx------ 2 root root 4096 Aug 20 10:11 .
dr-xr-x---. 5 root root 4096 Aug 20 09:31 ..
-rw------- 1 root root 401 Aug 20 10:15 authorized_keys
步骤三:配置服务器端ssh,允许使用密钥登录
编辑配置文件 /etc/ssh/sshd_config 找到这个位置,把这三行前面的
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
最后
service sshd restart
直接登录
完成正确配置之后应该是这样的:
本地的 .ssh/ 目录下应该有 id_rsa 和 id_rsa.pub 两个文件
服务器 .ssh/ 目录下的 authorized_keys 文件里应该有本地 id_rsa.pub 的内容
服务器 /etc/ssh/sshd_config 文件中已经配置了允许使用 密钥登录
现在就可以直接登录了,不需要使用密码。