遠程連接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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章