远程连接ubuntu服务器设置ssh免密码登陆的简单方式

在工作中我们经常需要登陆服务器,但是登陆服务器就需要输入密码,这里介绍一种简单的免密码的登陆方式,这里默认服务器已经安装了ssh server,本地也安装好了ssh client,我用的是mac,本地无须安装ssh client,只需要服务器安装ssh server就行了:

sudo apt-get install openssh-server

首先生成ssh key,如果已经生成了,则无须重复生成:

ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

如果已经生成了,那么进入cd ~/.ssh cat id_rsa.pub查看

然后:ssh-copy-id 你的用户名@你的IP

例如:

(base) ➜  ~ ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/eric/.ssh/id_ed25519.pub"
The authenticity of host '10.18.96.116 (10.18.96.116)' can't be established.
ECDSA key fingerprint is SHA256:zRR/HcgYecAov/tjE1NZZgXnw4CK42GjgrXAZN2Wxq8.
Are you sure you want to continue connecting (yes/no)? yes
/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
[email protected]'s password:

Number of key(s) added: 1

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

(base) ➜  ~ ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/admin/.ssh/id_rsa.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
[email protected]'s password:

Number of key(s) added:        1

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

会自动把公钥传输到服务端的$HOME/.ssh/authorized_keys文件夹里,这样就把公钥上传到服务器上了,全程无须手动复制,是不是很简单呢。

每次登录就不用输密码了

参考文献

[1]. VScode Remote 远程开发与调试. https://www.jianshu.com/p/0f2fb935a9a1

猜你喜欢

转载自blog.csdn.net/w5688414/article/details/105386894
今日推荐