Linux创建秘钥文件登录

生成 .pem 步骤如下:
1、

  • 在服务器端直接生成密码密钥对
    $ ssh-keygen -t rsa -b 2048 -v
    执行上述命令首先会让你输入生成密钥的文件名:我这里输入的 323,之后一路回车。
    在执行命令的当前目录下会生成一个323.pub、323两个文件。
  • 或者在Xshell里创建秘钥文件。(未测试完)

2、

把生成的 323.pub 通过本地命令推送到服务器端,使服务器自动添加认证这个证书
$ ssh-copy-id -i 323.pub zhangqing@192.168.3.23

zhangqing@zqubuntu:/data/ttt$ ssh-copy-id -i 323.pub zhangqing@192.168.3.23
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "323.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
zhangqing@192.168.3.23's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'zhangqing@192.168.3.23'"
and check to make sure that only the key(s) you wanted were added.


可能会报错:

报错 解决
root@zqubuntu:/data# ssh-copy-id -i /data/ttt/323.pub zhangqing@192.168.3.23

/usr/bin/ssh-copy-id: ERROR: failed to open ID file '/data/ttt/323': No such file
	(to install the contents of '/data/ttt/323.pub' anyway, look at the -f option)
这个问题的解决办法很简单,使用touch命令在公钥323.pub所在目录下创建一个空文件“323”即可。

3、

在服务器上测试连接:ssh -i 323.pub zhangqing@192.168.3.23

报错 解决
Permissions 0644 for ‘323.pub’ are too open.
仔细阅读了一下ssh文档和这句提示,大概的意思时ssh的私有密钥的权限开放尺度太大了,可以供人随意>欣赏了,ssh自身的策略关闭了ssh。
解决方案:将权限由0644降低为0600
root@zqubuntu:/data/ttt# chmod 600 323
root@zqubuntu:/data/ttt# ssh -i 323.pub zhangqing@192.168.3.23
Load key "323.pub": invalid format
zhangqing@192.168.3.23: Permission denied (publickey).
前期Xshell建立的秘钥报的错误。后来直接在服务器上生成秘钥,不报错了。

 

4、

复制323文件到客户端,然后可以直接用public key方式连接。

.pem后缀加不加都行。我默认没有这个后缀也没有加。

发表评论

1,040 次浏览