服务器取消密码,改用秘钥登录

 

最近服务器为了安全,取消了密码登录,换成公钥私钥对进行登录

 

前提环境:

服务器:Ubuntu20.04、操作用户需要有相应的权限,如root

本地:windows环境

 

一、本地windows环境生成密钥对

1、在安装了git的前提下,桌面上点击右键,打开Git Bash Here

2、输入命令:ssh -keygen -t rsa 然后一直enter变能生成密钥对

3、电脑上查看: 用户/username/.ssh,便能看见新生成的三个文件

 

二、服务器端更换设置使用秘钥登录

1、创建新用户,密码可不设置(若对已有的用户更换,跳过这一步)

adduser 用户名

2、进入需要更换秘钥的用户,创建.ssh文件夹(不能使用root或其他用户创建,不然会有权限问题)

mkdir  ~/.ssh

3、进入新创建的文件夹.ssh并创建文件authorized_keys

cd  ~/.ssh
touch  authorized_keys

4、编辑vim authorized_keys文件,把windows上创建的公钥(id_rsa.pub)添加进来

5、配置权限

chmod 700 ~/.ssh

chmod 600 ~/.ssh/authorized_keys

6、进入root用户,配置sshd_config

vim /etc/ssh/sshd_config

打开AuthorizedKeysFile

 

关闭PasswordAuthentication密码登录

 

7、重启SSH

Systemctl restart sshd

 

三、pycharm更换秘钥登录

Authentication认证的地方从password认证选择为Key pair,然后选择自己本机上的私钥即可。

 

问题:在pycharm使用私钥认证时,一直提示需要填写Passphrase私钥密码才行,不然无法使用;本机创建秘钥对一直enter,也没有创建什么密码。

在问了度娘后,可以使用openssl命令去掉私钥的密码

openssl rsa -in ~/.ssh/id_rsa -out ~/.ssh/id_rsa_new

参考:https://www.cnblogs.com/littlemonsters/p/5783672.html

 

但是我运行后会报错:unable to load Private Key

8824:error:0909006C:PEM routines:get_name:no start line:../openssl-1.1.1l/crypto/pem/pem_lib.c:745:Expecting: ANY PRIVATE KEY

 

然后又问了google,通过以下方式解决:(将原始密钥转换为PEM (SSH2)格式)

ssh-keygen -p -f ~/.ssh/id_rsa -m pem

参考:https://github.com/openssl/openssl/issues/13947

 

posted @ 2022-04-21 18:59  emunshe  阅读(581)  评论(0)    收藏  举报